4. Structuring with Indentation

By Bernd Klein. Last modified: 02 Nov 2021.



A block is a group of statements in a program or script. Usually, it consists of at least one statement and declarations for the block, depending on the programming or scripting language. A language which allows grouping with blocks, is called a block structured language. Generally, blocks can contain blocks as well, so we get a nested block structure. A block in a script or program functions as a means to group statements to be treated as if they were one statement. In many cases, it also serves as a way to limit the lexical scope of variables and functions.

Initially, in simple languages like Basic and Fortran, there was no way of explicitly using block structures. Programmers had to rely on "go to" structures, nowadays frowned upon, because "Go to programs" turn easily into spaghetti code, i.e. tangled and inscrutable control structures.

Block structures were first formalized in ALGOL as a compound statement.

Programming languages, such as ALGOL, Pascal, and others, usually use certain methods to group statements into blocks:

A code snippet in Pascal to show this usage of blocks:

with ptoNode^ do
      x := 42;
      y := 'X';
if (x==42) {
    printf("The Answer to the Ultimate Question of Life, the Universe, and Everything\n");
} else {
    printf("Just a number!\n");

The indentations in this code fragment are not necessary. So the code could be written - offending common decency - as

 if (x==42) {printf("The  Answer to the Ultimate Question of Life, the Universe, and Everything\n");} else {printf("Just a number!\n");}

Please, keep this in mind to understand the advantages of Python!

Indenting Code


Python uses a different principle. Python programs get structured through indentation, i.e. code blocks are defined by their indentation. Okay that's what we expect from any program code, isn't it? Yes, but in the case of Python it's a language requirement, not a matter of style. This principle makes it easier to read and understand other people's Python code.

So, how does it work? All statements with the same distance to the right belong to the same block of code, i.e. the statements within a block line up vertically. The block ends at a line less indented or the end of the file. If a block has to be more deeply nested, it is simply indented further to the right.

Beginners are not supposed to understand the following example, because we haven't introduced most of the used structures, like conditional statements and loops. Please see the following chapters about loops and conditional statements for explanations. The program implements an algorithm to calculate Pythagorean triples. You will find an explanation of the Pythagorean numbers in our chapter on for loops.

from math import sqrt
n = input("Maximum Number? ")
n = int(n)+1
for a in range(1,n):
    for b in range(a,n):
        c_square = a**2 + b**2
        c = int(sqrt(c_square))
        if ((c_square - c**2) == 0):
            print(a, b, c)


3 4 5
5 12 13
6 8 10
8 15 17
9 12 15
12 16 20
15 20 25

There is another aspect of structuring in Python, which we haven't mentioned so far, which you can see in the example. Loops and Conditional statements end with a colon ":" - the same is true for functions and other structures introducing blocks. All in all, Python structures by colons and indentation.