Python basic Syntax

Python has many similarities to other programming languages  such as C, Java and Perl. However it has some differences as well. The Python basic syntax tells the rules must followed while writing code in Python. For example, In Python, we use indentation to create blocks like conditional statements or loops.

Execution –

Python code can be directly executed in the command line interpreter.

>>>print("Hello World")
Hello World

However, if you want to make a separate file, you need to save the file with the extention .py. For example – mytest.py.

Code within a file named mytest.py

print("Hello World")

Once the file is saved, you can either run the code in python shell or directly inside the command prompt.

Running in Python Shell-

Hello World

C:\Users\Your Name>python myfile.py 

Indentation in python –

Indentation is the space at the start of a particular line of code. While indentation in other programming languages is optional and is often used to make the code look systmetic and beautiful, in python indentation is mandatory. Unlike other programming languages, python uses indentation instead of braces, to indicate a block of code. A block of code could be a function, a conditional statement, a loop or a class.

For Example –

a = 10
if a > 7 :
    print("yes")
else :
    print("No")

However, if you ignore the indentation when defining a block, you would get an error.

a = 10
if a > 7 :
print("yes")
else :
print("No")

When this program runs, an error would be generated that says – “Indentation Error: expected an indented block”

Ending a statement –

Unlike other programming languages, python does not use semicolon(;) to end a statement. Rather to terminate a statement in python, we simply press enter.

Comments in python –

Comments in python can be multiline or a single line. A single line comment can be written by adding a hash sign (#) before a string. While a multiline comment can be written inside triple quotes as follows:

#This is a single line comment
'''This is comment line 1
This is comment line 2
This is comment line 3'''

Note: As long as a string literal is not assigned to a variable, it is ignored by the python interpreter. To learn more about comments in python, go to Python Comments

Reserved keywords in python

Reserved keywords in a programming language is a set of predefined words that cannot be used as an identifier such as name of a variable, a function or a class. This is a list of reserved keywords within python programming language:

and assert break
class continue def
del elif else
except exec finally
for from global
if import in
is lambda not
or pass print
raise return try
while with yield