Python · Variables and types
Boolean
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Intro to Booleans
Boolean is a special type of variable that can only have two values:
True
or
False
. (Pay attention to the capital letters!)
It is used to represent truth values in programming. And even if it doesn't seem that impressive and pretty simple, it is actually one of the most important types in programming!
codemon_alive = True print(codemon_alive) codemon_sleeping = False print(codemon_sleeping)
Only TRUE or FALSE?
Booleans are True/False values. But they can also be understood as 1/0, ON/OFF, YES/NO, PRESENCE/ABSENCE, etc.
Exercise 2 — Correct way of writing a Boolean variable
Which of these is a correct way to create a Boolean variable in Python?
Options
• ```codemon_alive = True```
• ```codemon_alive = TRUE```
• ```codemon_alive = true```
• ```codemon_alive = "True"```
Hint : Remember: Boolean values must start with a capital letter in Python.
Exercise 3 — Boolean Values
What are the two possible values for a Boolean variable in Python?
Options
• ```True, False```
• ```true, false```
• ```YES, NO```
• ```1, 0```
Hint : Boolean variables in Python can only have two values: True or False (with capital letters).
Exercise 4 — Which is a Boolean?
Which of these variables contains a Boolean value?
Options
• ```is_ready = False```
• ```age = 25```
• ```name = "Pierre"```
• ```level = 4.2```
Hint : False is a Boolean value. The other variables contain a number, a string, and a decimal number.
Exercise 5 — Creating a Boolean Variable
Complete the code to create a Boolean variable that indicates the Codémon is sleeping:
is_sleeping = ____ print(is_sleeping)
Options: True true false TRUE FALSE "True" "False"
Hint : Boolean variables can be True or False. Remember the capital letters!
Exercise 6 — Ready or not ?
Create a boolean variable
ready_for_battle
and set it to the value you want (boolean value). Then, print the variable and its type.
Starter code
# Code here
Back to the course