Python Statement, Indentation, Comments.

Statements in Python

In Python, any executable instruction, tells the computer to perform a specific action is known as Statement.

Python gives no braces to demonstrate a block of code for class and function definitions or flow control. Block of code is indicated by line indentation, which is inflexibly authorized.

The quantity of spaces in the indentation is variable, however, all statements inside the block should be indented a similar amount. For instance:

if (4 % 2 == 0):

Multi-line statements

In Python, the end of a executable statement is marked by a newline character. But  if required you can extend a statements over multiple lines explicitly with the line continuation character (\). For instance:

message = ("Hello\n"
           "Welcome To\n"

In Python we can define line continuation inside parenthesis (), {} and [] which is known as Explicit Line Continuation.

Indentation in Python

A large portion of the programming languages like C, C++, and Java use { } to characterize the block of code. Python, however, uses indentation. Generally four white spaces are used for indentation and is preferred over tab.

A code block (body of a function, loop, and so on.) begins with space and finishes with the first unindented line. The amount of indentation is up to you, however, it must be steady all through the program. Statements with the same indentation belongs to the same group. The use of indentation makes the code look clean and consistent.

Let us consider some examples and see how indentation works in Python.

a = 5
b = 10
if(a == 5):
    print("a =",a)
    if(b == 10):
        print("b =",b)

In the above example, we can see that first three lines are following the same indentation. However, after the “if” statement interpreter makes an indentation of four white spaces and then it will execute the statement i.e. it will print the value of ‘a’.

Indentation can be overlooked in line continuation, however, it’s consistently a smart thought to indent. It makes the code easy to understand. For instance:

if True:
    a = 10


if True: print('Welcome'); a = 10

both are valid and do something very similar, however, the former style is more clear.

Incorrect spacing will result in IndentationError. Let’s have a look of indentation error program:

In the above example, you will get indentation error because print(“World”) does not match any outer indentation level.

Python Comments

Comments are significant while composing a program.In Python, comments are a set  of statements that are ignored by the Python Interpreter i.e. they are non executable statements. We use comments because it makes it easy for us to understand the source code.

You may overlook the key details of the program you just wrote in a month’s time. So setting aside the effort to clarify these ideas as the comment is consistently productive.

In Python, we use the hash (#) symbol to begin composing a comment.

It stretches out up to the newline character.

#this is a basic example
#of how comments work 
if True:
     a = 5

Another method of doing this is to use triple quotes, either ”’ or “””.

These triple quotes are commonly utilized for multi-line strings. Multi-line comment are used when we need to comment on many lines.

Except if they are not docstrings, they don’t produce any additional code.

Docstring is short for  Documentation String. It is a string that occurs as a first statement in a class, function, methods and so on. Triple quotes(“”” “””) are used for writing docstrings.

Example of a multi-line comment:

"""This is an 
example of
multiline comments"""

Example of a docstring using triple quotes: