Statements, Indentation and Comments

Python Statement

In Any programming Language, If the Interpreter is able to understand and execute a Instruction, that becomes an statement. for Example:

x= 1 is an statement in python because python interpreter can understand x is an integer whose value is 1.

Multiline statement

You can extend an statement over multiple lines by using line Continuation character(\). For example

x = 1+2+3+\
  4+5+6\
  +7+8

above statement extended over three lines. This can be one way of writing multiline statement. But, in python we generally use to write statement extended over multiple line by using parentheses ( ), brackets [ ] and braces { }. For example

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

Here, enclosing parentheses implies line continuation, similarly we can use brackets [ ] and braces { }. Example

a = (1 + 2 + 3 +
4 + 5 + 6 +
7 + 8 + 9)
colors = ['red', 'blue', 'green']

We can also write ,multiple statements in a single line by using semicolon ; as a statement separator. For example

a =1 ; b =2 ; num = "one"

Python Comments

 In Python Comments are used for different purposes. like

  • For explaining python code
  • To make the code more readable
  • To prevent execution when testing.

In Python, # is used for writing comments, wherever # is used in python, Interpreter ignores the preceding lines. For Example

#This is a comment
print("Hello, World!")

Comments can be placed at the end of a line, Python will ignore the rest of the line. For Example

print("Hello, World!") #This is a comment

You can also place put # at the beginning of a code if you don’t want to execute that line. Example:

#print("Hello, World!")
print("Welcome to Tecnotab")
Multiline comments:

Python does not have Syntax for multiple line comments, You need to insert # for each line of comment.

Note: Since Python will ignore the string literals that are not assigned to a variable, You can use triple quote (“”” or ‘’’) to write comment over multiple line

For Example,

"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")

Docstrings in Python

Documentation Strings in python is basically referred as DocsString

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

Triple quotes are used while writing docstrings. For example:

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

you can view the docstring by using a predefined method in python  __doc__ . For Example

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

Output

Function to double the value

Indentation In Python

Indentation in Python is most important unlike other languages like C C++ and java.

In other Languages we use braces { } to write a block of code but not in Python. Python has Indentation.

Generally 4 white spaces are used for indentation, In other words, If you write a multiline code syntax and press enter on keyboard, 4 spaces will be automatically provided and that is called indentation. For Example

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

It is not necessarily 4 white spaces even one white space is enough but in order to make code look neat and clean, we give 4 white spaces. If we dont give even a single indentation, we will get IndentationError.

Leave a Reply