CodeBlanks Logo Icon CodeBlanks

bool

Learn True/False values with bool.

The bool type

bool is a logical type with only two values: True and False. These values are the basis of comparisons and conditions.

is_active = True
is_admin = False

bool is actually a special case of int: True = 1, False = 0. So True + True gives 2.

bool values are used in decisions with if and while. Comparison operators (==, <, >) return bool.

Truthy and falsy values

Python evaluates every value as if it were bool. Falsy values: 0, 0.0, , [], None. Everything else is truthy.

if 0:
    print("This will not run")
if "text":
    print("This will run")

This behavior is handy with if and while: to say "if not empty" just write if text:.

Practice Print bool variables

Assign True to is_active, assign False to is_admin. Then print print(is_active, is_admin).

Starter code

main.py
# Define the bool variables

Sample solution

main.py
is_active = True
is_admin = False
print(is_active, is_admin)

Expected output

True False

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Define the active variable

Question
is_active = ____
Answer
is_active = True

Explanation: is_active = True assigns a bool.

2 What does this code print?

main.py
print(True + True)
  • True
  • 1
  • It raises an error
  • 2 Correct answer

Explanation: Since True = 1, True + True = 2.

3 Match the values

Term Meaning
True True-like
False False-like
0 Falsy
"text" Truthy

All lessons in Beginner