๐Ÿ“ Python

Logical Operators in Python: and, or, not ๐Ÿ”—

0
Author
04e5cc8b-58ac-4bdc-bdee-661bbb
๐Ÿ“…
Published
30.03.2026
โฑ๏ธ
Reading time
6 min
๐Ÿ‘๏ธ
Views
98
๐ŸŒฑ
Level
Beginner

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

Imagine: you can ride the roller coaster only if age >= 10 AND height >= 140
Or: it’s a day off if Saturday OR Sunday

Logical operators let you combine conditions!

๐Ÿค The AND operator โ€” both must be true

and = “AND” = all conditions must be True

Syntax:

if condition1 and condition2:
    code runs only if BOTH are True

Example:

age = 17
has_license = True

if age >= 16 and has_license:
    print("You can ride a scooter! ๐Ÿ›ต")
else:
    print("Not yet")

What happens:
- age >= 16 โ†’ 17 >= 16 โ†’ True โœ…
- has_license โ†’ True โœ…
- True and True โ†’ True โ†’ code runs!

Change age = 15:
- 15 >= 16 โ†’ False โŒ
- True and False โ†’ False โ†’ code does NOT run

Real-world example โ€” movie access:

age = int(input("Age: "))
has_ticket = input("Do you have a ticket? (yes/no): ") == "yes"

if age >= 16 and has_ticket:
    print("๐ŸŽฌ Enjoy the movie!")
else:
    if age < 16:
        print("โŒ Film is rated 16+")
    elif not has_ticket:
        print("โŒ You need a ticket")

More than two conditions:

age = 18
has_license = True
has_car = True

if age >= 18 and has_license and has_car:
    print("๐Ÿš— You're good to go!")
else:
    print("โŒ Conditions not met")

All three must be True!

๐Ÿ’ซ The OR operator โ€” at least one must be true

or = “OR” = one True is enough

Syntax:

if condition1 or condition2:
    code runs if AT LEAST ONE is True

Example:

day = input("What day is it? ")

if day == "Saturday" or day == "Sunday":
    print("๐ŸŽ‰ Weekend!")
else:
    print("๐Ÿ“š Weekday")

What happens:
- “Saturday” โ†’ first is True โ†’ result is True
- “Sunday” โ†’ second is True โ†’ result is True
- “Monday” โ†’ both False โ†’ result is False

Access control:

role = "admin"

if role == "admin" or role == "moderator":
    print("โœ… Admin panel access granted")
else:
    print("โŒ Access denied")

Multiple or clauses:

grade = input("Grade: ")

if grade == "A" or grade == "B" or grade == "C":
    print("โœ… Passed!")
else:
    print("โŒ Not passed")

Any grade of A, B, or C โ†’ passed

๐Ÿ”„ The NOT operator โ€” negation (inversion)

not = “NOT” = flips True to False and vice versa

Syntax:

if not condition:
    code runs if the condition is False

Example:

is_raining = False

if not is_raining:
    print("โ˜€๏ธ Go for a walk!")
else:
    print("โ˜” Stay inside")

What happens:
- is_raining โ†’ False
- not False โ†’ True
- Code runs!

With variables:

is_logged_in = False

if not is_logged_in:
    print("โŒ Please log in")
else:
    print("โœ… Welcome!")

NOT with comparison operators:

age = 15

if not age >= 18:  # Same as: age < 18
    print("You are under 18")

Though it’s cleaner to just write:

if age < 18:
    print("You are under 18")

๐Ÿ“Š Truth tables

AND โ€” all must be True:

True  and True   = True   # Only this case yields True!
True  and False  = False
False and True   = False
False and False  = False

Rule of thumb: Any single False โ†’ result is False

OR โ€” at least one True:

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 of thumb: Any single True โ†’ result is True

NOT โ€” flips the value:

not True  = False
not False = True

๐ŸŽฏ Combining operators

You can mix and, or, and not!

Example โ€” amusement park entry:

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

# Entry allowed if:
# (age >= 10 AND height >= 140) OR age >= 16

if (age >= 10 and height >= 140) or age >= 16:
    print("๐ŸŽข Welcome aboard!")
else:
    print("โŒ Sorry, entry not allowed")

Let’s trace some cases:
1. Age 12, height 145: (12โ‰ฅ10 and 145โ‰ฅ140) โ†’ True โ†’ allowed โœ…
2. Age 9, height 145: (9โ‰ฅ10 and 145โ‰ฅ140) โ†’ False, 9โ‰ฅ16 โ†’ False โ†’ not allowed โŒ
3. Age 17, height 130: (17โ‰ฅ10 and 130โ‰ฅ140) โ†’ False, but 17โ‰ฅ16 โ†’ True โ†’ allowed! โœ…

Complex example โ€” VIP access:

age = 20
is_member = True
has_discount = False

# VIP if:
# (age >= 18 AND member) OR (NOT has_discount AND age >= 21)

if (age >= 18 and is_member) or (not has_discount and age >= 21):
    print("โญ VIP access!")
else:
    print("๐ŸŽซ Standard access")

Use parentheses for clarity!

๐Ÿ“ Operator precedence

When multiple operators are used together:

  1. not first
  2. Then and
  3. Then or

Example:

x = True
y = False
z = True

result = not x and y or z
# Evaluated as:
# 1. not x โ†’ not True โ†’ False
# 2. False and y โ†’ False and False โ†’ False
# 3. False or z โ†’ False or True โ†’ True
# Result: True

Tip: Use parentheses to make the intent clear!

result = ((not x) and y) or z  # Much clearer!

๐Ÿ’ก Real-world examples

1. Password validation:

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")

2. Season detection:

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)

3. Basic email validation:

email = input("Email: ")

has_at = "@" in email
has_dot = "." in email
not_empty = len(email) > 0

if has_at and has_dot and not_empty:
    print("โœ… Email looks valid")
else:
    print("โŒ Invalid email")

4. Game โ€” can the player attack?

health = 80
mana = 30
has_weapon = True

# Attack is possible if:
# health > 20 AND (mana >= 10 OR has_weapon)

if health > 20 and (mana >= 10 or has_weapon):
    print("โš”๏ธ Attack!")
else:
    print("๐Ÿ›ก๏ธ Defend...")

โš ๏ธ Common mistakes

1. Forgetting to repeat the variable:

# โŒ Wrong:
if age >= 10 and >= 18:
    print("OK")

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

# โœ… Even better (Python idiom):
if 10 <= age <= 18:
    print("OK")

2. Using = instead of ==:

# โŒ Assignment!
if age = 18 and has_ticket:
    print("OK")

# โœ… Comparison:
if age == 18 and has_ticket:
    print("OK")

3. Confusing and vs or:

# Task: allow entry if EITHER age >= 18 OR has_parents

# โŒ Wrong (requires BOTH):
if age >= 18 and has_parents:
    print("OK")

# โœ… Correct (either one is enough):
if age >= 18 or has_parents:
    print("OK")

4. Forgetting the keyword:

# โŒ Missing 'and':
if age >= 10 height >= 140:
    print("OK")

# โœ… Correct:
if age >= 10 and height >= 140:
    print("OK")

๐ŸŽฎ Practice exercises

Easy level:

1. Range check
Is the number between 10 and 20 (inclusive)?

2. Day off
Is the day “Saturday” or “Sunday”?

3. NOT even
Is the number NOT even? (use not)

Medium level:

4. Exam eligibility
Eligible if: (attendance > 80% AND all homework submitted) OR has an individual plan

5. Discount
20% off if: (purchase >= 5000 AND has loyalty card) OR purchase >= 10000

6. Allowed to drive
Allowed if: age >= 18 AND has license AND NOT license revoked

Hard level:

7. Course access
Access if: (student AND paid) OR (teacher) OR (admin AND NOT blocked)

8. Triangle type
Given three side lengths, determine: equilateral / isosceles / scalene

๐Ÿš€ Summary

You’ve learned to:

โœ… Use and to require ALL conditions
โœ… Use or when ONE condition is enough
โœ… Use not to invert a condition
โœ… Combine operators
โœ… Understand operator precedence
โœ… Avoid common mistakes

With logical operators, your programs become much smarter! ๐Ÿง 


Practice in CodeHS! Build programs with different combinations of conditions. Experiment! ๐Ÿ’ช

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

๐Ÿ“

Setting Up Your Environment: Python, pip, and VS โ€ฆ

Before writing code locally, you need to set up three tools: Python, pip, and VS...

๐Ÿ“… 04.06.2026 ๐Ÿ‘๏ธ 17
๐Ÿ“

The datetime Module: Working with Dates and Times

datetime is Python's standard module for working with dates and times. It's part of the...

๐Ÿ“… 08.05.2026 ๐Ÿ‘๏ธ 67
๐Ÿ“

.env Files and Environment Variables: Keeping Secโ€ฆ

Imagine you wrote a program with an API key hardcoded in the source and pushed...

๐Ÿ“… 08.05.2026 ๐Ÿ‘๏ธ 76

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!