Indentation and blocks in Python:

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.

Blocks in Python

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:

  • A header line ending with a colon (:)
  • One or more indented statements inside the block

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.

Some more Rules related to Python Statements

  • In many programming languages, each statement must end with a special character (often a semicolon). However, in Python, a line break serves this purpose. Additionally, Python comments always begin with the # symbol, and anything following it is ignored by the interpreter.
  • Statements are grouped under specific header lines (such as if, elif, else, while, and def), which always end with a colon (:).
  • Blocks are defined by indentation: all lines within a block must have the same indentation level (typically using the same number of spaces).
  • Any consistent number of spaces can be used for indentation, but most Python programmers prefer multiples of four spaces.
  • It is possible to use tabs for indentation, but mixing spaces and tabs within the same block should be avoided. Even though visually they may appear the same, Python treats spaces and tabs as distinct characters, potentially leading to difficult-to-debug errors.
  • To prevent such issues, many developers prefer spaces over tabs. If you are using a smart text editor, it is advisable to enable the option to replace tabs with spaces.
  • Except for indentation at the start of a line, spaces inside statements and expressions are generally ignored (unless they are part of a string). Similarly, comments—marked with #—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

Leave a Reply