Stewdent.eu
▾

Logical Operators

Logical operators (and, or, not) combine Boolean expressions to create more powerful conditions. They are essential for writing programs that can handle complex real-world rules.

Last updated: 9 December 2025

★★☆☆☆ Level 2computingprogrammingconditionalsbooleanlogical operators4 min read

Report an error

Simple comparisons can only check one thing at a time. But real decisions often involve multiple conditions — a rollercoaster might need you to be both tall enough and old enough. To combine conditions in Python, you use logical operators: and, or, and not.

The Three Logical Operators

OperatorMeaningResult is True when...
andBoth conditionsBoth sides are True
orEither conditionAt least one side is True
notOppositeThe condition is False

The and Operator

and requires both conditions to be True for the overall result to be True:

age = 15
height = 140

if age >= 12 and height >= 130:
    print("You can ride!")
else:
    print("Sorry, you cannot ride.")

If either condition is False, the whole expression is False and the person cannot ride.

Truth table for and:

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

The or Operator

or only requires one condition to be True:

day = "Saturday"

if day == "Saturday" or day == "Sunday":
    print("Weekend! No school.")
else:
    print("Weekday. Time for school.")

Truth table for or:

ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse
Note

or is inclusive — it is True if one or both sides are True. In everyday English "or" sometimes implies "but not both", but in programming or always means "at least one".

The not Operator

not flips a Boolean value:

is_logged_in = False

if not is_logged_in:
    print("Please log in.")

not False is True, so the message displays. not True would be False.

Tip

not is useful for making conditions read more naturally. if not is_empty: is clearer than if is_empty == False:.

Combining Multiple Operators

You can combine and, or, and not in the same expression:

score = 85
is_member = True

if score >= 80 and is_member or score >= 95:
    print("Prize awarded!")
Warning

Be careful with operator precedence: not binds most tightly, then and, then or. Use brackets to make your intentions explicit and avoid subtle bugs: if (score >= 80 and is_member) or score >= 95: is much clearer.

A Practical Example

username = input("Username: ")
password = input("Password: ")

valid_user = username == "admin"
valid_pass = password == "secure99"

if valid_user and valid_pass:
    print("Access granted.")
elif valid_user and not valid_pass:
    print("Incorrect password.")
else:
    print("Unknown username.")

Practice Problems

What does this expression evaluate to? (5 > 3) and (10 < 8)

Show answer
False. The first part (5 > 3) is True, but the second part (10 < 8) is False. True and False evaluates to False.

Write a condition using logical operators that checks whether a number n is between 1 and 100 inclusive.

Show answer
n >= 1 and n <= 100. Both conditions must be True for the number to be in the valid range, so and is the correct operator.