Python · Boolean in Depth
Comparison Operators
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Intro to Comparison operators
Hopefully, you remember the boolean values
True
and
False
from the previous lesson.
There is a way to "create" those boolean values, by using comparison operators. They are used to compare two values and return a boolean value.
If the result of the comparison is true, it returns
True
, otherwise it returns
False
.
print(2 > 1) # 2 > 1, True
print(1 > 2) # 1 > 2, False
Exercise 2 — Comparison operators
You can create Boolean values by comparing values with these operators:
==
Equal to (checks if two values are the same)
!=
Not equal to (checks if two values are different)
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
So basically, if the comparision is true, it will return
True
, otherwise it will return
False
.
Try to play around with the code below to understand how it works.
print('equal to', 2 == 2)
print('not equal to', 2 != 2)
print('greater than', 2 > 1)
print('less than', 1 < 2)
print('greater than or equal to', 2 >= 1)
print('less than or equal to', 1 <= 2)
Exercise 3 — True or False?
Match each comparison on the left with it's result on the right.
Items to match
• 10 == 10
• 10 != 10
• 10 > 10
• 10 <= 10
Definitions
• True
• False
• False
• True
Exercise 4 — Which comparison method
Which comparison will return True for these values:
codemon_level = 75
,
opponent_level = 70
?
Options
• ```codemon_level > opponent_level```
• ```codemon_level < opponent_level```
• ```codemon_level == opponent_level```
• ```codemon_level >> opponent_level```
Hint : Remember: == is the operator for 'equal to', != is the operator for 'not equal to', you can figure out the rest
Exercise 5 — Combination of comparison
Hopefully, you remember that we saw the operators
OR
,
AND
and
NOT
. They are used to combine boolean values.
Like we saw earlier, we can replace boolean values with comparison, we can also use those operators to combine everything!
level = 10
alive = True
print('Codemon ready ?', level > 5 and alive)
Exercise 6 — Try to solve this
Check if your Codémon can train. For that, his HP must be greater than 0 and he must be awake.
codemon_hp = 10
codemon_awake = True
can_train = codemon_hp ____ 0 ____ codemon_awake
print(can_train)
Options: and or not == < > !=
Hint : Remember: And is used to check if both conditions are True. Or is used to check if at least one condition is True.
Exercise 7 — Try to solve this
You want to enter a tournament with a level limit. Check if your Codémon level is between 10 and 20 (inclusive).
codemon_level = 15
can_enter = codemon_level ____ 10 ____ codemon_level ____ 20
print(can_enter)
Options: and or not == <= >= !=
Hint : Remember: And is used to check if both conditions are True. Or is used to check if at least one condition is True.
Exercise 8 — Try to solve this
To enter this arena, you need to either pay 10 coins (so you need at least 10 coins) or have a Codémon with a level strictly greater than 20. Check if you can enter.
coins = 5
codemon_level = 25
can_enter = coins ____ 10 ____ codemon_level ____ 20
print(can_enter)
Options: >= == != > < or and not
Hint : Remember: Strictly greater than X means than X is not included.
Exercise 9 — Try to solve this
Can you check if at least one of your 2 Codémon is awake ?
cdm_1_sleeping = False
cdm_2_sleeping = True
one_awake = ____ cdm_1_sleeping ____ ____ cdm_2_sleeping
print(one_awake)
Options: and or not not or and
Hint : Remember: Not is used to invert a boolean value. And is used to check if both conditions are True. Or is used to check if at least one condition is True.
Exercise 10 — Tournament Eligibility Check
The Grand Tournament has strict rules! Your Codémon can participate if:
- Its level is between 15 and 30 (inclusive)
- AND it's awake
- AND it has either won at least 5 battles OR has a power greater than 80
Create a boolean variable
can_participate
that checks all these conditions, then print it.
Starter code
level = 20
power = 75
is_awake = True
battles_won = 6
# Code here
Back to the course