- What logical operators are ā tools to combine boolean conditions
- AND Operator (and) ā True only if both conditions are True
- OR Operator (or) ā True if at least one condition is True
- NOT Operator (not) ā reverses the boolean value
- Truth Tables ā visualize logical operator behavior
- Operator Precedence ā order of evaluation
- Real-world examples ā user authentication, eligibility checks
- Common mistakes ā and how to avoid them
What are Logical Operators?
Logical operators are used to combine conditional statements. They work with boolean values (True and False) and return a boolean result. Python provides three logical operators: and, or, and not.
š” Key insight: Logical operators are like the "glue" that connects multiple conditions together. They let you create complex decision-making logic with simple, readable code.
Think of logical operators as question combiners. Instead of asking one question at a time, you can combine multiple questions: "Is the user logged in AND has permission?" or "Is the user an admin OR a moderator?"
1. AND Operator (and)
The AND operator (and) returns True only if both conditions are True. If any condition is False, the result is False.
# AND Operator Examples print(True and True) # Output: True print(True and False) # Output: False print(False and True) # Output: False print(False and False) # Output: False # With variables age = 25 has_license = True can_drive = age >= 18 and has_license print(can_drive) # Output: True # Short-circuit evaluation x = 5 print(x > 0 and x < 10) # Output: True
2. OR Operator (or)
The OR operator (or) returns True if at least one condition is True. It only returns False when both conditions are False.
# OR Operator Examples print(True or True) # Output: True print(True or False) # Output: True print(False or True) # Output: True print(False or False) # Output: False # With variables has_cash = False has_card = True can_pay = has_cash or has_card print(can_pay) # Output: True # Short-circuit evaluation x = 5 print(x < 0 or x > 0) # Output: True
3. NOT Operator (not)
The NOT operator (not) reverses the boolean value. It returns True if the condition is False, and False if the condition is True.
# NOT Operator Examples print(not True) # Output: False print(not False) # Output: True # With variables is_weekend = False is_workday = not is_weekend print(is_workday) # Output: True # Combining with other operators age = 15 can_vote = age >= 18 cannot_vote = not can_vote print(cannot_vote) # Output: True
4. Truth Tables
Truth tables help visualize how logical operators work. Here are the truth tables for AND, OR, and NOT:
AND (and)
| Condition 1 | Condition 2 | Result |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
OR (or)
| Condition 1 | Condition 2 | Result |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
NOT (not)
| Condition | Result |
|---|---|
| True | False |
| False | True |
5. Operator Precedence
When combining multiple logical operators, Python follows a specific order of evaluation:
Precedence (highest to lowest): 1. not (highest) 2. and 3. or (lowest) # Examples result = True or False and False # and is evaluated first: False and False = False # then or: True or False = True print(result) # Output: True # Use parentheses for clarity result = (True or False) and False print(result) # Output: False
6. Real-World Examples
š User Authentication
# User Authentication Example
is_logged_in = True
has_permission = True
is_admin = False
# Check if user can access dashboard
can_access = is_logged_in and (has_permission or is_admin)
print(f"Can access dashboard: {can_access}") # Output: True
š Grade Eligibility Check
# Grade Eligibility Example
score = 85
attendance = 90
has_passed = score >= 75 and attendance >= 80
print(f"Student passed: {has_passed}") # Output: True
š Discount Eligibility
# Discount Eligibility Example
total = 150
is_member = True
has_coupon = False
# Get discount if total > 100 AND (is_member OR has_coupon)
eligible = total > 100 and (is_member or has_coupon)
print(f"Eligible for discount: {eligible}") # Output: True
7. Common Mistakes to Avoid
ā Mistake 1: Using & instead of and
& is a bitwise operator, not a logical operator. Always use and, or, not for boolean logic.
# WRONG if x > 0 & x < 10: # & is bitwise AND, not logical AND # CORRECT if x > 0 and x < 10: # Use 'and' for logical AND
ā Mistake 2: Misunderstanding operator precedence
not has higher precedence than and, which has higher precedence than or. Use parentheses to avoid confusion.
# WRONG (unclear precedence)
if not x > 5 or y < 10:
pass
# CORRECT (using parentheses)
if not (x > 5 or y < 10):
pass
ā Mistake 3: Comparing with non-boolean values
Python treats certain values as False (0, None, empty strings, empty lists). Be careful when using them with logical operators.
# Truthy and Falsy values
print(0 and 5) # Output: 0 (first Falsy value)
print(5 and 0) # Output: 0 (second Falsy value)
print(5 and 10) # Output: 10 (last value)
# Always use explicit comparisons for clarity
if x is not None and x > 0:
pass
Try It Yourself!
Experiment with logical operators directly in your browser. Modify the code and see the results in real time.
LOGICAL OPERATORS
========================================
1. AND OPERATOR (and)
True and True: True
True and False: False
False and False: False
2. OR OPERATOR (or)
True or True: True
True or False: True
False or False: False
3. NOT OPERATOR (not)
not True: False
not False: True
4. REAL-WORLD EXAMPLE (Authentication)
Can access dashboard: True
ā Logical operators are essential for decision-making!
š You've Mastered Python Logical Operators!
You understand AND, OR, NOT operators, truth tables, and operator precedence. These are essential for building decision-making logic in Python!
Quick Quiz ā Test Your Knowledge
Let's see what you've learned about Python logical operators:
True and False?or operator return when one condition is True and one is False?not False?True or False and False?Frequently Asked Questions
š¤ What's the difference between and and &?
and is a logical operator used for boolean logic. & is a bitwise operator used for bit-level operations. Always use and for conditions like if x > 0 and x < 10.
š§ What is short-circuit evaluation?
and, if the first condition is False, the second isn't evaluated. For or, if the first condition is True, the second isn't evaluated.
š What are truthy and falsy values in Python?
False in boolean contexts: None, 0, empty strings (""), empty lists ([]), empty dictionaries ({}). Everything else is truthy (True).
š Can I use logical operators with non-boolean values?
and returns the first falsy value or the last truthy value. or returns the first truthy value or the last falsy value. not always returns a boolean.
ā” What is the difference between or and |?
or is a logical operator for boolean logic. | is a bitwise OR operator for bit-level operations. Use or for conditions and | for bit manipulation.
šÆ How do I combine multiple logical operators?
if (age >= 18 and has_license) or is_admin: is much clearer than if age >= 18 and has_license or is_admin.
š Where to Go From Here
Now that you understand Python logical operators, here are some related topics to explore:
š Relational Operators
Compare values with >, <, ==, !=
šÆ Decision Making
Use conditions to control program flow
š If Statements
Make decisions with if, elif, else