Logical Operators

basic
Published

April 8, 2024

Python’s logical operators are essential tools for controlling the flow of your programs and making decisions based on multiple conditions. Understanding how they work is important for writing efficient and readable code. This post will look into the three main logical operators: and, or, and not, providing clear explanations and practical examples.

The and Operator

The and operator returns True only if both operands are True. If either operand is False, the entire expression evaluates to False. Think of it as a requirement: all conditions must be met.

x = 10
y = 5

print(x > 5 and y < 10)  # Output: True

print(x < 0 and y > 0)  # Output: False

#Demonstrating with strings
print("hello" == "hello" and 5 == 5) #Output: True
print("hello" == "world" and 5 ==5) #Output: False

The or Operator

The or operator returns True if at least one of the operands is True. It only evaluates to False if both operands are False. It’s a more lenient condition; only one needs to be satisfied.

x = 10
y = 5

print(x > 5 or y > 10)  # Output: True

print(x < 0 or y < 0)  # Output: False

#Demonstrating with strings
print("hello" == "hello" or 5 == 6) #Output: True
print("hello" == "world" or 5 == 6) #Output: False

The not Operator

The not operator is a unary operator (it operates on a single operand). It inverts the truth value of its operand. If the operand is True, not makes it False, and vice-versa.

x = 10

print(not (x > 5))  # Output: False

print(not (x < 0))  # Output: True

#Demonstrating with boolean values
print(not True) # Output: False
print(not False) # Output: True

Combining Logical Operators

You can combine these operators to create complex conditional expressions. Remember to use parentheses to ensure the intended order of operations.

x = 10
y = 5
z = 20

print((x > y and x < z) or (y > 0 and z > 15)) # Output: True

This example demonstrates the power of combining logical operators to create complex conditional logic within your Python programs. Understanding the precedence of operators is important for correct evaluation. Parentheses help clarify the order and prevent unexpected results.