Every condition you write after if ends up as one of two values: True or False. Try printing a comparison directly:
print(5 > 3) print(10 == 7) score = 80 print(score >= 75)
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():
is_sunny = "True" print(type(is_sunny)) is_sunny = True print(type(is_sunny))
<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:
is_sunny = False
if is_sunny:
print("Practice on the field")
else:
print("Practice in the gym")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:
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!")Booth approved!
Since a comparison already gives True or False, you can assign it straight to a variable in one line:
age = 17 is_adult = age >= 18 print(is_adult)
False
Practice
Assign True to is_sunny so the program prints Practice on the field.
is_sunny = ___
if is_sunny:
print("Practice on the field")
else:
print("Practice in the gym")Press Run to see the result. Complete the condition so Ready to go! is printed when is_ready is true. Use the variable on its own as the condition.
is_ready = True
___
print("Ready to go!")
else:
print("Not ready yet...")Press Run to see the result. Print the data type of the value in is_sunny using type().
is_sunny = True
Press Run to see the result. If the condition holds, assign True to result; otherwise assign False.
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!")Press Run to see the result. Complete the second condition: result is true and quiz_score is 75 or more.
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!")Press Run to see the result. 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.
Hint
Any non-empty text counts as True inside an if.will_rain = "False"
if will_rain:
print("Bring your umbrella")
else:
print("No umbrella needed")Press Run to see the result. 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.
homework_done = True mark = 64
Press Run to see the result. Key points
- A bool is only
TrueorFalse, without quotes. - A comparison like
score >= 75gives a bool you can store in a variable. if is_sunny:means the same asif is_sunny == True:.