AlgoBazaAlgoBaza
Python for kids ยท Module 2

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
๐Ÿ”’๐Ÿ”’๐Ÿšช๐Ÿ’Ž
1

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 ๐Ÿ”ต.

Step 1 / 4

Lab: and โšก

Click the switches. With and they stand one after another - current has to pass through both.

โšกI'm 120 cmTrueI have a ticketFalseโ—‹

๐Ÿ’ก I get on: False

I'm 120 cmI have a ticketI'm 120 cm and I have a ticket
TrueTrueTrue ๐Ÿ’ก
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
?

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.

Code
1height = 130
2ticket = True
3if height >= 120 and ticket:
4 print("Get on!")
5else:
6 print("Not this time")
Screen
โ–
1/5 ยท line 1 runs next

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.

Code
1height = 130
2ticket = False
3if height >= 120 and ticket:
4 print("Get on!")
5else:
6 print("Not this time")
Screen
โ–
1/5 ยท line 1 runs next

Free ice cream ๐ŸŽฌ

Click Next - or is two roads to the same prize.

Bajtek
๐ŸŽ‚or๐Ÿ“…Sundayโžก๏ธ๐Ÿฆ
1

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!

Step 1 / 4

Lab: or โšก

With or the switches stand side by side - the current has two roads. One is enough!

โšกit's my birthdayTrueit's SundayFalse๐Ÿ’ก

๐Ÿ’ก free ice cream: True

it's my birthdayit's Sundayit's my birthday or it's Sunday
TrueTrueTrue ๐Ÿ’ก
TrueFalseTrue ๐Ÿ’ก
FalseTrueTrue ๐Ÿ’ก
FalseFalseFalse

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.

Code
1birthday = True
2day = 3
3if birthday or day == 7:
4 print("Free ice cream! ๐Ÿฆ")
5else:
6 print("Ice cream for 5 zloty")
Screen
โ–
1/5 ยท line 1 runs next

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!

โšกnot โ†ปnot rainingTrue โ†’ not โ†’ Falseโ—‹

๐Ÿ’ก a walk: False

rainingnot raining
TrueFalse
FalseTrue ๐Ÿ’ก
?

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.

Code
1raining = False
2if not raining:
3 print("Let's go for a walk ๐ŸŒณ")
4else:
5 print("We stay at home")
Screen
โ–
1/4 ยท line 1 runs next

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.

QuestionCondition
n from 10 to 20n >= 10 and n <= 20
n outside the range 0..100n < 0 or n > 100
Saturday or Sundayday == 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.

Code
1age_str = input()
2age = int(age_str)
3if age >= 13 and age <= 19:
4 print("A teenager!")
5else:
6 print("Not a teenager")
Screen
โ–
1/2 ยท line 1 runs next

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.

Code
1day_str = input()
2day = int(day_str)
3if day == 6 or day == 7:
4 print("The weekend! ๐ŸŽ‰")
5else:
6 print("School ๐ŸŽ’")
Screen
โ–
1/2 ยท line 1 runs next

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 = 15
print(n > 10 and n < 20)
?

What will this program print?

n = 5
print(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 <= 20 or 10 <= 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.