Boolean Logic: and / or / not
Conditions are booleans: they are either True or False. You can combine them with three words.
andis true only when both sides are true.oris true when at least one side is true.notflips a boolean.
logged_in = True
is_admin = False
print(logged_in and is_admin) # False
print(logged_in or is_admin) # True
print(not is_admin) # TrueYou will often combine these with comparisons. Here a value is "in range" only when both checks pass:
age = 30
allowed = age >= 18 and age <= 65
print(allowed) # TruePython also lets you chain comparisons, which reads like math. This means exactly the same thing:
age = 30
allowed = 18 <= age <= 65
print(allowed) # TrueUse not to invert a whole check. not (a and b) is true when they are not both true.
Define a function can_ride(age, height_cm) that returns True only when the person is at least 12 years old and at least 140 cm tall, otherwise False. Use and (a chained comparison is fine too).
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.