PythiloLesson 5 of 30 · progress kept in this browser

Comparisons and booleans

A comparison produces a bool, which is either True or False. A bool is an ordinary value: you can store it in a name, return it or print it.

age = 17
is_adult = age >= 18
print(is_adult)            # False
print(3 == 3.0, 3 != 4)    # True True
print("apple" < "banana")  # True
print("Zebra" < "apple")   # True
print(True + True)         # 2
print(sum([True, False, True]))  # 2
  • == and != test equality. <, <=, > and >= test order.
  • Strings compare character by character, by code point, so every capital letter sorts before every lowercase one. Compare a.lower() with b.lower() when case should not matter.
  • bool is a kind of int: True counts as 1 and False as 0, so sum over bools counts the true ones.
  • 1 < "2" raises TypeError, while 1 == "1" is simply False.
Your task

Write is_teen(age) returning True for ages 13 to 19 inclusive and False otherwise. Write count_passing(scores, pass_mark) returning how many scores are at least pass_mark.

Next: Branching