Python if, elif and else Statements with Real Examples for Beginners
P5.4 Python and, or, not: Combine Conditions
Why. Rules often have two parts: "premium AND order above $50", "weekend OR holiday".
What.
and— True only if both sides are True.or— True if at least one side is True.not— flips True to False and back.
How.
Combine checks
- Write each simple condition.
- Join them with
and/or. - Use brackets when you mix them.
Worked example A — Free delivery rule.
Step 1 — What we do. A shop in London gives free delivery to members with orders over £30.
Step 2 — Type and run this code.
is_member = True
total = 42
if is_member and total > 30:
print("Free delivery")
else:
print("Delivery fee £3.99")
Step 3 — Output you should see.
Free delivery
Step 4 — What it means. Both are True, so delivery is free.
Worked example B — Weekend or holiday.
Step 1 — What we do. A hospital adds extra staff on weekends or holidays.
Step 2 — Type and run this code.
day = "Sunday"
is_holiday = False
if day in ("Saturday", "Sunday") or is_holiday:
print("Extra staff needed")
Step 3 — Output you should see.
Extra staff needed
Step 4 — What it means. One side is True, so or is True.
Worked example C — Using not.
Step 1 — What we do. We send a reminder when an invoice is not paid.
Step 2 — Type and run this code.
is_paid = False
if not is_paid:
print("Send payment reminder")
Step 3 — Output you should see.
Send payment reminder
Step 4 — What it means. not False is True, so the reminder goes.
Ravindra Bagale's Tip
In pandas you will write &, | and ~ instead of and, or, not, with brackets around each part. The logic is the same.
Ravindra Bagale's Tip – मराठी
pandas मध्ये and, or, not ऐवजी &, | आणि ~ लिहाल, प्रत्येक भागाभोवती brackets ठेवून. Logic तेच आहे.
Ravindra Bagale's Tip – हिंदी
pandas में and, or, not की जगह &, | और ~ लिखोगे, हर हिस्से के आसपास brackets रखकर. Logic वही है.