Indentation
Indentation
• Indentation:
• Indentation refers to the spaces at the beginning of a code line.
• Where in other programming languages the indentation in code
is for readability only, the indentation in Python is very
important.
• Python uses indentation to indicate a block of code.
• Python indentation is a way of telling a Python interpreter that
the group of statements belongs to a particular block of code.
• A block is a combination of all these statements.
• Block can be regarded as the grouping of statements for a
specific purpose
Indentation
• Indentation:
Logging on to google...
All set !
Indentation
The lines print(‘Logging on to google…’) and print(‘retype the URL.’) are two separate
code blocks.
The two blocks of code in our example if-statement are both indented four spaces.
The final print(‘All set!’) is not indented, and so it does not belong to the else-block.
Output:
Logging on to google...
All set !
Indentation
• Indentation : Example 2
j=1
while(j<= 5):
print(j)
j=j+1
• To indicate a block of code in Python, you must indent each line
of the block by the same whitespace.
• The two lines of code in the while loop are both indented four
spaces.
• It is required for indicating what block of code a statement
belongs to.
Indentation
Output:
1
2
3
4
5