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:
not(first)and(second)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")
๐ฌ Comments (0)
No comments yet
Be the first to share your opinion about this article!