Chapter 2: Control Flow / Lesson 15

Logical Operators

Logical Operators

Logical operators allow you to combine multiple conditions in your if statements. Python provides three logical operators: and, or, and not. These operators help you create more complex conditional logic.

Understanding how these operators work is crucial for writing effective conditionals and making your programs more intelligent.

The AND Operator

The and operator returns True only if both conditions are true. If either condition is false, it returns False:

logical.py
age = 25 has_license = True if age >= 18 and has_license: print("Can drive") # Both conditions must be true print(True and True) # True print(True and False) # False print(False and False) # False

The OR Operator

The or operator returns True if at least one condition is true. It only returns False if both conditions are false:

or_operator.py
# Check if age qualifies for discount age = 10 if age < 13 or age > 65: print("Discount available") # At least one condition must be true print(True or False) # True print(False or True) # True print(False or False) # False

The NOT Operator

The not operator reverses the boolean value. It returns True if the condition is false, and False if the condition is true:

not_operator.py
has_license = False if not has_license: print("Get license") print(not True) # False print(not False) # True print(not 0) # True (0 is falsy) print(not "") # True (empty string is falsy)

Combining Multiple Operators

You can combine multiple logical operators. Python evaluates them based on operator precedence (not, then and, then or):

combining.py
age = 25 has_license = True has_insurance = False # Complex condition combining all operators if age >= 18 and has_license and not has_insurance: print("You can drive but need insurance!") # Using parentheses for clarity if (age >= 18 and has_license) or (age < 18 and not has_license): print("Special case")

Short-Circuit Evaluation

Python uses short-circuit evaluation with logical operators. This means it stops evaluating as soon as it knows the result:

💡 Short-Circuit Behavior

and: Stops if first condition is False (doesn't check second)

or: Stops if first condition is True (doesn't check second)

short_circuit.py
# Short-circuit with 'and' x = 0 if x != 0 and (10 / x) > 5: print("Safe division") # Since x != 0 is False, Python doesn't evaluate 10/x # Short-circuit with 'or' name = "Alice" result = name or "Unknown" print(result) # "Alice" - doesn't need to check "Unknown"

Truth Tables

Understanding truth tables helps you predict how logical operators work:

truth_tables.py
# AND Truth Table # True and True = True # True and False = False # False and True = False # False and False = False # OR Truth Table # True or True = True # True or False = True # False or True = True # False or False = False # NOT Truth Table # not True = False # not False = True

Practical Examples

Here are some real-world examples of logical operators in action:

practical.py
age = 25 income = 50000 has_credit_history = True # Loan eligibility: must meet ALL conditions if age >= 18 and income >= 30000 and has_credit_history: print("Eligible for loan") # Discount: qualifies if ANY condition is true if age < 18 or age > 65 or income < 20000: print("Discount available") # Using 'not' to check for empty or invalid data username = "" if not username: print("Username is required")
🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...