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.
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