Using Logical Operators in
Python
Master and, or, and not to write smarter code
Logical operators let Python evaluate multiple conditions
simultaneously4just like making real decisions:
and ³ both must be true
or ³ at least one true
not ³ reverses the result
The and Operator
Example: School Entry Time
Students can enter only between 7:00 and 8:00 AM
time = int(input("Enter time (24h): "))
if time >= 7 and time <= 8:
print("Welcome! You're on time.")
else:
print("You are late.")
The and operator checks both time boundaries
The or Operator
Example: Youtube Permission Check
Watch if 18+ OR have parent permission
age = int(input("Age: "))
permission = input("Parent OK? (yes/no): ")
if age >= 18 or permission == "yes":
print("You can watch.")
else:
print("Content restricted.")
The or operator offers two valid paths
The not Operator
Example: WiFi Connection Check
Connect only if not already connected and password is correct
password = input("Enter WiFi password: ")
connected = input("Already connected? (yes/no): ")
if password == "magis2025" and not connected == "yes":
print("Connecting to WiFi...")
else:
print("Cannot connect.")
Password Check not connected
Must match credentials Prevents duplicate connection
Example: Login Authentication
Both username and password must be correct
username = input("Enter username: ")
password = input("Enter password: ")
if username == "student" and password == "12345":
print("Login successful!")
else:
print("Wrong credentials.")
This pattern appears in every app you use daily
Homework Challenge
Build a door access that checks:
Card state
1
Must be not blocked
The conditions must be true for "Access granted"
Otherwise: "Access denied"