beginnerPython
What are logical operators in Python?
Answer
Clear, interview-ready explanation
Logical operators combine or reverse conditions. Python provides three logical operators:
-
: Returns a truthy result when both conditions are truthy.and -
: Returns a truthy result when at least one condition is truthy.or -
: Reverses the truth value of a condition.not
age = 25
has_id = True
if age >= 18 and has_id:
print("Access granted")
Logical operators use short-circuit evaluation:
-
stops when it finds a falsy operand.and -
stops when it finds a truthy operand.or
In Python, and and return one of their operands rather than always returning a Boolean value.
or