Lesson 7 · 7 of 20
Comparisons and logic · Python
Compare values in Python with ==, !=, < and >=, and combine conditions with and, or and not to decide who rides the roller coaster.

Comparison operators answer True or False: == (equal), != (not equal), <, >, <= and >=. Note that comparing uses two signs, ==; a single = stores a value.
and needs both conditions to be true: the girl is 1.42 m tall and has a ticket, so can_ride is True. or is happy with one: the park is open because it is a holiday, even though it is not the weekend. not flips the result.
Python lets you chain comparisons as in maths: 13 <= age <= 19 checks in one go whether the age is between 13 and 19.
Text can be compared too, letter by letter and case-sensitive: "Ana" == "ana" is False.
At the end it all comes together in an if: age < 18 and not has_ticket fails because she does have a ticket, so Python moves on to elif age < 18, which is true.
Code · main.py
# Comparisons give True or False.age = 16height = 1.42has_ticket = Trueprint("age == 16:", age == 16)print("age != 18:", age != 18)print("age >= 18:", age >= 18)print("height < 1.40:", height < 1.40)# and: both must be true. or: at least one. not: the opposite.can_ride = height >= 1.40 and has_ticketprint("Can ride the roller coaster:", can_ride)weekend = Falseholiday = Trueprint("Park is open:", weekend or holiday)print("Park is closed:", not (weekend or holiday))# Python lets you chain comparisons.print("Teenager:", 13 <= age <= 19)# Text can be compared too (letter by letter).print("Same name:", "Ana" == "ana")if age < 18 and not has_ticket: print("Ask an adult to buy you a ticket.")elif age < 18: print("Enjoy the park, and stay with your group!")else: print("Enjoy the park!")Output · python3 main.py
age == 16: True age != 18: True age >= 18: False height < 1.40: False Can ride the roller coaster: True Park is open: True Park is closed: False Teenager: True Same name: False Enjoy the park, and stay with your group!
Key points
==compares;=stores.and: both true.or: at least one.not: the opposite.- Parentheses make the order clear:
not (weekend or holiday). a <= x <= bchecks whetherxlies betweenaandb.
Exercise
Add a with_adult variable and change the rule so that children between 1.30 and 1.40 m can also ride when they come with an adult.