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.
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.
The lamp on and off
Watch the lamp: True shines, False is dark.
Click the arrow - you'll see variables with True and False as lamps.
1lamp = True2print(lamp)3lamp = False4print(lamp)5lamp = True6print(lamp)
โ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
1print(type(True))2print(type(7 > 5))3print(type("True"))4print(type(5))
โ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.
Code
๐ฅ๏ธ Screen
True twice
Kuba writes print("True"), and Ola print(True). The screen shows the same: True. So is it one and the same?
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.
Click the arrow - Bajtek will ask questions about the age.
1age = 152adult = age >= 183teenager = age >= 134print(adult)5print(teenager)
โ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.
Click the arrow - the sky will show what time it is.
1hour_str = input()2hour = int(hour_str)3dark = hour >= 204print(dark)
โ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 = 10big = x > 100print(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,trueis 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.