Skip to content

Python Boolean operation

  • by

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:

OperationDescriptionExample
andReturns True if both operands are TrueTrue and True
True and False
False and False
orReturns True if at least one operand is TrueTrue or True
True or False
False or False
notReturns the opposite of the operand’s valuenot 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:

Python Boolean operation

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.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading