Skip to content

Indentation in Python (With Examples)

Indentation in Python

Have you ever wondered why all the statements in Python code (program) often do not have the same vertical alignment?

It’s only because of indentation in Python.

You can observe it through the following code snippet.

Indentation in Python

And,

The importance of indentation in Python is much more than that in other programming languages.

Let’s see. How?

In this article, you’ll learn:

1. What is indentation in Python?

2. How does Python indentation work?

3. How to indent your Python code?

4. What is the role of indentation in Python? (Why is indentation used?)

5. Why is Python indentation important?

(Including examples)

So, Let’s dive right in!

What is Indentation in Python?

Before diving into the actual definition or meaning of Indentation in Python,

I want to ask you a question.

So, Are you Ready?

Here we go!

The question is: Do you know what is the meaning of indentation in English dictionary?

Now,

You might be wondering that, why I am asking this question here. Right?

I am asking only because, the meaning of indentation in English dictionary and in Python is almost similar.

So,

If you know one definition, then it’s going to be much easier to understand the meaning of other.

In English dictionary: Indentation simply means, adding some blank spaces between the beginning of the first character of a written text in a line and the margin.

Similarly,

In Python: Indentation refers to the spaces that are added at the beginning of a statement in our code.

And, as we already know, each and every line of code is called a statement.

Now,

One more question arises here,

How much spaces you can use for indenting a statement or a block of code?

Ans: You can use any number of spaces for indentation, It is up to you, but it has to be at least one.

By default, Python uses four spaces for indentation.

And, for four (4) spaces, you can use a tab key of your keyboard.

All statements with the same indentation [having same distance (space) to the right] belong to the same block of code.

In simple words, statements belonging to a block of code must start from the same vertical alignment.

And,

If a block has to be more deeply nested from the preceding one, it is simply indented further to the right.

Whenever you provide different number of spaces for the statements within a same block of code, you will get an error message by the python interpreter.


How does Python indentation work?

When you write a python program, most of the time, you have to define a group of statements (block of code) for conditional statements, functions and loops.

And, due to conditional statements, functions and loops, program gets bigger and more complicated to read and understand.

As well as, It is hard for the python interpreter to figure out the order in which the execution of statements takes place.

And,

Here, Indentation comes into picture.

So, to make it easier, we divide the code into several blocks of code and indent it.

This indentation helps the Python interpreter to understand the following:

  • The order in which each statement or block of code should be executed.
  • Which statement belongs to which block of code.

Hence, Python indentaion is a way of telling the Python interpreter that a group of statements belongs to a particular block of code.


How to indent your Python code?

Indenting the python code is as simple as adding some whitespaces (blank spaces) before the beginning of that statement which you want to indent.

But,

Don’t worry!

You do not need to bother about indenting a statement or a block of code in Python.

Because, Most of the Python IDEs or code editor will automatically indent your Python code.

So, it’s an easy task to write a properly indented code in Python.

Let’s understand with the help of an example:

age = int(input("enter your age: "))    # statement 1
if age >= 18:       # statement 2
    print("You are eligible to vote")      # statement 3
else:       # statement 4
    print("Not eligible to vote")      # statement 5

In the above code,

We are writing a python program to take input (in the form of a number) from the user using a built-in input() function and converting that number into integer format using an another built-in int() function.

After that, we are checking the number entered by the user is greater than or equal to eighteen (18) or not using if-else statements.

And, at last, display the eligibility of the user using print() function.

How the above code will be executed?

Statement 1 gets executed first, then it gets to statement 2.

Now only if statement 2 is correct, we would want statement 3 to get printed.

And, as we already know, writing an if statement always requires body (code block) with some indentation so that all the group of statements inside the if block belongs to a separate block of code from the remaining statements or the block of codes and also executed if the given condition is true. Hence Statement 3 is indented with 4 spaces.

Also, statement 5 should be printed, if statement 2 is false and hence this is also indented with 4 spaces.

The 1st, 2nd and the 4th statement are main or we can say parent statement because there is no (zero) indentation in front of them.


What is the role of indentation in Python? (Why is indentation used?)

Basically,

Indentation in Python is used to specify a block of code (a block of code may contain a single statement).

Or,

In other words, Indentation in Python is used to separate a statement or a block of code from the remaining blocks of code.


Why is Python Indentation important?

Indentation is so important in Python due to the following:

  • 1. For indicating what code belongs to what part of the program.

or,

In simple words, It indicates which statement belongs to which block of code.

  • 2. The order in which each statement or block of code should be executed.
  • 3. Without proper indenting the python code, Python interpreter will return an IndentationError.

Conclusion:

In this article, you learned what are Indentation in Python, you also became familiar with How Python Indentation works, How to indent your python code and many more with the help of examples.

So,

I really hope that you liked my article and got a lot of value from it.

Now, It’s your turn!

If you have any question, feel free to ask in the comment section below right now.


Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *