0% found this document useful (0 votes)
7 views

Indentation

Uploaded by

supriya sundaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Indentation

Uploaded by

supriya sundaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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:

• Python uses indentation to highlight the blocks of code.


Whitespace is used for indentation in Python.
• All statements with the same distance to the right belong to the
same block of code.
• If a block has to be more deeply nested, it is simply indented
further to the right
Indentation
• Indentation : Example
# Python program showing
# indentation
site = 'google'
if site == 'google’:
print('Logging on to google...')
else:
print('retype the URL.')
print('All set !')
Output:

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

You might also like