What is Boolean Logic?
In Python, Boolean logic is the foundation of decision-making. Every condition evaluates to either True or False. These values come from:
- Direct Boolean literals:
True,False - Comparison operations:
5 > 3→True - Logical combinations:
age >= 18 and has_id
Boolean expressions power if statements, but understanding them in isolation builds strong logic skills.
Logical Operators
Logical operators are the building blocks of decision-making in programming. They allow you to combine or invert conditions to model real-world rules like:
- “You can drive if you are 18 and have a license.”
- “The alarm rings if it’s morning or there’s an emergency.”
- “Access is denied if not authenticated.”
Python provides three logical operators: and, or, and not. Their behavior is defined by truth tables.
Complete Truth Tables
| A | B | A and B |
A or B |
|---|---|---|---|
True |
True |
True |
True |
True |
False |
False |
True |
False |
True |
False |
True |
False |
False |
False |
False |
NOT Operator (Unary – Works on One Value)
| A | not A |
|---|---|
True |
False |
False |
True |
How They Work in Code
These operators return True or False based on the truth values of the expressions.
# Example 1: AND – Both must be true
age = 20
has_license = True
can_drive = age >= 18 and has_license
print(can_drive) # True# Example 2: OR – At least one true
is_weekend = True
is_holiday = False
can_rest = is_weekend or is_holiday
print(can_rest) # True# Example 3: NOT – Inverts the condition
logged_in = False
access_denied = not logged_in
print(access_denied) # True# Example 4: Combined with input
temp = float(input("Temperature: "))
is_cold = temp < 10
is_raining = input("Raining? (y/n): ") == "y"
stay_indoors = is_cold and is_raining
print("Stay indoors:", stay_indoors)Why It Matters
Without logical operators, programs could only check one condition at a time. With them, you can express complex rules clearly and concisely—making your code smarter, safer, and more realistic.
10 Powerful Boolean Logic Examples
Each example uses only variables, input/output, and operators—no loops or functions.
age = int(input("Age: "))
has_consent = input("Parental consent? (yes/no): ") == "yes"
allowed = age >= 13 or has_consent
print("Access granted:", allowed)username = input("Username: ")
password = input("Password: ")
is_valid = username == "admin" and password == "secret123"
print("Login successful:", is_valid)temp = float(input("Temperature (°C): "))
is_extreme = temp < 0 or temp > 40
print("Extreme weather warning:", is_extreme)score = int(input("Score: "))
bonus = input("Has bonus? (y/n): ") == "y"
passed = score >= 50 or bonus
print("Student passed:", passed)day = input("Day of week: ")
is_weekend = day == "Saturday" or day == "Sunday"
is_weekday = not is_weekend
print("It's a weekday:", is_weekday)email = input("Email: ")
has_at = "@" in email
has_dot = "." in email
valid_format = has_at and has_dot
print("Basic format valid:", valid_format)total = float(input("Total purchase: "))
is_member = input("Member? (y/n): ") == "y"
eligible = total > 100 and is_member
print("Discount eligible:", eligible)hour = int(input("Current hour (0-23): "))
is_night = hour < 6 or hour > 22
print("Quiet hours active:", is_night)password_ok = input("Password correct? (y/n): ") == "y"
otp_ok = input("OTP correct? (y/n): ") == "y"
access_granted = password_ok and otp_ok
print("Access granted:", access_granted)num = int(input("Enter a number: "))
is_positive_nonzero = num > 0 # implies not zero
print("Positive and non-zero:", is_positive_nonzero)Key Takeaways
- Boolean logic turns real-world rules into code.
and→ both must be true.or→ at least one must be true.not→ flips the truth value.- Always test edge cases (e.g., 0, empty string, boundary values).