Combining Conditions with and, or and not

and is True only when both conditions are True, or is True when at least one is, not flips True and False, and in checks whether text appears inside other text, which together turn a real rule with several parts into one condition.

Apprentice · Python · Updated

By

After this lesson you can

  • Combine conditions with and, or and not, and tell whether the whole condition is True
  • Check whether text appears inside other text with in, and write each side of an and or an or as a whole comparison

Key takeaways

  • and gives True only when the conditions on both sides are True. or gives True when at least one of them is.
  • not flips an answer, so not True is False. Parentheses group the conditions you want worked out first.
  • in checks whether a piece of text appears inside another, such as "SPLASH" in code, and not in checks that it doesn't. Capital letters count.
  • Each side of an and or an or must be a whole comparison. day == "saturday" or "sunday" is always True, because "sunday" on its own counts as true.

Real rules rarely hang on one question. Free shipping might need an order over $50 and an address in the country. A sign-in works with the right password or a code from your phone. A sale runs on every day that is not a holiday. You'll write rules like these in every program that checks a form, a price or a permission, and Python has three small words for joining questions: and, or and not.

The example is Splash Valley Water Park again: who may ride the Big Splash slide alone, and which days cost more.

Both must be true: and

The Big Splash slide has a second rule: to ride without an adult, a guest must be at least 48 inches tall and at least 10 years old. Being tall enough isn't enough on its own.

height = 50
age = 9
if height >= 48 and age >= 10:
    print("You can ride Big Splash on your own")
else:
    print("Big Splash: ride with an adult")
Big Splash: ride with an adult

and joins two conditions into one, and the whole thing is True only when both sides are True. This guest passes the height check but not the age check, so the whole condition is False and the else runs. and, or and not are Python's logical operators: they work on True and False and answer True or False.

This table shows every combination. In your textbook it may be called a truth table:

height >= 48 age >= 10 and gives or gives
True True True True
True False False True
False True False True
False False False False

Either one will do: or

Weekend tickets cost $6 more, and "the weekend" means Saturday or Sunday. or gives True when at least one side is True:

day = "sunday"
if day == "saturday" or day == "sunday":
    print("Weekend price: $36")
else:
    print("Weekday price: $30")
Weekend price: $36

day == "saturday" is False and day == "sunday" is True, so the or is True. Only a day that is neither makes it False.

Flipping an answer: not

not turns True into False and False into True. It reads the way you'd say it: not is_weekend is "not the weekend".

day = "tuesday"
is_weekend = day == "saturday" or day == "sunday"
has_season_pass = False
print("Weekend:", is_weekend)
print("Weekday:", not is_weekend)
print("Pays at the gate:", not has_season_pass)
Weekend: False
Weekday: True
Pays at the gate: True

When one condition mixes and with or, Python works out not first, then and, then or. Parentheses decide the order yourself, as they do in math: has_season_pass and not (day == "saturday" or day == "sunday") asks about the whole weekend before the not flips it. A variable such as is_weekend does the same job and reads even more clearly.

Is it inside the text: in

in asks whether one piece of text appears anywhere inside another, read "is in". Every summer coupon for the park has SPLASH somewhere in its code:

code = "SUMMER-SPLASH-25"
print("SPLASH" in code)
print("WINTER" in code)
print("splash" in code)
print("WINTER" not in code)
True
False
False
True

not in, read "is not in", asks the opposite. Capital letters count here too, which is why the lowercase "splash" isn't found. Compare code.upper() when the guest might type the code in lowercase.

The mistake that is always True

This looks like the weekend check, and it runs, but it gives the wrong answer:

day = "tuesday"
if day == "saturday" or "sunday":
    print("Weekend price")
else:
    print("Weekday price")
Weekend price

Python doesn't read it as "day is Saturday or Sunday". It reads two separate pieces: day == "saturday", which is False, or the text "sunday" on its own. Text on its own isn't a question at all, and in a condition Python counts any text that isn't empty, and any number that isn't 0, as true. Programmers say those values are truthy. So the whole condition is always True, whatever the day. The same goes for age == 1 or 2.

The fix is to write each side of an and or an or as a whole comparison: day == "saturday" or day == "sunday". The opposite mix-up is day == "saturday" and day == "sunday", which is never True, because no day is both.

Do it: let season pass holders in free on weekdays

Splash Valley's season pass holders get in free on weekdays. On Saturdays and Sundays, everyone pays at the gate.

  • day: the day of the visit, in lowercase, such as "tuesday".
  • has_pass: True when the guest has a season pass, False when they don't.

The practice box can't wait for someone to type, so the starter gives you a function line, def show_entry(day, has_pass):, and the tests call it with several guests. Functions come in Module 2 (What a Function Is: def and Calling); for now, write your lines indented under that line, and use day and has_pass as variables that already hold one guest's values.

Your job: inside show_entry(day, has_pass), print exactly one line:

  • Free entry when the guest has a season pass and the day is not "saturday" or "sunday".
  • Pay at the gate in every other case: no season pass, or a Saturday or Sunday visit.

Worked through: with show_entry("wednesday", True), the guest has a pass and Wednesday isn't a weekend day, so it prints Free entry. With show_entry("saturday", True), the day is Saturday, so it prints Pay at the gate.

Practiced live on the lesson page: write the Python right there and check it.

Check yourself

2 questions to try on the lesson page.

So what

and, or and not turn a real rule, with all its parts, into one condition, as long as each side is a whole question of its own.

Key terms

Related