Home » Python Logical Operators : A Tutorial with Examples

Python Logical Operators : A Tutorial with Examples

Oracle Java Certification
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Programmer I [1Z0-815] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]
Spring Framework Basics Video Course
1 Year Subscription

Logical operators in Python are used to combine multiple conditions (or expressions) and return a boolean value (True or False).

These operators are essential when working with decision-making statements (e.g., if, while) where you need to evaluate more than one condition at the same time.

In this tutorial, we will cover:

  1. What are Logical Operators?
  2. Types of Logical Operators
  3. Using and Operator
  4. Using or Operator
  5. Using not Operator
  6. Combining Logical Operators
  7. Short-Circuit Evaluation
  8. Examples and Use Cases for Logical Operators

Let’s explore each concept with explanations and examples.

1. What are Logical Operators?

Logical operators are used to perform logical operations on boolean values (expressions that evaluate to either True or False). These operators allow you to combine multiple conditions or invert the result of a condition.

The three logical operators in Python are:

  1. and: Returns True if both conditions are True.
  2. or: Returns True if at least one condition is True.
  3. not: Inverts the boolean value (returns True if the condition is False and vice versa).

2. Types of Logical Operators

Here are the three logical operators in Python, along with their functions:

Operator Description Example
and Returns True if both expressions are True. a and b
or Returns True if at least one expression is True. a or b
not Inverts the boolean value of the expression. not a

3. Using and Operator

The and operator returns True only if both of the conditions on either side of the operator are True. If one or both conditions are False, the result will be False.

Syntax:

condition1 and condition2

Example:

a = 5
b = 10

# Both conditions are true
result = (a > 3) and (b > 8)
print(result)  # Output: True

# One condition is false
result = (a > 6) and (b > 8)
print(result)  # Output: False
  • In this example, the and operator evaluates both conditions:
    • a > 3 and b > 8 both evaluate to True, so the result is True.
    • a > 6 evaluates to False, so the overall result is False.

4. Using or Operator

The or operator returns True if at least one of the conditions on either side of the operator is True. If both conditions are False, the result will be False.

Syntax:

condition1 or condition2

Example:

a = 5
b = 10

# One condition is true
result = (a > 6) or (b > 8)
print(result)  # Output: True

# Both conditions are false
result = (a > 6) or (b < 8)
print(result)  # Output: False
  • In this example, the or operator checks whether at least one of the conditions is True:
    • The first condition, a > 6, is False, but b > 8 is True, so the result is True.
    • In the second case, both conditions are False, so the result is False.

5. Using not Operator

The not operator is used to invert the boolean value of a condition. If a condition evaluates to True, the not operator makes it False, and vice versa.

Syntax:

not condition

Example:

a = 5

# Inverting a True condition
result = not (a > 3)
print(result)  # Output: False

# Inverting a False condition
result = not (a > 6)
print(result)  # Output: True
  • In this example, the not operator inverts the boolean result:
    • a > 3 is True, so not (a > 3) becomes False.
    • a > 6 is False, so not (a > 6) becomes True.

6. Combining Logical Operators

You can combine multiple logical operators (and, or, not) to form complex conditions. Parentheses can be used to control the precedence of the operators.

Example: Combining and, or, and not

a = 5
b = 10
c = 3

# Combining multiple conditions with and, or, and not
result = (a > 3 and b > 8) or not (c > 4)
print(result)  # Output: True
  • In this example, the result is True because:
    • (a > 3 and b > 8) evaluates to True.
    • not (c > 4) evaluates to True since c > 4 is False, and not False is True.
    • The final result is True or True, which evaluates to True.

7. Short-Circuit Evaluation

Python uses short-circuit evaluation for logical operators, meaning that Python stops evaluating expressions as soon as the result is determined.

  • For the and operator, if the first condition is False, Python does not evaluate the second condition because the result will always be False.
  • For the or operator, if the first condition is True, Python does not evaluate the second condition because the result will always be True.

Example 1: Short-Circuiting with and

a = 5
b = 10

# First condition is False, so the second condition is not evaluated
result = (a > 6) and (b > 100)
print(result)  # Output: False
  • Since a > 6 is False, Python does not evaluate (b > 100).

Example 2: Short-Circuiting with or

a = 5
b = 10

# First condition is True, so the second condition is not evaluated
result = (a > 3) or (b < 5)
print(result)  # Output: True
  • Since a > 3 is True, Python does not evaluate (b < 5).

8. Examples and Use Cases for Logical Operators

Example 1: Checking Multiple Conditions in an if Statement

You can use logical operators in if statements to check multiple conditions.

age = 25
has_license = True

# Checking multiple conditions
if age >= 18 and has_license:
    print("You can drive.")
else:
    print("You cannot drive.")

Output:

You can drive.
  • In this example, the person can drive if they are at least 18 years old and have a driver's license.

Example 2: Default Value Using or

You can use the or operator to provide a default value when a variable is None or evaluates to False.

name = None

# Provide a default value if name is None or False
display_name = name or "Guest"
print(display_name)  # Output: Guest
  • In this example, since name is None, or returns “Guest” as the default value.

Example 3: Inverting a Condition with not

You can use not to invert conditions in loops or other control structures.

valid_password = False

# Keep asking for the password until it's valid
while not valid_password:
    password = input("Enter password: ")
    if password == "secure123":
        valid_password = True

print("Access granted.")
  • In this example, the loop continues to prompt the user for the password until the correct password is entered.

Summary of Python Logical Operators:

Operator Description Example
and Returns True if both expressions are True. condition1 and condition2
or Returns True if at least one expression is True. condition1 or condition2
not Inverts the boolean value of an expression. not condition
Short-circuit Stops evaluating once the result is determined. (a > 3) and (b > 5)

Conclusion

Logical operators in Python are essential for evaluating and combining multiple conditions in control flow structures like if, while, and for statements.

In this tutorial, we covered:

  • The three main logical operators: and, or, and not.
  • Examples demonstrating how to use each logical operator to combine conditions.
  • How to combine multiple logical operators for complex conditions.
  • Short-circuit evaluation, where Python stops evaluating once the result is known.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More