Python Flow Control:
Mastering the Flow of Your
Code
This presentation will guide you through the essential concepts of Python
control flow, including if-else statements, loops, and conditional execution.
by Reyan Ahmed
A Quick Review of Last
Week
In the previous session, you
were introduced to
programming language and its
generation, basic syntax in
python, and program writing.
Today, additional language
constructs and concepts that
can help you develop a program
in Python are introduced. We
will discuss, Python for program
flow controls, conditionals, and
loop statements.
Phase 1: Foundations of
Python (Weeks 1–3)
• Goal: Understand Python basics and fundamental
programming concepts.
• Topics:
• Introduction to Python: Syntax, variables, and data types.
• Control flow: if/else statements, loops (for, while).
• Functions: Defining, calling, and using parameters.
• Milestone:
Complete small programs that involve basic concepts.
• In the previous session, you were introduced to
programming language and its generation, basic
syntax in python, and program writing. Today,
additional language constructs and concepts
that can help you develop a program in Python
are introduced. We will discuss, Python for
program flow controls, conditionals, and loop
statements.
Program Flow Controls and Syntax
in Python
• A program flow control is the order in
which the program’s code executes. In
Python, the flow control of the program is
regulated by the implementation of three
types of programming language constructs
or program logic, namely sequential,
branching, and loop.
• The sequential flow control consists of a simple
list of statements that are to be executed in the
order given. Everything you learnt about
declaring variables, printing output, and reading
input from the keyboard in the previous session
are sequential statements. In this session, the
implementation of the remaining two language
constructs: conditional and loop flow controls
are focused.
Conditionals Program Flow
Controls
• In some cases, you may need a program to choose one out of
two or more alternatives, depending on the input given. For
example, suppose you design a program that determines
whether the given number is even or odd. An even number is
a number divisible by two without a remainder, whereas an
odd number is divisible by two with a reminder. We can
describe the problem with the following mathematical
equation which is called expression in python.
• num % 2 = 0 , num is Even, for num any integer number.
• num % 2 ≠ 0 , num is Odd, for num any integer number
• A given integer number is either even or
odd (i.e. two possible options), but this can
be decided based on the remainder after
dividing the number by two. Python
provides conditional or branching
statement to implement such solutions.
Before starting conditional statements, it is
important to learn conditional expressions
in Python.
CONDITIONAL EXPRESSIONS
• Conditional expressions are statements that
use Boolean operators (such as AND, OR, NOT)
or comparison operators (such as >,<. ≠). Like
in mathematics, comparisons of two values in
Python are described with comparison signs
used in mathematics. The Table below
presents comparison expressions in Python
and Mathematics.
Conditional Expression
Description Comparisons in Comparisons in
Python Mathematics
Equals a == b a=b
Not Equals or inequality a != b a≠b
Less than a<b a<b
Greater than a>b a>b
Less than or equal to a <= b a≤b
Greater than or equal to a >= b a≥b
• The comparisons in the table are also called
conditional expressions that correspond to the
Boolean values - true or false. For example,
3==4 results in false (as 3 is not equal to 4).
Note that operators used in python’s
expressions are slightly different from operators
used in Mathematics (e.g. == vs. =, != vs. ≠ ).
Python conditionals can be used in several
ways, most commonly in ‘if statements’ and
loop statements.
Conditional or branching
statements
• Conditionals are statements in a program in which the
program decides at runtime whether some parts of the
code should or should not be executed. From the
earlier even/odd example, a given integer number can
be even or odd, but this can be decided based on the
remainder after dividing the number by two.
• Therefore, first: Decide whether (num % 2 == 0) is or is
not true. If it is true, then the number is even. If it is
not, then the number is odd.
• The evaluation of the conditions
• (i.e., num%2==0) is a test that can be checked to see
if it is true or false; as a result, executing one thing
when the condition is true, or something else when
the condition is false.
• In Python, a conditional statement is written using
the if keyword. Like many other programming
languages, Python provides various versions of
branching statements that can be implemented using
the if clause: simple if statement, if..else statement,
and if..elif…else statement.
IF STATEMENT
• If statement : this is the simplest implementation
of a conditional statement that decides only one
part of the program to be executed if the condition
is satisfied or ignored if the condition fails. The
condition is an expression that is either true or
false. If the condition (expression) is true, then do
the indented statement(s). If the condition is not
true, then skip the indented statements.
IF…ELSE STATEMENT
• If…else statement : the ‘if...Else statement’
provides two alternative statements or blocks:
one following the ‘if expression’ and another
following the ‘else clause’. ‘If…else’ allows us
to specify two options: one which is executed
if a condition is true (satisfied), and another
which is executed if the condition is false (not
satisfied)
• Syntax in a python program is a set of rules that
defines how a python program is written and
interpreted. For instance, the expression of an
‘if statement’ must be followed by a colon (:).
Some programming languages require
expressions to be enclosed in parenthesis(), but
in python it is optional. So in python, “if num %
2 == 0:” or “if (num % 2 == 0):”, this complies to
Python’s syntax rule.
Indentation
• Another important syntax in python is
indentation. Indentation refers to the spaces at
the beginning of a code line. While in other
programming languages the indentation in code
is only for readability, the indentation in Python
is very important. Python uses indentation to
indicate a block of code.
• For example, if you ignore the indentation,
the code generates an error. Of course, the
number of spaces is up to you as a
programmer, but it has to be at least one.
• Use the same number of spaces in the same
block of code; otherwise, Python gives you an
error (See Figure 6.5, the last if statement).
These are important concepts when you write
control statements.
if...elif…else statement
• if...elif…else statement: Assume a problem that
could have many options to choose from or
require several conditions. For example, you
want to develop a program that will print
‘Excellent, Very Good, Good, Satisfactory, or
Fail’ based on the student mark. In such
situations, Python allows you to add any
number of alternatives using an elif clause after
an if and before an else clause.
• The elif is a keyword in Python to say “if the
previous condition(s) are not true, then try this
condition”. The else keyword catches anything that
is not caught by all the preceding conditions.
• Each expression is evaluated one after the other,
and if the expression is found to be true, all
statements in that specific block are executed.
Otherwise, if none of the expressions before the last
‘else’ statements are true, the statements under
‘else’ are executed.
Loops or Iteration Program Flow
Controls
• Most of real-world problems include some
action that is repeated several number of times.
For example, consider a program that
determines the student’s mark as “Excellent”,
“Very Good”, etc. If you have 20 students in a
class, then a more complete program would
repeat this status determination 20 times (i.e.
once for each student in the class).
• A program is often used to automate such
repetitive tasks. A portion of program code that
repeats a statement or group of statements in
programming is called loop.
• Loops are set of statements that run repeatedly
for a fixed number of times, or until a condition
is satisfied. Loop statements control a block of
code to be executed iteratively or until a certain
condition fails.
• Loops are a useful and frequently used
feature in all modern programming
languages.
• Python provides several language features
to make iteration/looping easier. There are
two types of loops that are built into
Python: for loops and while loops.
FOR LOOPS
• For loops: the for loop in python is used to iterate over a
sequence. For loop in combination with the python’s
range() function is used for counting in all kinds of ways
(see for loop with range function at the end of this section).
The for loop in python differs a bit from other like C or
pascal. In python for loop is used to iterate over the items
of any sequence including the python list, string, tuple,
dictionary, etc. It is also used to access elements from a
container (e.g. List, string, tuple) using a built-in function
range(). The general syntax of for loop is as follows.
• for loop with range() function: The range()
function returns a list of consecutive integers. The
sequence of numbers starts from 0 by default,
and counts by incrementing 1(by default), and
ends at a specified number. It is widely used count
controlled loops.
• range(x): generates a sequence of numbers from
0 to x, excluding x, incrementing by 1
• range(x, y): This generates a sequence of numbers
from x to y excluding y, incrementing by 1
• range(x, y, z): This generates a sequence of
numbers from x to y excluding y,
incrementing by z. This is different from the
above range function in that the increment
is set by the z value.
• for loop in iterable object: Unlike the
previous ones, the loop iterates while
something is true. This type of loop is
called a condition controlled loop.
• Syntax: for variable_name in string for loop can iterate
through string. The string is an iterable object in python
because it contains a sequence of characters. Thus,
applying for loop in a string allows us to access the
content character by character.
• break Statement with for: The term break is a keyword in
python. With the break statement, you can stop the loop
before it has looped through all the items:
• continue keyword with for loop: The term continue is a
keyword in python. With the continue statement, you can
stop the current iteration of the loop, and it continues
with the next.
• while Statement: The while statement in Python
supports repeated execution of a
• statement or block of statements that is controlled by
a conditional expression. The
• general syntax for the while statement is:
Syntax:
• while expression:
• Statement_1
• Statement_2
• […]
• Note that in python expression must end by colon(:).
Statements under the while must be indented.
• The while loop runs as long as the condition
(expression) evaluates to True and executes the
program block (statement_1, statement_2 …). The
expression is checked every time at the beginning of
the loop and the first time when the expression
evaluates to False, the loop stops without executing
any remaining statement(s).
• Don’t forget to increment count(i.e. count +=1);
otherwise, the loop continues forever.
• break and continue keywords with while
loop: With the break statement, you can
stop the loop even if the while condition is
true. It causes the loop to quit even before
reaching the last iteration.
• As you have learned in for loop above, the
continue statement, causes the current
iteration to stop, and continues with the
next.
Actionable Tips for Progress
• Schedule Study Time: Dedicate at least 1 hours daily.
• Practice Regularly: Complete small challenges daily
(e.g., Codewars).
• Join Communities: Participate in Python forums like
Reddit, Stack Overflow, or Telegram groups.
• Seek Feedback: Share code with peers or mentors for
review.
• Reflect and Revise: Revisit previous exercises to
strengthen weak areas.
Q&A