Python > Core Python Basics > Basic Operators > Logical Operators (and, or, not)
Logical Operators in Python: A Comprehensive Guide
This guide provides a detailed overview of logical operators (and, or, not) in Python. Understanding these operators is fundamental for controlling the flow of execution in your programs based on multiple conditions.
Introduction to Logical Operators
Python uses three keywords for logical operations: and, or, and not. These operators are used to combine or modify boolean expressions, returning True or False based on the truth values of the operands.
The 'and' Operator
The and operator returns True if both operands are True. Otherwise, it returns False. In the first example, both x > 0 and y < 20 are true, so the result is True. In the second example, y > 20 is false, so the result is False.
x = 5
y = 10
result = (x > 0) and (y < 20)
print(result) # Output: True
result = (x > 0) and (y > 20)
print(result) # Output: False
The 'or' Operator
The or operator returns True if at least one of the operands is True. It returns False only if both operands are False. In the first example, y < 20 is true, so the result is True. In the second example, both x < 0 and y > 20 are false, so the result is False.
x = 5
y = 10
result = (x < 0) or (y < 20)
print(result) # Output: True
result = (x < 0) or (y > 20)
print(result) # Output: False
The 'not' Operator
The not operator is a unary operator that negates the truth value of its operand. If the operand is True, not returns False, and vice versa. In the first example, x > 0 is true, so not (x > 0) is False. In the second example, x < 0 is false, so not (x < 0) is True.
x = 5
result = not (x > 0)
print(result) # Output: False
result = not (x < 0)
print(result) # Output: True
Short-Circuit Evaluation
Python uses short-circuit evaluation for logical operators. This means that the second operand is only evaluated if necessary. For In the and, if the first operand is False, the second operand is not evaluated because the result will always be False. For or, if the first operand is True, the second operand is not evaluated because the result will always be True. This can be useful for optimizing performance and preventing errors.and example, because the first value is False the function some_function is never called. Similarly, in the or example, because the first value is True the function is never called.
def some_function():
print("Function called!")
return True
result = False and some_function()
print(result) # Output: False
result = True or some_function()
print(result) # Output: True
Real-Life Use Case
Logical operators are frequently used in conditional statements to make decisions based on multiple criteria. This example checks if a person is eligible to drive based on their age and whether they have a driver's license.
age = 25
has_license = True
if age >= 18 and has_license:
print("Eligible to drive.")
else:
print("Not eligible to drive.")
Best Practices
Interview Tip
Be prepared to explain how logical operators work, including short-circuit evaluation. You might be asked to evaluate complex boolean expressions or write code that uses logical operators to solve a problem.
When to Use Them
Use logical operators whenever you need to combine or modify boolean expressions to control the flow of execution in your program. They are essential for creating complex conditional logic and decision-making processes.
Memory Footprint
Logical operators themselves have minimal memory footprint. The primary memory usage comes from the operands (variables or expressions) involved in the logical operation. Using short-circuit evaluation can sometimes save memory by preventing unnecessary computations.
FAQ
-
What is the difference between 'and' and 'or'?
andreturnsTrueonly if both operands areTrue, whileorreturnsTrueif at least one operand isTrue. -
What does the 'not' operator do?
Thenotoperator negates the truth value of its operand. If the operand isTrue,notreturnsFalse, and vice versa. -
What is short-circuit evaluation?
Short-circuit evaluation means that the second operand of a logical operator is only evaluated if necessary. This can improve performance and prevent errors.