AlgoBazaAlgoBaza
Python for kids ยท Module 2

Lesson 20

True and False

After this lesson: You'll store the answer to a question in a variable and meet the bool type.

Video

?

The video for this lesson is coming soon.

Explanation

In lesson 19 Python answered questions: True or False. Today we'll discover a secret: these two words are values - just like numbers and text. You can put them in a box, print them and check their type! ๐Ÿ’ก

The lamp in Bajtek's room ๐ŸŽฌ

Click Next - you'll see that True and False are like a lamp switched on and off.

Bajtek
โœจ today you'll learn โœจA new data type: boolTrueFalse
1

Something new today!

You already know numbers (int, float) and text (str). Today you'll learn a new data type: bool. It has only two values: True and False.

Step 1 / 6

The lamp on and off

Watch the lamp: True shines, False is dark.

empty
lamp

Click the arrow - you'll see variables with True and False as lamps.

Code
1lamp = True
2print(lamp)
3lamp = False
4print(lamp)
5lamp = True
6print(lamp)
Screen
โ–
1/7 ยท line 1 runs next

The bool type

Every value has its type - remember from lesson 15? 5 is an int, "cat" is a str, 2.5 is a float. True and False have the type bool:

print(type(True))
print(type(7 > 5))

Both times Python prints <class 'bool'>. The bool type has only two values: True and False.

The type of an answer

Code
1print(type(True))
2print(type(7 > 5))
3print(type("True"))
4print(type(5))
Screen
โ–
1/5 ยท line 1 runs next

Fun fact: where does the name bool come from?

From the English mathematician George Boole, who almost 200 years ago worked out how to calculate with only trues and falses. Today every computer calculates exactly like that! ๐Ÿงฎ

A trap: True in quotes ๐ŸŽฌ

Click Next - two programs print the same thing, and yet they are different.

?Bajtek
But it looks the same...๐Ÿค”

Code

print("True")
print(True)

๐Ÿ–ฅ๏ธ Screen

True
True
1

True twice

Kuba writes print("True"), and Ola print(True). The screen shows the same: True. So is it one and the same?

Step 1 / 4

An answer in a box

The result of a comparison can be stored in a variable. Then we ask the question once and have the answer for later:

age = 15
adult = age >= 18
teenager = age >= 13

Python works out every comparison and puts True or False into the box. A good name for such a variable sounds like the answer to a question: adult, dark, won.

Two answers in two boxes

Two questions about the age - each answer is True or False.

๐Ÿง” adult from 18
๐Ÿง‘ teenager from 13
0102030
๐Ÿ“ฆ adult = ?
๐Ÿ“ฆ teenager = ?

Click the arrow - Bajtek will ask questions about the age.

Code
1age = 15
2adult = age >= 18
3teenager = age >= 13
4print(adult)
5print(teenager)
Screen
โ–
1/6 ยท line 1 runs next

Is it dark yet? ๐ŸŒ™

Type an hour from 0 to 23 ๐Ÿ•— (e.g. 21) and press Enter โŽ - the sky shows the hour, and the house tells you whether to switch the light on.

๐Ÿ•— --:00

Click the arrow - the sky will show what time it is.

Code
1hour_str = input()
2hour = int(hour_str)
3dark = hour >= 20
4print(dark)
Screen
โ–
1/2 ยท line 1 runs next
?

What type does the result of 5 > 3 have?

print(type(5 > 3))
?

Which of these is a bool value?

?

What will this program print?

x = 10
big = x > 100
print(big)

Remember

  • True and False are values of type bool - like a lamp on and off.
  • We write them with a capital letter and without quotes. "True" is text, true is an error.
  • The answer to a comparison can be stored in a variable: dark = hour >= 20.

On to the tasks! Store the answers in boxes and print them.

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.