Day 5: Conditional Statements
if / elif / else
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(f"Grade: {grade}") # Grade: B
Ternary Operator
You can express a condition in a single line.
age = 20
status = "adult" if age >= 18 else "minor"
print(status) # adult
Combining Logical Operators
temperature = 25
humidity = 60
if temperature > 20 and humidity < 70:
print("Pleasant weather")
elif temperature > 35 or humidity > 85:
print("Uncomfortable weather")
Truthy and Falsy
These are the values that evaluate to false in Python.
| Falsy Value | Description |
|---|---|
False | Boolean false |
0, 0.0 | Numeric zero |
"" | Empty string |
[], (), {} | Empty collections |
None | No value |
items = []
if not items:
print("The list is empty")
name = ""
username = name or "anonymous"
print(username) # anonymous
Today’s Exercises
- Write a program that takes a year as input and determines whether it is a leap year.
- Take three numbers as input and print the largest one (without using the max function).
- Calculate BMI and classify it as underweight/normal/overweight/obese.