£199.99 £139.99 for one year of Unlimited learning. Offer ends on 14 November 2022 at 23:59 (UTC). T&Cs apply

Find out more
Control Flow in Python
Skip main navigation

Control Flow in Python

Program execution happens sequentially in Python - the Python interpreter reads a program just like you are reading this page: one line at a time, from left to right and top to bottom. The interpreter executes operations and functions in the order that it encounters them. This is called control flow. 
4.2
A conditional flow in Python means that the program has a branching structure that allows it to react differently depending on specified conditions. When writing code in Python, you define this branching structure with if, elif and else statements. Let’s look at how these statements work. Imagine you are travelling along a road and you come to an intersection with three other roads branching off. How do you decide which road to take? Perhaps you could ask yourself, how do you feel? If you feel hungry, you should go to the shop to buy some food. An if statement evaluates a condition using Boolean logic. It checks if the statement is True. Usually the statement involves comparing a variable against a value or another variable.
52.9
It might be equal, not equal, greater than or less than, for instance. If the statement is True, the program executes each indented line under the if statement in sequence. Consider the scenario again. If you don’t feel hungry, but you do feel sick, you should go to the doctor to find out what’s wrong. An elif, short for else if statement, is useful here. You can create as many elif statements as you want to cover every other condition. Each elif statement has its own set of actions to take when its condition is True. Back to our example. If you feel anything else, maybe tired or bored, you should probably go home.
97.1
An else statement acts as a catch-all option for any other condition that your code doesn’t address. It can be the default. Once you master, if, elif and else statements, you can do so much more with Python. Let’s get going.

Program execution happens sequentially in Python – the Python interpreter reads a program just like you are reading this page: one line at a time, from left to right and top to bottom. The interpreter executes operations and functions in the order that it encounters them. This is called control flow.

Without control flow expressions, a program is simply a list of statements that are sequentially executed. With control flow, you can execute certain code blocks conditionally and/or repeatedly and these basic building blocks can be combined to create sophisticated code.

Python uses if, elif (short for else if), and else keywords to create control flow structures in your program. Imagine all these keywords are traffic controllers telling the control flow where to go.

Watch the video to get an overview about the three control flow expressions in Python and to understand how they work.

Let’s look at each of each of these control flow expressions in more depth.

Control Flow Expressions in Python

1. if statement

The if statement is one of the most well-known control flow statement types. It checks a condition and executes one of two functions:

For example:

Let us say we want to know if the input value of a is positive or negative. We use the if statement and set two conditions: if a is greater than 0, print ‘Its positive’, and if a is less than 0, print ‘Its negative.’

  • If the set condition is True, the code in the following block is executed.
  • If the set condition is False, the code in the following block is ignored.

The code snippet highlights this code behaviour:

Code:

a = 20
if a>0:
... print("Its Positive")
...

 

Output:
Its Positive

 

Code:

 

if a<o:
... print("Its Negative")

 

Observer that print was not executed because condition didn’t evaluate to true.

 

2. elif and else

 

An if statement can be optionally followed by one or more elif blocks, and a catch-all else block if all of the conditions are false.

 

The code snippet highlights the code behaviour using elif and else code blocks:

 

Code:

 

def codeblock(x):
... if x<0:
... print("Its Negative")
... elif x==0:
... print("Equal to Zero")
... elif 0<x<5:
print("Positive but smaller than 5")
... else:
print("Positive and larger than or equal to 5")
...
codeblock(-10)

 

Output:
Its Negative

 

Code:

 

codeblock(5)

 

Output:
Positive and larger than or equal to 5

 

Code:

 

codeblock(0)

 

Output:
Equal to Zero

 

Code:

 

codeblock(4)

Output: Positive but smaller than 5

This article is from the free online

Data Analytics and Python Fundamentals

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education