Conditional - Statements in Python by Yenny
Conditional - Statements in Python by Yenny
KNUST
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 1 / 19
Table of Contents
1 Introduction
3 Loops in Python
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 2 / 19
Overview of Control Flow
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 3 / 19
Importance of Conditional Statements and Loops
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 4 / 19
What Are Conditional Statements?
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
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
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
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
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
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 13 / 19
What Are Loops?
EMMANUEL KWAME YENDORK (KNUST)Conditional Statements and Loops in Python October 13, 2024 14 / 19
The ‘for‘ Loop
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