Examples of Statement, Indentation and Comments in python Programming

0

Python Statement

Instructions that a Python interpreter can execute are called statements. For example :

a = 1 is an assignment statement. if statement, for statement, while statement, etc.

 

All Contributions

Docstrings in Python :

A docstring is short for documentation string

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

Triple quotes are used while writing docstrings

Example :

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

The docstrings are associated with the object as their __doc__ attribute

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__)

Python Comments : 

we use the hash (#) symbol to start writing a comment

It extends up to the newline character 

Example :

#This is a comment
#print out Hello
print('Hello')

Multi-line comments :

use the hash(#) symbol at the beginning of each line.

Example :

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

 

Python Indentation :

Most of the programming languages like C, C++, and Java use braces { } to define a block of code. Python uses indentation.

A code block (body of a function, loop,) starts with indentation and ends with the first unindented line.

example :

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

 

Multi-line statement :

In Python, the end of a statement is marked by a newline character. But we can make a statement extend over multiple lines with the line continuation character (\)

For example :

a = 1 + 2 + 3 + \
    4 + 5 + 6 + \
    7 + 8 + 9

line continuation is implied inside parentheses ( ), brackets [ ], and braces { }. For instance, we can implement the above multi-line statement as:

a = (1 + 2 + 3 +
    4 + 5 + 6 +
    7 + 8 + 9)

the surrounding parentheses ( ) do the line continuation implicitly. Same is the case with [ ] and { }. For example:

colors = ['red',
          'blue',
          'green']

total contributions (4)

New to examplegeek.com?

Join us