and, or, not
Combine conditions, with a movie-ticket price example.
🎬 Video version coming soon — read it now
Real decisions often depend on more than one thing. Python combines conditions with three words.
- and: True only if both sides are True
- or: True if at least one side is True
- not: flips True to False and back
Example: movie ticket prices
A theatre in Jagadamba Junction charges ₹100 for children and seniors, ₹80 for anyone on a Tuesday, and ₹200 otherwise.
age = int(input("Age: "))
day = input("Day: ").strip().lower()
if day == "tuesday":
price = 80
elif age < 12 or age >= 60:
price = 100
else:
price = 200
print(f"Ticket: ₹{price}")and: both must hold
has_ticket = True
has_id = False
if has_ticket and has_id:
print("Enter")
else:
print("Please show both ticket and ID")not: the opposite
is_raining = False
if not is_raining:
print("Let's go to the beach")Range checks the Python way
marks = 72
if 60 <= marks < 75:
print("First class")Python lets you chain comparisons like maths, which reads much better than marks >= 60 and marks < 75.
A common trap
# Wrong: always True!
if day == "saturday" or "sunday":
print("Weekend")
# Right
if day == "saturday" or day == "sunday":
print("Weekend")
# Even better
if day in ("saturday", "sunday"):
print("Weekend")The wrong version asks: is day == "saturday", or is "sunday" True? A non-empty string is always True, so it prints "Weekend" every day.
Tip: When a condition gets long, store parts of it in well-named variables:
is_weekend = ..., then if is_weekend and not is_raining:.In one line
and needs both, or needs one, not flips the answer, and each side of an or must be a full comparison.
Saved on this device. Sign in to keep progress everywhere.
Share with a friend