If Statements
Run code only when something is true.
Comparing two values
One = stores a value. Two == asks a question. Mixing them up is the most common bug there is.
| Symbol | What it asks | Example |
|---|---|---|
| == | Is it equal to? | name == "Becca" |
| != | Is it not equal to? | name != "Becca" |
| < | Is it less than? | score < 60 |
| > | Is it greater than? | score > 90 |
| <= | Is it less than or equal to? | age <= 12 |
| >= | Is it greater than or equal to? | age >= 18 |
Comparing text
Text comparisons are exact. Capital letters matter.
name = "Becca"
if name == "Becca":
print("Hey, it is Becca.")
else:
print("Not Becca.")Hey, it is Becca.
Comparing text without caring about capitals
.lower() both sides and "BECCA", "becca", and "Becca" all match. Handy after input(), since you never know how they typed it.
name = input("Enter a name: ")
if name.lower() == "becca":
print("Hey, it is Becca.")
else:
print("Not Becca.")Enter a name: BECCA Hey, it is Becca.
Pick which code runs
if on its own
Runs only when the condition is true. Otherwise nothing happens.
age = 27
if age >= 18:
print("You can vote.")You can vote.
A True or False variable as the condition
The variable is already True or False, so it can be the whole condition. No == True needed.
is_with_adult = True
if is_with_adult:
print("You can go on the ride.")You can go on the ride.
if with else
else runs whenever the if is false.
age = 15
if age >= 18:
print("You can vote.")
else:
print("Not yet.")Not yet.
if, elif, else
Python checks top to bottom and stops at the first true one.
score = 84
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("Keep working.")B
Putting them in the wrong order
95 is already at least 70, so Python stops at the first one. Put the hardest one to reach first.
score = 95
if score >= 70:
print("C")
elif score >= 80:
print("B")
elif score >= 90:
print("A")C
elif or separate ifs?
Same numbers, different answers. Neither one is wrong. Pick the one that does what you want.
Separate ifs run every match
Each if is its own question, so two of them can be true.
age = 27
if age < 20:
print("Under 20.")
if age < 30:
print("Under 30.")
if age < 100:
print("Under 100.")Under 30. Under 100.
elif stops at the first match
Once one is true, Python skips the rest.
age = 27
if age < 20:
print("Under 20.")
elif age < 30:
print("Under 30.")
elif age < 100:
print("Under 100.")Under 30.
Checking two things at once
and: both must be true
Both parts are true, so the login works.
username = "Ashlee"
password = "0321"
if username == "Ashlee" and password == "0321":
print("Welcome, Ashlee.")
else:
print("Wrong username or password.")Welcome, Ashlee.
and: one wrong is enough to fail
Same code, wrong password. One bad part fails the whole thing.
username = "Ashlee"
password = "0322"
if username == "Ashlee" and password == "0321":
print("Welcome, Ashlee.")
else:
print("Wrong username or password.")Wrong username or password.
or: just one must be true
It is Saturday, so Python never even checks Sunday.
day = "Saturday"
if day == "Saturday" or day == "Sunday":
print("It is the weekend.")
else:
print("It is a school day.")It is the weekend.
Two cases, one message
When two branches would print the same thing, join their conditions with or. Now the message is written once.
role = "worker"
age = 66
if role == "manager":
print("Free coffee.")
elif role == "employee" or age > 55:
print("Discounted coffee.")
else:
print("No discount.")Discounted coffee.
not: flips True and False
Reads as: if it is not raining.
is_raining = False
if not is_raining:
print("Go outside.")Go outside.
Is it between two numbers?
Each side of and needs its own full comparison. age > 30 and < 70 is an error, because < 70 does not say what is less than 70.
Between, with and
Bigger than 30 and smaller than 70.
age = 44
if age > 30 and age < 70:
print("Welcome.")
else:
print("Access denied.")Welcome.
Between, the math class way
Python lets you write it like a math problem. Same answer as the one using and.
age = 44
if 30 < age < 70:
print("Welcome.")
else:
print("Access denied.")Welcome.
An if inside an if
Nested if
Lets you tell the two failures apart instead of giving one vague message.
username = "Ashlee"
password = "0322"
if username == "Ashlee":
if password == "0321":
print("Welcome, Ashlee.")
else:
print("Right username. Wrong password.")
else:
print("That is not Ashlee.")Right username. Wrong password.