Indentation in Python

Indentation

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 in python

Indentation : 

Indentation is a very important concept of Python because without proper indentation the Python code will end up with Indentation Error and the code will not get compiled. 

  • It simply means adding whitespace before statement.
  • 1 Tab space is considered as one indentation.
  • 1 Tab space is equal to 4 white space.
  • Generally , after every ‘:’ we use Indentation.

Example:

  • def add(): #one tab space is preferred as indention generally.

Example  1:

Run
#indendation 
p='PrepInsta'
if(p=='PrepInsta'):
    print('Hello Prepsters we are here to help you.')
Output:

Hello Prepsters we are here to help you.

Example 2:

Run
#without indentation
p='PrepInsta'
if(p=='PrepInsta'):
print('Hello Prepsters we are here to help you.')
Output:

  File "main.py", line 4

    print('Hello Prepsters we are here to help you.') 

        ^

IndentationError: expected an indented block

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription