← Intro to programming with Python

Combining conditions: and, or, not

Ask two questions at once.

Start this lesson

Real decisions often depend on more than one thing. "You can borrow the car if you have a licence AND it is before 10 pm." Python has three words for this. and is True only when BOTH sides are True. or is True when AT LEAST ONE side is True. not flips True to False and back. They let you write one clear condition instead of several nested ifs.

has_ticket = True
age = 15
if has_ticket and age >= 13:
    print("Enjoy the movie")
day = "Sunday"
if day == "Saturday" or day == "Sunday":
    print("Weekend!")
if not has_ticket:
    print("Please buy a ticket")

Both parts of the first condition are True, so it prints Enjoy the movie. Sunday matches the second part of the or, so it prints Weekend! The last message does not show, because not True is False.

Common mistake: writing day == "Saturday" or "Sunday". Python reads the right side, "Sunday", on its own, and a non-empty string counts as True, so this condition is ALWAYS True. Repeat the full comparison on both sides.

You can check a range neatly: 13 <= age <= 19 means age is from 13 to 19.