Lesson 25
and, or, not - joining conditions
After this lesson: You'll join several conditions into one: all at once (and), at least one (or) or the other way round (not).
Video
The video for this lesson is coming soon.
Explanation
Sometimes one condition isn't enough. You can get on the ride when you're 120 cm and have a ticket. Ice cream is free when it's your birthday or it's Sunday. To join conditions Python has three little words: and, or and not. Today you'll see them as... electricity! โก
A door with two locks ๐ฌ
Click Next - and is two locks that must both be open.
Bajtek's treasury
Bajtek hides treasure ๐ behind a door with two locks. Each lock opens with a different key: a red one ๐ด and a blue one ๐ต.
Lab: and โก
Click the switches. With and they stand one after another - current has to pass through both.
๐ก I get on: False
| I'm 120 cm | I have a ticket | I'm 120 cm and I have a ticket |
|---|---|---|
| True | True | True ๐ก |
| True | False | False |
| False | True | False |
| False | False | False |
and: the switches stand one after another. Click them - the bulb lights only when ALL of them are on.
The ride: height and ticket
Both conditions are true - current flows through both switches and the bulb shines.
Click the arrow - you'll see the condition as a circuit with switches and a bulb.
1height = 1302ticket = True3if height >= 120 and ticket:4print("Get on!")5else:6print("Not this time")
โNo ticket
One switch is open - the current won't pass, even though the height is fine.
Click the arrow - you'll see the condition as a circuit with switches and a bulb.
1height = 1302ticket = False3if height >= 120 and ticket:4print("Get on!")5else:6print("Not this time")
โFree ice cream ๐ฌ
Click Next - or is two roads to the same prize.
A deal at the ice cream shop
The ice cream shop has a sign: free ice cream if it's your birthday or it's Sunday ๐ฆ. One of the two is enough!
Lab: or โก
With or the switches stand side by side - the current has two roads. One is enough!
๐ก free ice cream: True
| it's my birthday | it's Sunday | it's my birthday or it's Sunday |
|---|---|---|
| True | True | True ๐ก |
| True | False | True ๐ก |
| False | True | True ๐ก |
| False | False | False |
or: the switches stand side by side. Click them - ONE switch on is enough for the bulb to shine.
Free ice cream in Python
Day 3 is Wednesday, but it's a birthday - one road is enough.
Click the arrow - you'll see the condition as a circuit with switches and a bulb.
1birthday = True2day = 33if birthday or day == 7:4print("Free ice cream! ๐ฆ")5else:6print("Ice cream for 5 zloty")
โnot - the other way round
The third little word, not, flips the answer: not True is False, and not False is True. It's handy when something should happen when a condition is not met:
if not raining:
print("Let's go for a walk")
We read it: if it's not raining - let's go for a walk.
Lab: not โก
not is a switch that works the other way round. Click it and see!
๐ก a walk: False
| raining | not raining |
|---|---|
| True | False |
| False | True ๐ก |
not: the switch works the other way round. Click it - when the condition is True the bulb goes out, and when it's False it shines.
A walk when it isn't raining
raining is False, and not turns it into True - the bulb shines.
Click the arrow - you'll see the condition as a circuit with switches and a bulb.
1raining = False2if not raining:3print("Let's go for a walk ๐ณ")4else:5print("We stay at home")
โA number in a range
A very common question: is a number between 10 and 20? That's two conditions at once - so and:
if n >= 10 and n <= 20:
Python also knows a shorter way, like in maths: 10 <= n <= 20. Both mean the same - pick the one you understand better.
| Question | Condition |
|---|---|
| n from 10 to 20 | n >= 10 and n <= 20 |
| n outside the range 0..100 | n < 0 or n > 100 |
| Saturday or Sunday | day == 6 or day == 7 |
Are you a teenager? ๐ง
A teenager is 13 to 19 years old. Type an age ๐ and press Enter โ - the current passes only through two closed switches.
Click the arrow - you'll see the condition as a circuit with switches and a bulb.
1age_str = input()2age = int(age_str)3if age >= 13 and age <= 19:4print("A teenager!")5else:6print("Not a teenager")
โThe weekend? ๐
Type the number of a day of the week ๐ (1 - Monday, 7 - Sunday) and press Enter โ.
Click the arrow - you'll see the condition as a circuit with switches and a bulb.
1day_str = input()2day = int(day_str)3if day == 6 or day == 7:4print("The weekend! ๐")5else:6print("School ๐")
โA trap: or with a bare number
Kuba writes if day == 6 or 7: - and the program always says The weekend! Why? Python reads it as (day == 6) or 7, and the bare number 7 (not zero) counts as True. Each side of or must be a whole question: day == 6 or day == 7.
What will this program print?
n = 15print(n > 10 and n < 20)
What will this program print?
n = 5print(n < 0 or n > 100)
What does print(not 3 > 1) print?
print(not 3 > 1)
Which condition checks whether n is between 1 and 9?
Remember
- and - true when all the parts are true (switches one after another).
- or - true when at least one part is true (switches side by side).
- not - flips: not True is False, not False is True.
- A range:
n >= 10 and n <= 20or10 <= n <= 20. - Each side of or / and must be a whole question:
day == 6 or day == 7.
On to the tasks! First think: do you need all the conditions (and), or is one enough (or)?
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.