Conditional Statements and Loops in Python
EMMANUEL KWAME YENDORK
KNUST
October 13, 2024
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 1 / 19
Table of Contents
1 Introduction
2 Conditional Statements in Python
3 Loops in Python
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 2 / 19
Overview of Control Flow
Control flow determines the order in which statements are executed.
Essential for decision-making and repetitive tasks.
Comprises:
Conditional Statements: ‘if‘, ‘elif‘, ‘else‘.
Loops: ‘for‘, ‘while‘.
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 3 / 19
Importance of Conditional Statements and Loops
**Conditional Statements**: Allow programs to make decisions.
**Loops**: Enable repetitive execution of code blocks.
Fundamental for:
Data processing
Automation
Algorithm implementation
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 4 / 19
What Are Conditional Statements?
Allow execution of code blocks based on conditions.
Control the flow of the program.
Basic Structure:
if condition:
another condition:
else:
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 5 / 19
The ‘if‘ Statement
Evaluates a condition.
Executes the indented block if the condition is ‘True‘.
Syntax
if condition: Code block
Example
age = 18
if age >= 18:
print(”You are an adult.”)
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 6 / 19
The ‘else‘ Statement
Provides an alternative block of code.
Executes if the ‘if‘ condition is ‘False‘.
Syntax
if condition:
else:
Example
age = 16
if age >= 18:
print(”You are an adult.”)
else:
print(”You are a minor.”)
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 7 / 19
The ‘elif‘ Statement
Stands for ”else if”.
Checks multiple conditions sequentially.
Executes the block for the first ‘True‘ condition.
Syntax
if condition1:
elif condition2:
else:
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 8 / 19
The ’Elif’ Statement
Example
score = 85
if score >= 90:
print(”Grade: A”)
elif score >= 80:
print(”Grade: B”)
elif score >= 70:
print(”Grade: C”)
else:
print(”Grade: F”)
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 9 / 19
Nested Conditionals
Placing conditional statements within other conditional statements.
Allows for more complex decision-making.
Example
age = 20
has license = True
if age >= 18:
if has license:
print(”You can drive.”)
else:
print(”You need a driver’s license to drive.”)
else:
print(”You are too young to drive.”)
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 10 / 19
Logical Operators in Conditionals
Combine multiple conditions.
**‘and‘**: True if both conditions are True.
**‘or‘**: True if at least one condition is True.
**‘not‘**: Inverts the boolean value.
Examples
Using ’and’
temperature = 30
humidity = 70
if temperature > 25 and humidity > 60:
print(”It’s hot and humid.”)
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 11 / 19
Logical Operators in Conditionals
Example
Using ’or’
day = ”Sunday”
if day == ”Saturday” or day == ”Sunday”:
print(”It’s the weekend!”)
Example
Using ’not’
is raining = False
if not is raining:
print(”You don’t need an umbrella today.”)
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 12 / 19
Best Practices for Conditionals
1 Use Descriptive Conditions: Ensure conditions are clear and
meaningful.
2 Avoid Deep Nesting: Simplify code by avoiding multiple levels of
nested conditionals.
3 Consistent Indentation: Maintain proper indentation for readability.
4 Comment Complex Logic: Use comments to explain non-obvious
conditions.
5 Use Logical Operators Wisely: Combine conditions effectively
without making them too complex.
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 13 / 19
What Are Loops?
Allow execution of a block of code multiple times.
Essential for repetitive tasks and iterating over data structures.
Two primary types in Python:
‘for‘ Loop
‘while‘ Loop
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 14 / 19
The ‘for‘ Loop
Iterates over a sequence (e.g., list, tuple, string, range).
Executes the block for each item in the sequence.
Syntax
for item in sequence:
Code block
Example
fruits = [”apple”, ”banana”, ”cherry”]
for fruit in fruits:
print(fruit)
apple
banana
cherry
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 15 / 19
The ‘while‘ Loop
Repeats as long as a condition is ‘True‘.
Useful when the number of iterations is not known beforehand.
Syntax
while condition:
Code block
Example
count = 0
while count < 5:
print(count)
count += 1
0
1
2
3
4
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 16 / 19
Loop Control Statements
Modify the behavior of loops.
**‘break‘**: Exit the loop prematurely.
**‘continue‘**: Skip the current iteration and proceed to the next.
**‘pass‘**: Do nothing; serves as a placeholder.
‘break‘ Example
for i in range(10):
if i == 5:
break
print(i)
0
1
2
3
4
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 17 / 19
‘continue‘ and ‘pass‘ Statements
‘continue‘ Example
for i in range(10):
if i continue
print(i)
1
3
5
7
9
‘pass‘ Example
for i in range(5):
pass TODO: Implement later
**Explanation:** The loop runs but does nothing for each iteration.
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 18 / 19
THANK YOU
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 19 / 19