Sidama Public Health Institute (SPHI)
Regional Data Management and Analytics Center
(RDMC)
Python Training
By Senbeto K. Hawassa, Sidama
(MSc in Information Technology January, 2025
Control Structures
in python
Contents
3
Control Structures
Python Conditional/Control Statements
Conditional statements
if Statement
if-else Statement
if-elif-else Statement
Loop
For loop
While loop
Python Control structure
break Statement
continue Statement
Control Structures in python
4
Control Structures are fundamental programming constructs that
dictate the flow of execution of a program.
They enable developers to control how and when certain blocks
of code are executed based on specific conditions.
Control structures are essential for creating dynamic and
responsive applications, allowing for decision-making,
repetition, and branching logic.
Control Structures:
Conditional statements
Iterative statements (loop)
Python Conditional statement
5
Conditional statements in Python are used to execute certain
blocks of code based on specific condition is TRUE or
FALSE.
Types
if Statement: Executes a block of code if the condition is
true. Used for one condition.
if-else Statement: Executes one block if the condition is
true, another if it is false. Used for two condition.
if-elif-else Statement: Tests multiple conditions.
Python Conditional statement
6
if Statement: is the simplest form of a conditional
statement. It executes a block of code if the given condition
is true.
Example
age = 20
if age >= 18:
print("Eligible to vote.")
Python Conditional statement
7
If-else Statements: similar to if statement except the fact
that, it also provides the block of the code for the false case
of the condition to be checked. If the condition provided in
the if statement is false, then the else statement will be
executed.
Example:
age = 20
if age >= 18:
print(“You are eligible to vote.")
else:
print("You are not eligible to vote.")
Python Conditional statement
8
if-elif-else Statement : check multiple conditions and execute
the specific block of statements depending upon the true
condition among them. age = int(input("Enter the age: "))
Example:1
if age <= 0:
print(“Invalid age.")
elif age <= 12:
print(“You are Child.")
elif age <= 19:
print("You are Teenager.")
elif age <= 35:
print("You are Young adult.")
else:
print("You are Adult.")
Python Conditional statement
9
if-elif-else Statement :
Example:2 marks = int(input("Enter the marks? "))
if marks > 85 and marks <= 100:
print("Congrats ! you scored grade A ")
elif marks > 60 and marks <= 85:
print("You scored grade B +")
elif marks > 40 and marks <= 60:
print("You scored grade B")
elif (marks > 30 and marks <= 40):
print("You scored grade C")
else:
print("Sorry you are fail!")
Python Iteration statement (loop)
10
Python loop: used to run a single statement or set of
statements repeatedly using a loop command.
Types:
For loop
While loop
Python Iteration statement (loop)
11
For loop: Python's for loop is designed to repeatedly execute a
code block while iterating through a list, tuple, dictionary, range.
For loop allows you to apply the same operation to every item
within loop.
The process of traversing a sequence is known as iteration.
Example:1
for x in range(10): ,n = 10
print(x) for i in range(0, n):
print(i, end=‘, ’)
Result 0 1 2 3 4 5 6 7 8 9
Python Iteration statement (loop)
12
language = 'Python'
For loop:
Example:2 # iterate over each character in language
for x in language:
print(x)
Result
P
y
t
h
o
n
Python Iteration statement (loop)
13
numbers = [4, 2, 6, 7, 3, 5, 8, 10, 6, 1, 9, 2]
For loop:
square = 0
Example:3 # Creating an empty list
squares = [ ]
# Creating a for loop
for value in numbers:
square = value ** 2
squares.append(square)
print("The list of squares is", squares)
Result
The list of squares is [16, 4, 36, 49, 9, 25, 64, 100, 36, 1, 81, 4]
Python Iteration statement (loop)
14
While loop: Repeats a statement or group of statements while
a given condition is TRUE.
It tests the condition before executing the loop body.
Example:1 number = 1
while number <= 4:
print(number)
number = number + 1
Result: 1
2
3
4
Key Differences
Feature for Loop while Loop
Known in advance Unknown (depends on
Iteration Count
(fixed) condition)
Iterating over sequences Repeating until a
Use Case
or fixed ranges condition changes
Syntax for item in iterable: while condition:
More straightforward for More flexible for dynamic
Control Flow
fixed iterations conditions
Control Flow Statements
These statements alter the normal flow of execution within loops.
break Statement: Exits(end) the loop prematurely when the loop reach the
condition.
Example: for i in range(10):
if i == 5:
break # Exits the loop when i equals 5
print(i)
Result 0 1 2 3 4
Control Flow Statements
These statements alter the normal flow of execution within loops.
continue Statement: Skips the current iteration and moves to the next one.
Example:
for i in range(10):
if i == 5:
continue # Skips(jump) the iteration when i equals 5
print(i)
Result 0 1 2 3 4 6 7 8 9
Nested Control Structures
Nested control structures refer to the practice of placing one control
structure inside another.
This allows for more complex decision-making and flow control in a
program.
By nesting control structures, you can evaluate multiple conditions and
execute different blocks of code based on a pre-defined combination of
criteria.
Types of Nested Control Structures
Nested Conditional Statements
Nested Loops
Combination of Nested Conditionals and Loops
Nested Conditional Statements
Nested conditional statements involve placing an if, elif, or else statement
inside another conditional statement. This is useful when you need to check
multiple conditions.
Example: Evaluating a patient's health status based on age and symptoms
Nested Conditional Statements
age = int(input("Enter the age: "))
symptom = bool(input("Enter TRUE or FALSE: "))
Example: 1 if age > 60:
if symptom=="Yes":
print("High risk patient, recommend immediate care.")
else:
print("Monitor regularly.")
else:
print("Low risk patient.")
#the outer if checks the age of the patient. If the patient is older than 60,
the inner if checks for symptoms. This structure allows for more
nuanced decision-making based on multiple criteria.
Result High risk patient, recommend immediate care.
Nested Loops
Nested loops involve placing one loop inside another loop. This is often used
when dealing with multi-dimensional data structures, such as lists of lists.
Example: Printing a multiplication table
for i in range(1, 6): # Outer loop for rows
for j in range(1, 6): # Inner loop for columns
print(i * j, end=' ')
print() # New line after each row
Result 1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Nested Loops Example Explained
i=1 i=2 i=3 i=4 i=5
j=1 (1*1=1) j=1 (2*1=2) j=1 (3*1=3) j=1 (4*1=4) j=1 (5*1=5)
j=2 (1*2=2) j=2 (2*2=4) j=2 (3*2=6) j=2 (4*2=8) j=2 (5*2=10)
j=3 (1*3=3) j=3 (2*3=6) j=3 (3*3=9) j=3 (4*3=12) j=3 (5*3=15)
j=4 (1*4=4) j=4 (2*4=8) j=4 (3*4=12) j=4 (4*4=16) j=4 (5*4=20)
j=5 (1*5=5) j=5 (2*5=10) j=5 (3*5=15) j=5 (4*5=20) j=5 (5*5=25)
print(i * j,end=' ‘) print(i * j,end=' ‘) print(i * j,end=' ‘) print(i * j,end=' ‘) print(i * j,end=' ‘)
print() print() print() print() print()
Result 1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Importance of Control Structures
Control structures are vital for several reasons:
Decision Making: They allow programs to make decisions based on conditions,
enabling dynamic responses to different inputs or states.
Repetition: Loops facilitate repetitive tasks without the need for redundant code,
making programs more efficient and easier to maintain.
Complex Logic: Nested control structures enable the creation of complex logic
flows, allowing for sophisticated decision-making processes in applications.
Benefits of Nested Control Structures
Complex Decision-Making: They enable handling of complex scenarios where
multiple factors must be considered.
Organized Logic: Nesting helps keep related conditions and actions grouped
together, making the code easier to read and maintain.
Flexibility: They allow for dynamic responses based on various input
conditions, enhancing the interactivity of applications
Jupiter notebook launching problems
24
For file not found problem:
Type “pip install jupyter notebook” on CMD and hit ENTER KEY
For path problem:
Browse file location for anaconda and jupter
Paste in “edit the system environment variable”
Jupyter Interfaces
New->python
Heading/markdown
# First heading
## second heading
### third heading etc
Showing line number-View Menu-Toggle Line Number
25 .
26
Data Access and Sharing Policy