Combining with and/or

Concepts covered: Boolean Operators

Real decisions often depend on 2 facts at once. A discount applies when the customer is a student and today is Tuesday. A jacket is needed when it rains or the wind is strong. Python's boolean operators join conditions into one: and is True only when both sides are True, or is True when at least one side is True, and not flips a single boolean to its opposite.

leftrightleft and rightleft or right
TrueTrueTrueTrue
TrueFalseFalseTrue
FalseTrueFalseTrue
FalseFalseFalseFalse

The table has 4 rows because 2 booleans have 4 combinations. Read it with the same question each time: and asks whether both are True, and only the first row says yes. or asks whether either is True, and only the last row says no. not True is False, and not False is True.

is_student = True
day = "Tuesday"

if is_student and day == "Tuesday":
    print("Student Tuesday rate")

Python evaluates each side to a boolean first, then applies the operator. is_student is already True and day == "Tuesday" produces True. That makes True and True evaluate to True, so the block runs. Change day to "Friday" and the right side becomes False. Now True and False is False and the block is skipped.

Conditions are values

A combined condition is an expression like any other, so it can be stored in a variable and printed. The animation below builds 2 booleans and prints them before using one in an if together with not. Follow each comparison as it collapses to True or False.

age = 24
member = False
day = "Tuesday"

student_rate = age <= 25 and day == "Tuesday"
member_rate = member or day == "Tuesday"

print("Student rate:", student_rate)
print("Member rate:", member_rate)

if student_rate and not member:
    print("Charge 8")
else:
    print("Charge 12")
Student rate: True
Member rate: True
Charge 8
ExpressionStep 1Step 2Value
`age <= 25 and day == "Tuesday"``True and True`no further stepTrue
`member or day == "Tuesday"``False or True`no further stepTrue
`student_rate and not member``True and not False``True and True`True

Which operator applies first

When one condition holds both and and or, Python applies not first, then and, then or, the way multiplication happens before addition in arithmetic. Parentheses override that order, and they also make the intent visible to whoever reads the line next. The 2 prints below hold the same 3 variables and the same operators, and they disagree.

is_weekend = True
is_holiday = False
is_open = False

print(is_weekend or is_holiday and is_open)
print((is_weekend or is_holiday) and is_open)

Output:

True
False

In the first line Python groups is_holiday and is_open together to get False. What remains, True or False, is True. The parentheses in the second line make Python evaluate the or first and get True, and then True and is_open comes out False. Whenever and and or share a line, add the parentheses, even when the default order already does what you want.

Putting 3 conditions to work

Predict the output of the forecast check below before revealing it. The temperature is 22, it is not raining, and the wind is 35 km/h.

temperature = 22
raining = False
wind_kmh = 35

if temperature >= 18 and not raining:
    print("Good for a walk")

if raining or wind_kmh > 30:
    print("Bring a jacket")

if temperature >= 18 and raining:
    print("Warm rain")

print("Forecast done")

Output:

Good for a walk
Bring a jacket
Forecast done

The first condition is True and not False, which is True and True, so the walk line prints. The second is False or True, so the jacket line prints as well. The third is True and False, so nothing prints for warm rain. Each if decides on its own, which is why 2 of the 3 blocks ran.

Do
  • Write each side of and/or as a complete comparison
  • Add parentheses when and and or appear on one line
  • Name booleans so that a line like if raining or windy: reads as a sentence
Avoid
  • Write day == 6 or 7, which Python reads as (day == 6) or 7 and never as a second comparison
  • Rely on the default order to separate and from or
  • Compare a boolean to True, if raining == True:, when if raining: says the same

What does this program print?

username = "ana"
password_ok = True
locked = True

if password_ok and not locked:
    print("Welcome", username)
elif password_ok or not locked:
    print("Partial check passed")
else:
    print("Access denied")

Picking between and and or follows the wording of the rule. If every part must hold, use and. If any one part is enough, use or. A rule that says a parcel ships free when it is light or the buyer is a member needs or, because a heavy parcel bought by a member still qualifies.

About this lesson

This section is part of the Control Flow: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.