Python Basics Part - 1
I am writing this blog on Python basics and trying to give some practical examples. Here let's discuss Syntax, rules, syntax errors, and practical code examples.
synatx:-In Python, the syntax is like the rules of grammar for the computer—it tells the computer how to read and understand the code you write, like using correct spaces, keywords, and symbols to make the program work properly.
Basic syntax rules in Python
Case Sensitivity
in this example, name
and Name
are considered two different variables because of the difference in capitalization. Changing the case of letters changes the meaning and behavior of the code.
Single line comment
In this example, suppose you want to write something as a form of comment then before a sentence use the '#' symbol.
Multi-line comment
in this example, if you want to write more than one comment then use triple quotes intended for multi-line comment and the symbol is '''.
Indentation
Indentation in Python is crucial because it defines the structure and grouping of code blocks, such as those inside loops, conditionals, and functions. Python uses indentation (usually 4 spaces) to indicate which lines of code belong together.
In this example, both print
statements are indented to show that they are part of their respective if
blocks.
Identation Error
Here, the lack of indentation for the print
statement will cause an IndentationError
because Python expects indented code under the if
statement.
Line Continuation
use backslash (\) to continue a statement to the next line.
Multiple Statements on a single line
Assigns 5 to x
Assigns 10 to y
Prints the sum of x and y, which is 15.
Type inference
Name error
This happens because x has not been assigned a value before trying to print it.
Thanks for reading this blog.
Subscribe to my newsletter
Read articles from Nabaranjan palatasingh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nabaranjan palatasingh
Nabaranjan palatasingh
Aspiring Data Scientist | Machine Learning | AI | DevOps