Logical Operators in Python: Master “And,” “Or,” & “Not”

Logical operators in Python — and, or, and not — are fundamental tools for building complex conditional statements and controlling the flow of your programs. Understanding these operators is essential for writing clean, efficient, and expressive code. In this guide, we’ll break down each operator, provide practical examples, and demonstrate how to use them to create robust decision-making logic.

1. The and Operator: When Both Must Be True

The and operator returns True only if both operands are True. Otherwise, it returns False.

is_raining = True
is_sunny = False

if is_raining and is_sunny:
    print("We might see a rainbow")  # This won't print

Truth Table:

Operand 1Operand 2Result
TRUETRUETRUE
TRUEFALSEFALSE
FALSETRUEFALSE
FALSEFALSEFALSE

2. The or Operator: When At Least One Must Be True

The or operator returns True if at least one of the operands is True. It returns False only if both operands are False.

if is_raining or is_sunny:
    print("It might be rainy or it might be sunny")  # This will print

Truth Table:

Operand 1Operand 2Result
TRUETRUETRUE
TRUEFALSETRUE
FALSETRUETRUE
FALSEFALSEFALSE

3. The not Operator: Reversing the Truth

The not operator is a unary operator (it takes a single operand). It simply reverses the truth value of its operand.

is_adult = False

if not is_adult:
    print("You are not an adult") # This will print

Truth Table:

OperandResult
TRUEFALSE
FALSETRUE

4. Combining Logical Operators: Building Complex Conditions

You can combine multiple logical operators to create more sophisticated conditions:

if (is_raining and not is_sunny) or (not is_raining and is_sunny):
    print("Either it's raining and not sunny, or it's sunny and not raining")

Note: Parentheses are used to group conditions and control the order of evaluation.

5. Practical Example: Filtering with Logical Operators

ages = [12, 18, 39, 87, 7, 2]

for age in ages:
    is_adult = age > 17
    if not is_adult:
        print(f"Being {age} does not make you an adult.")

This example uses not to filter and print ages that are not considered adult.

Frequently Asked Questions (FAQ)

1. What is the order of precedence for logical operators in Python?

The order of precedence is not (highest), then and, then or (lowest).

2. Can I use logical operators with values other than booleans?

Yes, Python has a concept called “truthiness,” where non-boolean values are treated as either True or False in certain contexts (e.g., empty lists are False, non-empty lists are True).

3. How do I create more complex logical expressions in Python?

Combine multiple logical operators with parentheses to group conditions and control the order of evaluation. You can also use comparison operators (e.g., ==, !=, >, <) to build more elaborate expressions.

4. What are some common mistakes to avoid when using logical operators?

1. Forgetting parentheses and relying on the default precedence, which might not be what you intend.
2. Confusing the and and or operators.
3. Overusing the not operator, which can sometimes make expressions less readable.