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.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: