python

Python Statement, Indentation and Comments


In this tutorial, you will learn how to write Comments in Python.


Python Statements

Instructions that can be executed by python interpreter are called statements. For example, c = 1 is an assignment statement.
We will discuss later on for statement, if statement, while statement, etc

Multiline Statement

In Python, to make statement extend over multiple lines we use line continuation character (\).
For example:

a = 100 + 200 + 300 + \
    400 + 500 + 600 + \
    700 + 80 + 900
This is called an explicit line continuation. In Python, parentheses ( ), brackets [ ], and braces { } are used for line continuation. For example, we can write the above multi-line statement as:
a = (100 + 200 + 300 +
    400 + 500 + 6000 +
    700 + 800 + 90)
Here, the parentheses ( ) are doing the line continuation implicitly. Same is the case with [ ] and { }. We can also put multiple statements in a single line using semicolons
For example:
a = 100; b = 200; c = 300


Python Indentation

In C, C++, Java,etc braces {} are used to define a block of code. However, Python use indentation.
A block of code (body of a function, loop, etc.) starts with indentation and ends with the first unindented line. The amount of indentation is up to you, but it must be same throughout that block.
Usually, four whitespaces are used for indentation and are preferred over tabs.
For example

for i in range(1,11):
    print(i)
    if i == 6:
        break

Indentation makes our code look neat and clean. As a result in programs in python looks similar and consistent.
It makes the code more readable.
For example:

if True:
    print('WELCOME TO CODEMISTIC')
    a = 5

and

if True: print('WELCOME TO CODEMISTIC'); a = 5

Both the codes are correct and do the same thing, but the former style is clearer.

Incorrect indentation will result in IndentationError.


Python Comments

Comments are statements which are ignored by the interpreter or compiler.
We usually create comments to let developers understand our code’s logic.
This is a good and necessary practice, and good developers make heavy use of the comment system.
Without it, things can get confusing.

In Python, we use the hash (#) symbol to start creating a comment.

#This is a comment
#print out WELCOME TO CODEMISTIC
print('WELCOME TO CODEMISTIC')

Multiline Comment

To create a Multi Line Comments , the only problem with this style is we will have prefix each line with # , as shown below: For example:

#This is a long comment
#and it extends
#to multiple lines

If we want to simplify our efforts for writing Multi Line Comments , then we can wrap these comments inside triple quotes ( double or single ) as shown below

"""This is also a
perfect example of
multi-line comments"""

Triple quotes doesn’t create “true” comments. They are regular multiline strings which are not getting assigned to any variable. Therefore they will get garbage collected as soon as the code runs. Hence they are not ignored by the interpreter.


Docstring in Python

A docstring is short form for documentation string.

Python docstrings are called the literals that appear right after the definition of a function, method, class, or module.

While writing docstrings triple quotes are used.
For example:

def double(num):
    """Function to double the value"""
    return 2*num

Docstrings appear right after the definition of a function, class, or a module.

The docstrings are associated with the object as their __doc__ attribute.

So, we can access the docstrings of the above function with the following lines of code:

def double(num):
    """Function to double the value"""
    return 2*num
print(double.__doc__)

Output

Function to double the value