Python Friday #6: Control Structures

A big part of programming is to control the flow of your program. With control structures like if/else and loops we can do exactly that.

This post is part of my journey to learn Python. You can find the other parts of this series here.

 

Whitespaces are important

You may be used to languages that need curly brackets { } to define scope in the code. For example, in C# and Java you can use any arbitrary number of whitespaces on indentation (the thing before your line of code starts) – the compiler ignores them and only cares about the { } to figure out what code belongs together and what should run in an if statement.

In Python those whitespaces matter! There are no { } to define scope and therefore your indentation is the only clue to figure out what you try to achieve. Since that indentation is so important, you should follow the Python style guide with its recommended 4 whitespaces per indentation level (see PEP-8).

The only exception should be lines that you break into two or more to align the parameters to a function (so called continuation lines). In this case you should align the parameters vertically:

On pep8.org you can find a much better and longer explanation on the different aspects of the Python style guide. For the rest of this post you just need to know that you need to type the 4 whitespaces and cannot ignore them.

 

if Statements

I use If statements a lot and Python has an easy to learn syntax. All you need to write is if, followed by a condition and a colon. Indent the code you want to run when the condition is true with 4 spaces:

As with many other programming languages, you can specify a branch that is executed when the condition is not true:

Sometimes a single if is not enough, and you have to check the else branch for another condition. Before you start nesting if blocks, you should use elif instead:

 

while loops

In a while loop a block of code is repeated as long as the condition is true:

 

for loops

For loops in Python work like foreach loops in C# and let you iterate over all elements in a collection:

 

Ranges

Ranges are a great help when you work with loops. To execute a block of code 5 times you can write this:

In the example above you declared 5 as the stopping point. The range function creates the sequence 0, 1, 2, 3, 4 (0-based index) and your loop executes 5 times.

You can set a starting point as well get the numbers 6 up (and including) 10 by writing it this way:

You can even set the step size to have more flexibility. To create a countdown from 10 to 1, you can write this snipped of code:

 

switch Statements

Python does not have a switch statement build into the language. Stack Overflow offers a few interesting solutions that build on a dictionary that may help you for those cases you need a switch statement.

 

99 Bottles of Beer on the Wall

Python Central has a great little code example that creates the 99 bottles of beer song with just a few lines of all the control structures I explained in this post (and the last one about strings):

 

Next

Functions pop up everywhere when you try to do something with Python. It is time to take a closer look.

2 thoughts on “Python Friday #6: Control Structures”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.