ComparisonsSlide solved ✓
Sometimes you need to compare two numbers to see which is larger or smaller.
< (less than) and
> (greater than) let you do this.
Here are some examples of using
< to compare variables with numbers:
age < 18 day < 30
The result is a boolean value:
This code uses
> along with a conditional:
if time_taken > 100: print("you lose") else: print("there is still time")
Try comparing numbers
Add to the code below so that the
is_young function returns
True when its
age, is less than 18, and
False otherwise. You should use the
Less than or equal to
a < b checks if
a is strictly smaller than
If you instead want to check if
a is less than or equal to
b, you use the
<= symbols, pronounced "less than or equal to".
In other words, these conditionals are equivalent:
if age < 18: print("no entry") if age <= 17: print("no entry")
Greater than or equal to
Similarly, you can use
>= to check if a variable is greater than or equal to
Change the code below so that it uses
>= instead of of the
You have learned earlier that
== checks if two values are equal.
You have also learned that
not() will negate a Boolean condition.
So you could write:
if not(password == "1234"): deny_access()
!= (read "is not equal to") are a shorter way to accomplish the
same thing. So you could also write:
if password != "1234": deny_access()
Try using not equals
Change the code below to use
!= without changing what the program does.