Skip to content

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.

Beginner 14 min read·Lesson 53 of 306 in Python Programming

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

ABA and BA or Bnot A
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue

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

Text
if condition_a and condition_b:
if condition_a or condition_b:
if not condition:
  • and requires both.
  • or requires at least one.
  • not inverts one condition.

Code examples

Combining conditions

Python
age = 20
has_id = True
print(age >= 18 and has_id)
print(age < 13 or age > 60)
print(not has_id)
Output
True
False
False

Operators return operands

Python
name = ""
print(name or "Guest")
print(0 and 5, 3 and 5)
Output
Guest
0 5

More examples

Free delivery rules with short-circuiting

Python
def free_delivery(total, is_member, promo_active):
return (total >= 999 and is_member) or promo_active
cases = [(1200, True, False), (1200, False, False), (300, False, True)]
for total, member, promo in cases:
print(total, member, promo, "->", free_delivery(total, member, promo))
orders = []
print("first order large:", bool(orders) and orders[0] > 500)
Output
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:

TextOutput
1200 True False -> True
1200 False False -> False
300 False True -> True
first order large: False

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

if colour == "red" or "blue":

Prefer

if colour == "red" or colour == "blue":

"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

if a or b and c:

Prefer

if (a or b) and c:

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

  1. 1
    Write a condition that is True for a weekend day that is not a holiday, using variables is_weekend and is_holiday.
  2. 2
    Use 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?
Practise this topic