Topic 03 of 12 | π Lecture 3 β ICT582Topic03.pdf
Control Flow: Conditions
if, elif, else statements β making decisions in your program.
π― Learning Objectives
- Write
if,if-else, andif-elif-elsestatements - Use comparison operators:
==,!=,<,>,<=,>= - Use logical operators:
and,or,not - Write nested
ifstatements - Understand truthy and falsy values in Python
π Key Concepts
Basic if Statement
Executes a block only if the condition is True. Indentation (4 spaces) defines the block.
if condition:
# runs when condition is True
statement
score = 75
if score >= 50:
print("Pass")
if-else Statement
One branch runs when condition is True, the other when it's False.
score = 40
if score >= 50:
print("Pass")
else:
print("Fail")
if-elif-else Chain
Checks multiple conditions in order β only the first matching branch runs.
score = 72
if score >= 80:
grade = "HD"
elif score >= 70:
grade = "D"
elif score >= 60:
grade = "C"
elif score >= 50:
grade = "P"
else:
grade = "N"
print("Grade:", grade) # Grade: D
Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | x == 5 |
!= | Not equal to | x != 5 |
< | Less than | x < 10 |
> | Greater than | x > 10 |
<= | Less than or equal | x <= 10 |
>= | Greater than or equal | x >= 10 |
Logical Operators
| Operator | Meaning | Example |
|---|---|---|
and | Both must be True | x > 0 and x < 10 |
or | At least one True | x < 0 or x > 100 |
not | Inverts True/False | not (x == 5) |
age = 20
if age >= 18 and age <= 65:
print("Working age")
Truthy and Falsy Values
In Python, the following values are considered False (falsy):
0,0.0β zero numbers""β empty string[],(),{}β empty collectionsNoneFalse
Everything else is True (truthy).
Nested if Statements
An if inside another if. Be careful with indentation.
age = 20
has_license = True
if age >= 18:
if has_license:
print("Can drive")
else:
print("Need a license")
else:
print("Too young")
Chaining Comparisons
Python allows chained comparisons β very readable:
# Checks if 0 < x and x < 10
0 < x < 10 # Pythonic way
# Equivalent but longer:
x > 0 and x < 10 # Same result
π» Code Examples
Example 1 β BMI calculator with conditions
weight = float(input("Weight (kg): "))
height = float(input("Height (m): "))
bmi = weight / (height ** 2)
if bmi < 18.5:
print("Underweight")
elif bmi < 25:
print("Normal")
elif bmi < 30:
print("Overweight")
else:
print("Obese")
Example 2 β Using logical operators
username = input("Username: ")
password = input("Password: ")
if username == "admin" and password == "1234":
print("Login successful")
else:
print("Invalid credentials")
β οΈ Exam Focus
- Know that
=is assignment,==is comparison β mixing them is a very common error. - In an
if-elif-elsechain, only the first matching branch runs β order matters! - Trace through nested if statements and identify exactly what gets printed for given inputs.
- Know logical operators:
andrequires both True;orneeds at least one True. - Indentation errors are syntax errors β be precise in exam code.
β Common Mistakes
- Using
=instead of==in conditions β assignment vs comparison. - Wrong indentation β Python uses indentation to define blocks; wrong indent = wrong logic.
- Putting conditions in wrong order in elif chain β most specific first.
- Forgetting the colon
:afterif,elif,else.
β‘ Quick Recap
if cond:runs when True;else:runs when False.elifchecks additional conditions β only first match runs.- Comparison:
==,!=,<,>,<=,>=. - Logical:
and,or,notβ combine boolean expressions. - Indentation defines which statements belong to which block.