True and False, comparison operators, and/or/not, chained comparisons, and which values count as false.
Python · beginner · 7 min · 9 steps · free
age = 17
has_ticket = True
is_adult = age >= 18
can_enter = is_adult or has_ticket
needs_help = not has_ticket and age < 12
in_range = 13 <= age <= 19 # comparisons can be chained
empty_is_false = bool("") # empty things count as False
zero_is_false = bool(0)
print(is_adult, can_enter, needs_help, in_range, empty_is_false, zero_is_false)age is created with the value 17.has_ticket is created with the value true.or is True if either side is: False or True gives True.not has_ticket is False, so the whole and is False without even checking the age.13 <= age <= 19 for rangesTrue and not False?Answer: True. not False is True; True and True is True.
Answer: []. An empty list is false; a list with an item is true, even if the item is 0.
← f-strings: Putting Values into Text · if, elif and else →