
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why is indentation important in Python?
Indentation indicates the spaces or tabs placed at the beginning of a line of code to indicate the block structure. In many programming languages, indentation is used to improve code readability.
In Python, indentation is the key part of the syntax. It is used to define the blocks of code, such as loops, conditionals, and functions. If indentation is not used properly in Python, it results in the IndentationError, causing the program to fail.
Using without Indentation
In this scenario, we are going to use the If-Else statement in the Python program without proper indentation and observe the output.
a = 5 if a > 2: print("a is greater than 2") else: print("a is less than or equal to 2")
The output of the above program is as follows -
IndentationError: expected an indented block after 'if' statement
Using with Proper Indentation
In this case, we are using the same If-Else statement with proper indentation. Where the program expects the print statement to be indented under the if and else blocks, indicating the code that belongs to each condition.
a = 5 if a > 2: print("a is greater than 2") else: print("a is less than or equal to 2")
The output of the above program is as follows -
a is greater than 2