Comparison and Logic
A comparison checks two values and gives True or False:
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
These three words join conditions together or flip a condition:
| Operator | Result is True when... |
|---|---|
and | Both sides are True |
or | At least one side is True |
not | The value after it is False |
age = 20
is_citizen = True
old_enough = age >= 18
can_vote = old_enough and is_citizen
young = age <= 25
print("Old enough:", old_enough)
print("Can vote:", can_vote)
print("Young:", young)
Output:
Old enough: True
Can vote: True
Young: True
First, Python checks age >= 18 and stores the result in old_enough. Then and checks that both voting rules are true: the person is old enough, and the person is a citizen. <= means "less than or equal to", so an age of exactly 25 would still count as young.
In the exercise, check whether a number is inside a range.