Python Boolean Operators
Boolean operators in Python are used to perform logical operations on boolean values (True
and False
). These operators are essential for control flow and conditional statements in programming.
1. Logical AND (and
)
The and
operator returns True
if both operands are True
; otherwise, it returns False
.
Example:
Output:
Explanation:
- Both
a
andb
need to beTrue
for the result to beTrue
. Sinceb
isFalse
, the result isFalse
.
2. Logical OR (or
)
The or
operator returns True
if at least one of the operands is True
; otherwise, it returns False
.
Example:
Output:
Explanation:
- At least one of
a
orb
needs to beTrue
for the result to beTrue
. Sincea
isTrue
, the result isTrue
.
3. Logical NOT (not
)
The not
operator negates the boolean value of the operand. It returns True
if the operand is False
and False
if the operand is True
.
Example:
Output:
Explanation:
- The
not
operator inverts the value ofa
. Sincea
isTrue
,not a
isFalse
.
4. Boolean Expression Evaluation
Boolean operators can be used to evaluate complex conditions by combining multiple boolean expressions.
Example:
a = 5
b = 10
c = 15
result = (a < b) and (b < c)
print("Combined Logical AND:", result)
result = (a > b) or (b < c)
print("Combined Logical OR:", result)
result = not (a < b)
print("Logical NOT with Expression:", result)
Output:
Explanation:
(a < b)
isTrue
and(b < c)
isTrue
, soTrue and True
results inTrue
.(a > b)
isFalse
but(b < c)
isTrue
, soFalse or True
results inTrue
.not (a < b)
isnot True
, which results inFalse
.
5. Short-Circuit Evaluation
Python uses short-circuit evaluation for boolean expressions, meaning it stops evaluating as soon as the result is determined.
Example:
def func1():
print("func1 called")
return True
def func2():
print("func2 called")
return False
result = func1() or func2()
print("Result:", result)
Output:
Explanation:
func1()
returnsTrue
, sofunc2()
is not called due to short-circuit evaluation in theor
operation.
6. Boolean Conversion
Non-boolean values can be converted to boolean values using bool()
.
Example:
print("Boolean conversion of 0:", bool(0))
print("Boolean conversion of 1:", bool(1))
print("Boolean conversion of '':", bool(''))
print("Boolean conversion of 'Hello':", bool('Hello'))
print("Boolean conversion of []:", bool([]))
print("Boolean conversion of [1, 2, 3]:", bool([1, 2, 3]))
Output:
Boolean conversion of 0: False
Boolean conversion of 1: True
Boolean conversion of '': False
Boolean conversion of 'Hello': True
Boolean conversion of []: False
Boolean conversion of [1, 2, 3]: True
Explanation:
- Values like
0
, empty strings''
, and empty lists[]
are consideredFalse
. - Non-zero numbers, non-empty strings, and non-empty lists are considered
True
.
Conclusion
Boolean operators are fundamental in Python for making decisions and controlling the flow of a program. They allow for the evaluation of conditions and logical operations that are crucial for effective programming.
By understanding and practicing these operators, you can create more complex and efficient logical conditions in your code.