Python 24-Day Course - Day 5: Conditional Statements

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 ValueDescription
FalseBoolean false
0, 0.0Numeric zero
""Empty string
[], (), {}Empty collections
NoneNo value
items = []
if not items:
    print("The list is empty")

name = ""
username = name or "anonymous"
print(username)  # anonymous

Today’s Exercises

  1. Write a program that takes a year as input and determines whether it is a leap year.
  2. Take three numbers as input and print the largest one (without using the max function).
  3. Calculate BMI and classify it as underweight/normal/overweight/obese.

Was this article helpful?