Combining conditions: and, or, not
🌍 In real life. A cinema lets you in if you have a ticket and you're old enough for the film: both must be true. A shop gives a discount if you're a student or over 60: one is enough. And "if it's not raining, let's walk" flips the question around.
Python has exactly these three words for joining conditions.
The idea in one sentence: and, or and not combine True/False answers into one answer.
Its words:
andis True when both sides are Trueoris True when at least one side is Truenotflips True to False, and False to True- short-circuiting: stopping as soon as the answer is known
Step by step, for age >= 13 and age <= 19 when age is 15:
age >= 13is True.andstill needs the right side:age <= 19is True.- Both are True, so the whole condition is True.
age = 15
if age >= 13 and age <= 19:
print("teenager")With or, one True side is enough. Python is lazy in a clever way: once the left side is True, it doesn't even look at the right side.
day = "Sat"
if day == "Sat" or day == "Sun":
print("weekend!")not reads naturally in English:
raining = False
if not raining:
print("Let's go outside")Python also lets you chain comparisons: 13 <= age <= 19 means the same as age >= 13 and age <= 19. When you mix and and or, add brackets so the order is clear: (a or b) and c.
age = 15
print(13 <= age <= 19)
print((age < 5 or age > 60) and age != 0)Python stops early. and stops at the first part that counts as False, and or stops at the first part that counts as True; the rest isn't even worked out. This is called short-circuiting, and it makes x != 0 and 10 / x > 1 safe when x is 0: the division never happens. One more surprise: and and or give back one of their parts, not always True or False. name or "guest" gives name if it counts as True (Lesson 5.4: an empty string counts as False), otherwise "guest".
x = 0
print(x != 0 and 10 / x > 1)
name = ""
print(name or "guest")
print("Rimi" or "guest")Mistakes and edge cases.
- Leaving out the second comparison.
day == "Sat" or "Sun"is always True: Python reads it as(day == "Sat") or "Sun", and the string"Sun"counts as True. Writeday == "Sat" or day == "Sun". See below. - A half comparison.
age > 13 and < 19givesSyntaxError: invalid syntax. Each side needs its own comparison, or chain them:13 < age < 19. - Mixing and with or.
andgoes beforeor, soa or b and cmeansa or (b and c). Add brackets. - not reaches the whole comparison.
not x == 5meansnot (x == 5), which is the same asx != 5.
day = "Mon"
if day == "Sat" or "Sun":
print("weekend!")Quiz
What does this print?
print(True and False)What does this print?
print(True or False)What does this print?
print(not True)What does this print?
x = 5 print(x > 1 and x < 10)When is
a or bFalse?- Only when both a and b are False
- When either one is False
- Never
This prints weekend! for every day. Which line has the mistake?
day = "Tue" if day == "Fri" or "Sat": print("weekend!")
Coding tasks
Ask for an age. Print
teenif it is from 13 to 19 (both included). Otherwise, printnot a teen.Ask for a year. Print
leapif it is a leap year, otherwisenot leap.A year is a leap year if it divides by 4 but not by 100, or if it divides by 400.