Module 4 · Operators — topic 10 of 16
Logical operators
Combine conditions with and, or and not. Learn their truth tables, short-circuit evaluation, how they return operand values, and how to write clear compound conditions.
Learning objectives
- Apply and, or and not with their truth tables
- Explain short-circuit evaluation
- Write readable compound conditions
Definition
Combine conditions with and, or and not. Learn their truth tables, short-circuit evaluation, how they return operand values, and how to write clear compound conditions.
Introduction
Real decisions usually depend on more than one condition: a customer gets free delivery if the order is large and they are a member, or if there is a promotion running. Logical operators combine simple conditions into compound ones.
Why it matters
Writing correct compound conditions is essential for business rules, validation and game logic. Understanding short-circuiting also lets you write safe checks, such as testing that a list isn’t empty before reading its first item.
Detailed explanation
and is True only when both sides are true. or is True when at least one side is true. not reverses a single condition.
Short-circuiting: and stops at the first false operand and or stops at the first true one, without evaluating the rest. items and items[0] > 0 is safe because items[0] is never read when items is empty.
and and or actually return one of their operands, not necessarily True or False: "" or "Guest" returns "Guest". This is used for simple defaults.
Precedence: not binds tightest, then and, then or. a or b and c means a or (b and c). Use parentheses to make mixed conditions clear.
De Morgan’s laws help simplify negations: not (a and b) equals (not a) or (not b), and not (a or b) equals (not a) and (not b).
Syntax
- and requires both.
- or requires at least one.
- not inverts one condition.
Code examples
Combining conditions
True False False
Operators return operands
Guest 0 5
More examples
Free delivery rules with short-circuiting
1200 True False -> True 1200 False False -> False 300 False True -> True first order large: False
Step-by-step explanation
- The parentheses make the rule explicit: (large order and member) or promotion.
- Each tuple in cases is unpacked into three named values.
- The third customer qualifies through the promotion alone.
- bool(orders) is False for an empty list, so and stops before orders[0], avoiding an IndexError.
Expected output
Running “Free delivery rules with short-circuiting” prints exactly:
Practical applications
- Access control checks combine roles and permissions with and/or.
- Search filters combine several criteria such as price and rating.
- Games test collisions and states with compound conditions every frame.
Best practices
- Use parentheses whenever and and or are mixed.
- Put cheap or safety checks first to benefit from short-circuiting.
- Extract long conditions into named Boolean variables.
Common mistakes
Writing English-style comparisons
Avoid
Prefer
"blue" on its own is always truthy, so the first version is always True. Repeat the comparison or use colour in ("red", "blue").
Mixing and/or without parentheses
Avoid
Prefer
and binds tighter than or; add parentheses to state the intended grouping.
Key takeaways
- and needs both true; or needs one; not inverts.
- Evaluation short-circuits as soon as the result is known.
- and/or return an operand; not always returns a Boolean.
Try it yourself
- 1Write a condition that is True for a weekend day that is not a holiday, using variables is_weekend and is_holiday.
- 2Use or to give a default city of "Unknown" when the user enters nothing.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?