๐Ÿ“ Python

Logical Operators in Python: and, or, not

P
Author
PyLand Team
๐Ÿ“…
Published
30.03.2026
โฑ๏ธ
Reading time
2 min
๐Ÿ‘๏ธ
Views
387
๐ŸŒฑ
Level
Beginner

One condition is fine. But what if you need to check multiple conditions at once?

  • Allowed on the ride: age >= 10 AND height >= 140
  • Day off: Saturday OR Sunday
  • Access denied: NOT authorized

Logical operators let you combine conditions.


AND โ€” all conditions must be True

if condition1 and condition2:
    # runs only if BOTH are True
    pass
age = 17
has_license = True

if age >= 16 and has_license:
    print("You can ride a scooter!")
else:
    print("Not yet")
# Output: You can ride a scooter!

Truth table for AND:

True  and True   โ†’ True    # only this case gives True
True  and False  โ†’ False
False and True   โ†’ False
False and False  โ†’ False

Rule: at least one False โ†’ result is False.


OR โ€” one True is enough

if condition1 or condition2:
    # runs if AT LEAST ONE is True
    pass
day = "Saturday"

if day == "Saturday" or day == "Sunday":
    print("Day off!")
else:
    print("Weekday")
# Output: Day off!

Truth table for OR:

True  or True   โ†’ True
True  or False  โ†’ True     # any True โ†’ result is True
False or True   โ†’ True
False or False  โ†’ False    # only both False โ†’ result is False

Rule: at least one True โ†’ result is True.


NOT โ€” inversion

if not condition:
    # runs if condition is False
    pass
is_raining = False

if not is_raining:
    print("Go for a walk!")
else:
    print("Stay home")
# Output: Go for a walk!
not True  โ†’ False
not False โ†’ True

Operator Precedence

When mixing operators, the order of evaluation is:

  1. not (first)
  2. and (second)
  3. or (third)
x = True
y = False
z = True

result = not x and y or z
# Step 1: not x   โ†’ False
# Step 2: False and y โ†’ False
# Step 3: False or z  โ†’ True
print(result)  # True

Use parentheses for clarity:

result = ((not x) and y) or z  # Explicit and clear

Combining Operators

age = int(input("Age: "))
height = int(input("Height (cm): "))

# Allowed if: (age >= 10 AND height >= 140) OR age >= 16
if (age >= 10 and height >= 140) or age >= 16:
    print("Come in!")
else:
    print("Sorry, not allowed")

Breakdown:
- Age 12, height 145: (True and True) or False โ†’ True โ€” allowed
- Age 9, height 145: (False and True) or False โ†’ False โ€” not allowed
- Age 17, height 130: (True and False) or True โ†’ True โ€” allowed


Practical Examples

Password Check

password = input("Password: ")
length_ok = len(password) >= 8
has_digit = any(char.isdigit() for char in password)

if length_ok and has_digit:
    print("Strong password")
else:
    if not length_ok:
        print("Password too short (need >= 8 characters)")
    if not has_digit:
        print("Password must contain a digit")

Determining the Season

month = int(input("Month (1-12): "))

if month == 12 or month == 1 or month == 2:
    season = "Winter"
elif month >= 3 and month <= 5:
    season = "Spring"
elif month >= 6 and month <= 8:
    season = "Summer"
elif month >= 9 and month <= 11:
    season = "Autumn"
else:
    season = "Invalid month"

print(season)

Common Mistake: Missing Variable Name

# โŒ WRONG โ€” syntax error
if age >= 10 and >= 18:
    print("OK")

# โœ… CORRECT
if age >= 10 and age <= 18:
    print("OK")

# โœ… Even better โ€” Python supports chained comparisons
if 10 <= age <= 18:
    print("OK")

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Math Operations in Python ๐Ÿงฎ

Python is a great calculator! Let's cover all the arithmetic operators with examples.

๐Ÿ“… 30.03.2026 ๐Ÿ‘๏ธ 417
๐Ÿ“

if __name__ == "__main__": the Python entry point

The if name == "main" guard defines a Python program's entry point: it distinguishes direct...

๐Ÿ“… 14.08.2026 ๐Ÿ‘๏ธ 44
๐Ÿ“

strip() and lower(): Preparing User Text ๐Ÿงน

A user may enter the correct word with extra spaces or different letter case. Python...

๐Ÿ“… 11.08.2026 ๐Ÿ‘๏ธ 50
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Python from Scratch: Build 7 Practical Projects Open course curriculum