Control Flows &
Functions in
Python
Making Python Decide, Repeat, and
Organize Code
01 NELSON NJIHIA
Class agenda
Conditional Statements
Loops
List Comprehensions
Functions
10
Conditional Statements
WHY ARE THEY IMPORTANT ?
Conditional statements help in the decision-making process
Here the Python decides what code to run and when.
Conditional statements check if something is true before
deciding what to do.
Types:
if → one decision
→
if…else two possible paths
→
if…elif…else multiple paths
06
The if Statement
This checks a single
condition - if it's true, the
code block runs.
This code checks if the value of temperature is
greater than 25.
Since temperature = 30, the condition
temperature > 25 is True, so it runs the code
inside the if block and prints:
It's a hot day!
04
if...else statement
This gives an alternative
when the condition is
false.
This code uses an if-else statement to decide between
two actions:
If temperature > 25, it prints "It's a hot day!"
Otherwise (if the condition is false), it prints "It's a
cool day!"
Since temperature = 20 (which is not greater than 25),
the condition is False, so it prints
04
“It's a cool day!”
if...elif...else statement
This checks multiple
conditions in order.
This code uses an if-elif-else chain to assign a grade
based on the score:
If score >= 90, grade is "A".
Else if score >= 80, grade is "B".
Else if score >= 70, grade is "C".
Otherwise, grade is "F".
Here, score = 85, so it matches the second condition
(score >= 80), setting grade = "B".
It then prints:
Your grade is B 04
LOOPS
In coding , loops are used to repeat a block of code.
We perform a process of iteration (repeating tasks).
we mainly have two types of loops
1. Indefinite iteration (while loop.), where the
number of times the loop is executed depends
on how many times a condition is met.
2. Definite iteration (for loop), where the number
of times the loop will be executed is defined in
advance (usually based on the collection size).
FOR LOOP
In a for loop, we will know in advance how
many times the loop will need to iterate
because we will be working on a collection
with a predefined length.
With for loops, on each iteration, we will be This code loops through the list fruits and
able to perform an action on each element of prints a message for each fruit.
On each loop, fruit takes one item from the
the collection. list ("apple", then "banana", then "cherry").
It prints:
I love apple!
I love banana!
I love cherry!
FOR LOOP: Using Range
A range is a series of values between two
numeric intervals.
We use Python's built-in function range() to
define a range of values.
For example,
five_steps = range(5)
# five_steps is now a collection with 5 This code uses a for loop with range(1, 6) to count from
1 up to 5.
elements: On each loop, number takes the next value in the range
# 0, 1, 2, 3, 4 and prints it.
It outputs:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
WHILE LOOP
Runs as long as the condition is
True.
Be careful: If the condition never
→
becomes False infinite loop!
This code uses a while loop that runs as long as count <=
5.
It starts with count = 1.
On each loop, it prints the current count and then
increases it by 1.
The loop stops when count becomes greater than
5.
It prints:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Loop controls: Break and
continue
The break statement is used to terminate
the loop immediately when it is
encountered.
The continue statement is used to skip the
current iteration of the loop and the
control flow of the program goes to the
next iteration.
23
Nested Loops
In Python, loops can be nested inside
other loops. Nested loops can be used
to access items of lists which are inside
other lists. The item selected from the
outer loop can be used as the list for
the inner loop to iterate over.
23
List Comprehension
EXAMPLE
Shorter, cleaner way to create
lists.
Syntax:
[expression for item in iterable if
This line creates a list of squares of
condition] numbers from 0 to 4 using list
expression: The value or transformation comprehension.
applied to each element. x**2 means “x squared,” and
item: A variable that represents each element range(5) generates numbers 0 to 4.
in the iterable.
iterable: A sequence (like a list, tuple, or Result:
range) to iterate over. numbers = [0, 1, 4, 9, 16]
condition: (Optional) A filter that determines
whether to include the element.
Functions
A function is a block of code designed to
SYNTAX
perform a specific task. You define a
function once and can use it multiple
times throughout your code.
So basically a function is a reusable
block of code
Types of Functions
1. Built-in Functions: Predefined functions in
Python (e.g., print(), len(), type()).
2. User-defined Functions: Functions created by
the user.
23
Key Components of a Function
1.
Function Name: Should be descriptive and follow
naming conventions.
2. Parameters: Variables passed into the function.
3. Docstring: An optional description of what the function
does.
4. Return Statement: Outputs a value from the function
(optional).
Benefits of Functions
1. Reusability: Write once, and use multiple
times.
2. Modularity: Break programs into smaller,
manageable parts.
3. Improved Readability: Descriptive names
make code easier to understand. 23
Q&A
17
Thank you!
17