Python 3D Academyবাংলা

Combining conditions: and, or, not

🌍 In real life. A cinema lets you in if you have a ticket and you're old enough for the film: both must be true. A shop gives a discount if you're a student or over 60: one is enough. And "if it's not raining, let's walk" flips the question around.

Python has exactly these three words for joining conditions.

The idea in one sentence: and, or and not combine True/False answers into one answer.

Its words:

  • and is True when both sides are True
  • or is True when at least one side is True
  • not flips True to False, and False to True
  • short-circuiting: stopping as soon as the answer is known

Step by step, for age >= 13 and age <= 19 when age is 15:

  1. age >= 13 is True.
  2. and still needs the right side: age <= 19 is True.
  3. Both are True, so the whole condition is True.
age = 15
if age >= 13 and age <= 19:
    print("teenager")

With or, one True side is enough. Python is lazy in a clever way: once the left side is True, it doesn't even look at the right side.

day = "Sat"
if day == "Sat" or day == "Sun":
    print("weekend!")

not reads naturally in English:

raining = False
if not raining:
    print("Let's go outside")

Python also lets you chain comparisons: 13 <= age <= 19 means the same as age >= 13 and age <= 19. When you mix and and or, add brackets so the order is clear: (a or b) and c.

age = 15
print(13 <= age <= 19)
print((age < 5 or age > 60) and age != 0)

Python stops early. and stops at the first part that counts as False, and or stops at the first part that counts as True; the rest isn't even worked out. This is called short-circuiting, and it makes x != 0 and 10 / x > 1 safe when x is 0: the division never happens. One more surprise: and and or give back one of their parts, not always True or False. name or "guest" gives name if it counts as True (Lesson 5.4: an empty string counts as False), otherwise "guest".

x = 0
print(x != 0 and 10 / x > 1)
name = ""
print(name or "guest")
print("Rimi" or "guest")

Mistakes and edge cases.

  • Leaving out the second comparison. day == "Sat" or "Sun" is always True: Python reads it as (day == "Sat") or "Sun", and the string "Sun" counts as True. Write day == "Sat" or day == "Sun". See below.
  • A half comparison. age > 13 and < 19 gives SyntaxError: invalid syntax. Each side needs its own comparison, or chain them: 13 < age < 19.
  • Mixing and with or. and goes before or, so a or b and c means a or (b and c). Add brackets.
  • not reaches the whole comparison. not x == 5 means not (x == 5), which is the same as x != 5.
day = "Mon"
if day == "Sat" or "Sun":
    print("weekend!")
It prints weekend! on a Monday: the right side, "Sun", is a non-empty string, so it counts as True.

Quiz

  1. What does this print?

    print(True and False)
  2. What does this print?

    print(True or False)
  3. What does this print?

    print(not True)
  4. What does this print?

    x = 5
    print(x > 1 and x < 10)
  5. When is a or b False?

    • Only when both a and b are False
    • When either one is False
    • Never
  6. This prints weekend! for every day. Which line has the mistake?

    day = "Tue"
    if day == "Fri" or "Sat":
        print("weekend!")

Coding tasks

  1. Ask for an age. Print teen if it is from 13 to 19 (both included). Otherwise, print not a teen.

  2. Ask for a year. Print leap if it is a leap year, otherwise not leap.

    A year is a leap year if it divides by 4 but not by 100, or if it divides by 400.