Comparison & Logical Operators
Part of the Python Roadmap
In short
"Is the user over 18 and logged in?" "Is the cart empty or the payment failed?" Real decisions combine several checks, and Python lets you write them almost exactly as you'd say them.
Comparison operators (==, !=, <, >, <=, >=) produce booleans, and logical operators (and, or, not) combine them. Python also allows chained comparisons like 18 <= age < 60 and short-circuits and/or.

Think of it like this
The club bouncer
The bouncer checks: are you 18 or older AND on the guest list? If the age check fails, he doesn't bother looking at the list. At the next door the rule is: VIP pass OR booked table. If you show a VIP pass, he lets you in without checking bookings. That is exactly how and and or short-circuit.
Watch it happen
Checking age >= 18 and has_ticket and seat_free
1/5Three conditions joined with and. All must be True for the whole thing to be True.
The big ideas
What you need to know
Six comparisons
==, !=, <, >, <=, >= all return True or False. Remember == compares, while = assigns.
Chained comparisons
18 <= age < 60 works just like in maths. It means 18 <= age and age < 60, but reads more clearly.
and, or, not
and needs both sides true, or needs at least one, not flips a value. Precedence is not, then and, then or, so use brackets when mixing.
Short-circuiting
and stops at the first False, or stops at the first True. So len(cart) > 0 and cart[0] == "milk" never crashes on an empty cart.
See it in code
Walk through the code
1age = 222has_id = True3city = "Chennai"4 5print(18 <= age < 60) # True6print(age >= 18 and has_id) # True7print(city == "Delhi" or city == "Chennai") # True8print(not has_id) # False9print("apple" < "banana") # True10 11cart = []12print(len(cart) > 0 and cart[0] == "milk") # False, no crash1/5A chained comparison: is age between 18 (included) and 60 (excluded)?
Comparisons give booleans, logical operators combine them, and short-circuiting protects the last line.
Save yourself the pain
Common mistakes
Oops
Writing if city == "Delhi" or "Mumbai": and finding it is always true.
Do this instead
Each side needs its own comparison: city == "Delhi" or city == "Mumbai", or better, city in ("Delhi", "Mumbai").
Oops
Using = inside a condition, like if age = 18:
Do this instead
= assigns, == compares. Python raises a SyntaxError for the first one.
Oops
Mixing and and or without brackets and getting surprising results.
Do this instead
Add brackets to show intent: (is_admin or is_owner) and is_active.
Your mission
Write the eligibility rules
Quick check
What does True or (1 / 0 > 1) evaluate to?
Go deeper
- 1Write a chained comparison that checks if marks are between 40 and 100.
- 2Combine two conditions with and, then with or, and predict the results first.
- 3Learn the difference between == (same value) and is (same object).
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.