Skip to main content

Python Indentation

What is Indentation in Python

Python indentation uses to define the block of the code. The other programming languages such as C, C++, and Java use curly braces {}, whereas Python uses an indentation. Its use of indentation highlight the blocks of code.

A code block (body of a function, loop, etc.) starts with indentation and ends with the first unindented line. The amount of indentation is up to you, but it must be consistent throughout that block. You can use Whitespace for indentation in Python.

The enforcement of indentation in Python makes the code look neat and clean, Which results in Python programs that look similar and consistent. 

Indentation can be ignored in line continuation, but it's always a good idea to indent. It makes the code more readable.

Use of Indentation in python

if True:
    print('ProgramBuzz')
    a = 5

If the indentation is not used properly, then that will result in Indentation Error.

Important Points about Indentation

1. The number of spaces is up to you as a programmer, but it has to be at least one. This is correct

if True:
 print('ProgramBuzz')

2. You have to use the same number of spaces in the same block of code, otherwise Python will give you an error. This is incorrect

if True:
  print('ProgramBuzz')
    a = 5

3. Python will give you an error if you skip the indentation. This is incorrect

if True:
print('ProgramBuzz')
Tags
Submitted by devanshi.srivastava on June 4, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.