Making decisions with conditionals
temperature = 35
if temperature > 30:
print("It's hot outside!")
else:
print("It's nice out.")if and else are the body of each branch. Python uses indentation (4 spaces) instead of braces {} like other languages.elif (short for "else if") to check multiple conditions in order:score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(f"Score: {score} → Grade: {grade}")True or False:== equal to | != not equal to> greater than | < less than>= greater or equal | <= less or equalage = 25
has_id = True
if age >= 21 and has_id:
print("Access granted")
temperature = 15
if temperature < 0 or temperature > 40:
print("Extreme weather!")
else:
print("Weather is fine")
is_raining = False
if not is_raining:
print("No umbrella needed")logged_in = True
is_admin = False
if logged_in:
if is_admin:
print("Welcome, admin")
else:
print("Welcome, user")
else:
print("Please log in")Build a grading system: given a `score` variable, print the grade (A/B/C/D/F) with a message. Use `elif` for each threshold and an f-string for the output.
loading editor...
Ask about Control Flow
◇
Hey! I'm your AI tutor.
Ask me anything about this lesson, or paste code you're stuck on.
AI tutor is a premium feature