Python language basics 3: significant white space

Introduction

In the previous post on Python basics we looked at how to import external libraries into your Python file using the import keyword.

This post will quickly introduce an interesting feature of Python: the significant whitespace. This is meant to simply prepare you for the fact that you can forget code block delimiters like ‘{‘ and ‘}’ where code blocks are used, such as methods or loops.

Whitespace in Python

We saw in the previous posts of this series that one distinguishing feature of Python among other popular object-oriented languages is the lack of a statement delimiter like ‘;’. Another one feature is significant whitespace in code blocks. Indentation is used to indicate bits of code that “belong together”.

As a first example let’s see how construct a simple for loop:

for i in range(10):
    print(i + 2)
    print(i * 2)
    print(i - 2)

This will execute all print statements within the for loop:

2
0
-2
3
2
-1
4
4
0
5
6
1
6
8
2
7
10
3
8
12
4
9
14
5
10
16
6
11
18
7

If, however, we modify the indentation…:

for i in range(10):
    print(i + 2)
    
print(i * 2)
print(i - 2)

…then only the first print statement will be executed in the loop. The last two will get the last value of ‘i’ after the loop completion, i.e. 9:

2
3
4
5
6
7
8
9
10
11
18
7

…where 18 and 7 are the output of the last two print statements.

The colon ‘:’ indicates the start of a new block. Here comes a nested indentation example:

for i in range(10):
    print(i + 2)
    if i == 3:
        print(i * 2)
        print(i - 2)
    elif i == 5:
        print(i ^ 2)
    print("Current value of i: {}".format(i))

Removing the indentation will also delimit the code block. You can probably see how the print statements are grouped. The first and last print statements will be executed in every iteration as they belong to the first level of indentation. The other three are conditionally executed based on the if statements:

2
Current value of i: 0
3
Current value of i: 1
4
Current value of i: 2
5
6
1
Current value of i: 3
6
Current value of i: 4
7
7
Current value of i: 5
8
Current value of i: 6
9
Current value of i: 7
10
Current value of i: 8
11
Current value of i: 9

A standard rule in the Python world is to use an indentation of 4 spaces which gives the same length of whitespace as a tab.

In the next post we’ll look into Python’s integer data type.

Read all Python-related posts on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: