Home › Learn › Python › Making decisions

Comparisons and booleans

Lesson 6 of 40. Ask questions about values and combine the answers.

Comparisons produce True or False: == equal, != not equal, <, >, <=, >=. Note the double equals; a single = assigns. Python allows chains like 13 <= age <= 19, which reads the way it looks.

and, or, and not combine booleans. a and b is true only when both are; a or b when at least one is; not a flips it. A boolean can be stored in a variable like any other value, which keeps conditions readable: name the question, then use the name.

temperature = 31
is_hot = temperature > 28
is_weekend = False
print(is_hot and not is_weekend)   # True

Your task

Using age and has_id from the starter, make is_adult (18 or older), can_enter (an adult who has an id), and is_teen (13 to 19 inclusive). Do not type True or False yourself; compute them.

Hint 1

is_adult = age >= 18

Hint 2

A chained comparison works: 13 <= age <= 19.

Show the solution
age = 20
has_id = True

is_adult = age >= 18
can_enter = is_adult and has_id
is_teen = 13 <= age <= 19

    Runs CPython compiled to WebAssembly in this tab (12 MB download the first time, then cached); nothing is sent anywhere.