Python Boolean operations are logical operations performed on Boolean values. They allow you to manipulate and combine Boolean values to perform logical evaluations and make decisions in your code.
Here’s an overview of the syntax for each Boolean operation:
1. Logical AND (and
):
operand1 and operand2
2. Logical OR (or
):
operand1 or operand2
3. Logical NOT (not
)
not operand
Here’s a tabular representation of the boolean operations in Python:
Operation | Description | Example |
---|---|---|
and | Returns True if both operands are True | True and True True and False False and False |
or | Returns True if at least one operand is True | True or True True or False False or False |
not | Returns the opposite of the operand’s value | not True not False |
Python boolean operation example
Simple example code.
# Logical AND
result = True and False
print(result) # Output: False
# Logical OR
result = True or False
print(result) # Output: True
# Logical NOT
result = not True
print(result) # Output: False
Output:
You can also combine multiple Boolean operations and use parentheses to control the order of evaluation:
result = (True or False) and (not False)
print(result) # Output: True
By using these Boolean operations, you can create complex logical expressions and conditions to guide the flow of your program.
Do comment if you have any doubts or suggestions on this Python Boolean topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.