Python / Making decisions

The bool type

Use True and False in variables and if conditions, and save the result of a condition to use later. Baccalaureate

Every condition you write after if ends up as one of two values: True or False. Try printing a comparison directly:

Python
print(5 > 3)
print(10 == 7)
score = 80
print(score >= 75)
Console
True
False
True

These two values are a data type called bool. They are written without quotes and start with a capital letter. You can see the type of any value with type():

Python
is_sunny = "True"
print(type(is_sunny))
is_sunny = True
print(type(is_sunny))
Console
<class 'str'>
<class 'bool'>
"True" in quotes is text (str); True without quotes is a bool. Writing true in lower case causes an error.

A bool variable in if

An if runs its block when the condition is True. So a bool variable can be the whole condition, instead of is_sunny == True:

Python
is_sunny = False
if is_sunny:
    print("Practice on the field")
else:
    print("Practice in the gym")
Console
Practice in the gym

Saving the result of a condition

You can check a condition, store the answer in a variable, and use that variable with another condition later. None means "no value yet"; use it as a starting value:

Python
stamp_count = 8
help_count = 5
quiz_score = 80
result = None

if stamp_count >= 8 and help_count >= 4:
    result = True
else:
    result = False

if result and quiz_score >= 75:
    print("Booth approved!")
else:
    print("Maybe next year!")
Console
Booth approved!

Since a comparison already gives True or False, you can assign it straight to a variable in one line:

Python
age = 17
is_adult = age >= 18
print(is_adult)
Console
False

Practice

1Fill in the blankCurriculum practice

Assign True to is_sunny so the program prints Practice on the field.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
is_sunny = ___
if is_sunny:
    print("Practice on the field")
else:
    print("Practice in the gym")
Console
Press Run to see the result.
2Fill in the blankCurriculum practice

Complete the condition so Ready to go! is printed when is_ready is true. Use the variable on its own as the condition.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
is_ready = True
___
    print("Ready to go!")
else:
    print("Not ready yet...")
Console
Press Run to see the result.
3Write the programCurriculum practice

Print the data type of the value in is_sunny using type().

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
is_sunny = True
Console
Press Run to see the result.
4Fill in the blankCurriculum practice

If the condition holds, assign True to result; otherwise assign False.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
ticket_count = 8
help_count = 5
result = False

if ticket_count >= 8 and help_count >= 4:
    ___
else:
    ___

if result:
    print("Booth approved!")
else:
    print("Maybe next year!")
Console
Press Run to see the result.
5Fill in the blankCurriculum practice

Complete the second condition: result is true and quiz_score is 75 or more.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
stamp_count = 8
help_count = 5
quiz_score = 60
result = None

if stamp_count >= 8 and help_count >= 4:
    result = True
else:
    result = False

if ___:
    print("Booth approved!")
else:
    print("Maybe next year!")
Console
Press Run to see the result.
6Edit the programExtra practice

This program always prints Bring your umbrella even though no rain is expected, because the value is text, not a bool. Fix it so it prints No umbrella needed.

HintAny non-empty text counts as True inside an if.
Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
will_rain = "False"
if will_rain:
    print("Bring your umbrella")
else:
    print("No umbrella needed")
Console
Press Run to see the result.
7Write the programExtra practice

Given homework_done = True and mark = 64, store the comparison mark >= 50 in passed. If homework is done and passed is true, print You can play now; otherwise print Study first.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
homework_done = True
mark = 64
Console
Press Run to see the result.

Key points

  • A bool is only True or False, without quotes.
  • A comparison like score >= 75 gives a bool you can store in a variable.
  • if is_sunny: means the same as if is_sunny == True:.