Python Control Flow Tools

Greetings! Some links on this site are affiliate links. That means that, if you choose to make a purchase, The Click Reader may earn a small commission at no extra cost to you. We greatly appreciate your support!

Python control flow tools change the flow of how code is executed by the Python interpreter.

Since the Python interpreter executes code in a line-by-line manner, Python control flow tools help dictate what line(s) of code should run in a Python program. There are different types of control flow tools available to us in Python and we will go through them in detail in this lesson.

Python Control Flow Tools

There are different kinds of Python control flow tools and we will cover them all in this lesson.


1. Python if statement

The Python if statement is a conditional statement responsible for executing programming blocks based on a specified condition. It is one of the most used Python control flow tools.

The syntax for writing an if statement in Python is as follows:

if condition:
    statement(s) 

Here is an example of a Python if statement where the condition is evaluated as True:

# An if statement where the condition is True
if 10 > 4:
    print("The if statement is executed.")
OUTPUT:
The if statement is executed.

In the above example, the Python interpreter first evaluates 10 > 4 and finds it to be True. Therefore, it executes the statements inside the if statement block.

Here is an example of a Python if statement where the condition is evaluated as False:

# An if statement where the condition is False
if 10 < 4:
    print("The if statement is executed.")
NO OUTPUT

In the above example, the Python interpreter first evaluates 10 < 4 and finds it to be False. Therefore, it skips the execution of the if block.


2. Python if-else statement

The Python if-else statement contains an added instruction in the Python if statement dictating what code should be executed if the condition is evaluated as False.

The syntax for writing an if-else statement in Python is as follows:

if condition:
    statement(s)
else:
    statement(s)

Here is an example of a Python if-else statement where the if condition is evaluated as True:

# An if-else statement where the condition is True
if 10 > 4:
    print("The if statement is executed.")
else:
    print("The else statement is executed.")
OUTPUT:
The if statement is executed.

In the above example, the Python interpreter first evaluates 10 > 4 and finds it to be True. Therefore, it executes the statements inside the if statement block and ignores the statements inside the else block.

Here is an example of a Python if-else statement where the if condition is evaluated as False:

# An if-else statement where the condition is False
if 10 < 4:
    print("The if statement is executed.")
else:
    print("The else statement is executed.")
OUTPUT:
The else statement is executed.

In the above example, the Python interpreter first evaluates 10 < 4 and finds it to be False. Therefore, it executes the statements inside the else statement block and ignores the statements inside the if block.


3. Python if-elif-else statement

The Python if-elif-else statement contains added if conditions in an if-else statement.

The syntax for writing an if-elif-else statement in Python is as follows:

if condition_1:
    statement(s)
elif condition_2:
    statement(s)
elif condition_n:
    statement(s)
else:
    statement(s)

Here is an example of a Python if-elif-else statement where the if statement is executed:

# An if-elif-else statement
if 10 > 4:
    print("The if statement is executed.")
elif 10 == 4:
    print("The elif statement is executed.")
else:
    print("The else statement is executed.")
OUTPUT:
The if statement is executed.

In the above example, the Python interpreter first evaluates 10 > 4 and finds it to be True. Therefore, it executes the statements inside the if block and ignores the statements inside the elif and else blocks.

Here is an example of a Python if-elif-else statement where the elif statement is executed:

# An if-elif-else statement
if 10 < 4:
    print("The if statement is executed.")
elif 10 == 10:
    print("The elif statement is executed.")
else:
    print("The else statement is executed.")
OUTPUT:
The elif statement is executed.

In the above example, the Python interpreter first evaluates 10 < 4 and finds it to be False. Then, it evaluates 10 == 10 and finds it to be True. Therefore, it executes the elif block and skips the execution of the if and else blocks.

Here is an example of a Python if-elif-else statement where the else statement is executed:

# An if-elif-else statement
if 10 < 4:
    print("The if statement is executed.")
elif 10 == 4:
    print("The elif statement is executed.")
else:
    print("The else statement is executed.")
OUTPUT:
The else statement is executed.

In the above example, the Python interpreter first evaluates 10 < 4 and finds it to be False. Then, it evaluates 10 == 4 and finds it to be False as well. Therefore, it executes the else block and skips the execution of the if and else blocks.

Here is an example of a Python if-elif-else statement where the second elif statement is executed:

# An if-elif-else statement
if 10 < 4:
    print("The if statement is executed.")
elif 10 == 4:
    print("The first elif statement is executed.")
elif 10 == 10:
    print("The second elif statement is executed.")
else:
    print("The else statement is executed.")
OUTPUT:
The second elif statement is executed.

In the above example, the Python interpreter first evaluates 10 < 4 and finds it to be False. Then, it evaluates 10 == 4 and finds it to be False. Finally, it evaluates 10 == 10 and finds it to be True. Therefore, it executes the second elif block and skips the execution of the if, first elif and else blocks.


4. Python For Loop statement

The Python for loop statement is a Python control flow tool used to iterate over the items of any sequence (a list or a string) in the order that they appear in the sequence. The syntax of for loop in Python is slightly different from what you may have used in other programming languages, such as C or C++.

The syntax for writing a for loop in Python is as follows:

for variable in iterable_sequence:
    statement(s)

Here is an example of a Python for loop that iterates over a given list of numbers and prints them out:

# Program that iterates over a given list of numbers

# Defining a list of integers
list_of_numbers = [10, 15, 20]

# for statement
for number in list_of_numbers:
    print(number)
OUTPUT:
10
15
20

You can also nest for loops inside each other in the following way:

# Example of nested for loops

# Declaring two lists
outer_list = [1, 2]
inner_list = ['a', 'b']

# Outer for-loop
for x in outer_list:
    # Inner for-loop
    for y in inner_list:
        print(x)
        print(y)
OUTPUT:
1
a
1
b
2
a
2
b

In the above example, the inner for loop is iterated inside of the outer for loop.


5. Python While Loop statement

The Python while loop is an iterator used to execute a block of statements until the specified condition is matched.

The syntax for writing a while loop in Python is as follows:

while condition:
    statement(s)

Here is an example of a while loop as a Python control flow tool:

# Declaring a variable
x = 10

while x < 35:  # run a loop while x is less than 35
    x = x + 5
    print(x)
OUTPUT:
15
20
25
30
35

Please be mindful that if the condition is always True, then, the while condition will not stop and your program will be stuck in an infinite loop.


6. Python Break and Continue statements

The Python break, and continue statements are also Python control flow tools that alter the flow of a normal loop.

The break statement breaks out of the innermost enclosing a for or while loop, whereas, the continue statement continues with the next iteration of the loop. In other words, the continue statement escapes the block of the statement below it and starts with the next iteration of the loop.

Here is an example of how the Python break and continue statements work as Python control flow tools:

# Defining a list of elements
actions = ["wake", "wash", "eat", "work", "play", "sleep"]

for action in actions:
    if action == "sleep":
        break          # if the action is sleep, this would terminate the loop completely
    elif action == "play":
        continue       # if the action is play, the loop continues to next iteration 
    else:
        print (action)
OUTPUT:
wake
wash
eat
work

7. Pass statement

The pass statement is one of those control flow tools in python that does nothing. It can be used when a statement is required syntactically, but the program requires no action.

Here is an example of a pass statement as a Python control flow tool:

# Program that shows the use of a pass function
x = 5
if x == 5:
   pass    # this does nothing
NO OUTPUT

This is all that we have for you on control flow tools in Python. Now, you can start altering the structure of how a Python program runs.

Leave a Comment