The Three Logical Operators

Truth Table

True  and True   # True
True  and False  # False
True  or  False  # True
False or  False  # False
not True         # False

Short-Circuit Evaluation

Python stops as soon as the answer is known. False and X skips X. Useful for guarding against None:

if user is not None and user.is_active:
    allow()

Combining With Comparison

if age >= 13 and age <= 19:
    print("teenager")

Readable Alternatives

if 13 <= age <= 19:
    print("teenager")
if role in ("admin", "editor"):
    print("can edit")

Practise this on PyForm — free

PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.

Open PyForm →