Indentation in Python isn’t just a style choice, it’s how the language itself knows where one block of code ends and another begins.
a = 150
if (a > 100):
print("a is greater then 100")
In the above, program, you must have noticed indention before print statement when it is written after the header line of if statement which always ends with colon (:). If you do not insert an indention, you will face an Indentation Error. If your editor does not automatically indent, you should now manually insert an indentation (press the Tab key or type four spaces) before entering the next line. Although program will execute with only one space, but standard is one tab which is equal to four spaces.
In other languages, blocks of code are often enclosed within specific markers (such as begin and end in some languages, or {} in C++ and Java). While these brackets allow statements to be written sequentially without indentation, they can make code harder to read. Many programmers working with these languages still prefer to use indentation for clarity.
Python enforces indentation instead of using explicit block markers. This eliminates the need for additional symbols while ensuring that code remains structured and readable. As a result, Python naturally encourages good coding practices.
This design choice traces directly back to Python’s origins. When Guido van Rossum began writing Python in December 1989, he carried the idea over from ABC, an experimental teaching language he had worked on years earlier at the CWI research institute in Amsterdam, which already used indentation instead of brackets to group statements. Van Rossum has explained the reasoning himself: because there are no opening and closing brackets that need to be kept in sync, there can never be a disagreement between how the parser groups a block of code and how a human reader perceives that same block just by looking at it. In languages that rely on indentation for readability and brackets for the compiler at the same time, the two can quietly drift apart – a block that looks indented one way but is bracketed another, misleading whoever reads it even though the code still runs exactly as written. Python closes that gap entirely by making the indentation itself the only thing that matters, so what the code looks like and what it actually does can never come apart.
The structure used with the if statement in the previous examples is a block of code.
Soon, you will encounter other types of blocks. In Python, compound statements follow a specific structure:
:)Structure:
Header line:
First statement of the block
...
...
Last statement of the block
If there are multiple statements indented under the same condition, they must be at the same level (for example, using 4 spaces for indentation). These statements after header line is what we call a block of statements.
A block of statements is a series of instructions that form a logical unit, which is executed only under specific conditions defined under the same condition.
In the previous example, the statements indented under the if statement form a single logical block. These statements execute together if the condition in the if statement is true—in this case, if the remainder of the division by 2 is zero.
# symbol, and anything following it is ignored by the interpreter.if, elif, else, while, and def), which always end with a colon (:).#—extend to the end of the line and are disregarded by Python.In the next article, we will analyze if-else statement in Python.
No Comments