Python Statement, Indentation and Comments

Python Statement, Indentation and Comments


In this section we will demonstrate the use of indentation in Python and will learn about comments in Python programming language.

 

Python Statement

A statement in Python is what interpreter executes or the instructions given to interpreter to be executed are known as statements. For example an assignment statement that is z = 5 is a statement that assigns the value 5 to a variable z. Statements are of many types in Python such as while statement, if statement, etc.

Multi line Statement

In Python there is a continuation character (\) which is used to extend a statement over multiple lines. This makes the statement a multi line statement. Multi line statement can also be declared using a parentheses (), braces {}, or square brackets []. We can also use semi colon to define multiple statements in one single line.

EXAMPLE using Continuation Character (\)

If the user wants to calculate the sum of some integers he can calculate in multi line statement using the continuation character. Consider the following example:

x = 1 + 2 + 3 + \

        4 + 5 + 6 + \

        7 + 8 + 9

EXAMPLE using parentheses ()

Multi line statement can also be declared using parentheses (). Consider the following example:

x = (1 + 2 + 3 +

        4 + 5 + 6 +

        7 + 8 + 9)

EXAMPLE using braces {}

Multi line statement can also be declared using braces {}. Consider the following example:

x = {1 + 2 + 3 +

        4 + 5 + 6 +

        7 + 8 + 9}

EXAMPLE using square brackets []

Multi line statement can also be declared using square brackets []. Consider the following example:

x = [1 + 2 + 3 +

        4 + 5 + 6 +

        7 + 8 + 9]

EXAMPLE using semi colon (;)

We can also use semi colon to define multiple statements in one single line. Consider the following example:

x = 2; y = 3; z = 4

 

Python Indentation

Python indentation is used to define a block of statement under a body of function or loop or decision structure etc. In other languages such as Java, C, C++ braces {} were used to define a block of statement. The body of function or loop is started from a colon. Usually four white spaces are used for indentation.

Consider the following example

CODE

>>> j = 1

>>> while(j<=5):

          print(j)

          j = j+ 1

OUTPUT

1

2

3

4

5

print

When the lines of code are indented they look neat. Therefore, it is a good practice to indent the code. Through indentation we get consistent and reliable results.  In the above example the block of statements under the while loop is indented and looks clear that the user understands which lines are under the loop and which lines are not the part of loop. It can be observed that indentation is not used in the continuation of lines in above example. We can write the above lines of code in the following way as well.

CODE

>>> j = 1

>>> while(j<=5): print(j); j = j + 1

OUTPUT

1

2

3

4

5

while

It can be seen that both methods generated the same result and hence are consistent and clearer. If the indentation is incorrect some way then interpreter will generate an error Indentation Error. And if there are white spaces that are not required then interpreter will generate an error that is Unexpected Indent.

 

Python Comments

The # symbol is used to add a comment to a python code. A comment is added to add more description to a program. Comments also make the user understand the code well.

The comments in any programming language are ignored and are not compiled or interpreted. Comments are used to add assumptions. They describe the working of the program to the user. This saves time of user. Consider the following example in which we added comment to each line of the code to explain the working of the program:

CODE

>>> a = 1; b= 3 #Declaring two integers

>>> c = a + b #adding two integers

>>> print(c) #displaying the result

OUTPUT

4

python-commentspython-comments

 

Multi Line comments

Multi line comments can be added to multiple lines by using the hash symbol (#) at the beginning of each line. Consider the following example:

# This program calculates

# sum of two integers

# and displays the result

Another way to add multi line comment is by using the triple quotes that “””. These triple quotes are used for multi line strings but can also be used for multi line comments. Consider the following example:

“””Inserting

       Multi line

       Comments to

       our program using

       triple quotes”””

 

Docstring in Python

Docstring stands for documentation string. To write a docstring in Python triple quotes are used. A docstring is a string that is the first statement in a module, function, and class or method definition describing the functionality of class, module, function or method. Consider the following example:

CODE

>>> def function1():

       “”” Outer function of nested function “””

          x = 1

          return x

>>> def function2():

        “”” Inner function of nested function “””

          x = 2

>>> print(function1())

>>> print(function2())

OUTPUT

1

None