Skip to content

Python Logical Operators

Navigating Logic: Understanding Logical Operators in Python

Section titled “Navigating Logic: Understanding Logical Operators in Python”

Logical operators in Python provide the tools for evaluating and combining Boolean values, allowing you to make decisions based on multiple conditions. These operators, including and, or, and not, play a crucial role in controlling the flow of your programs and creating dynamic, responsive code. In this comprehensive guide, we’ll explore the realm of logical operators, their syntax, and how they empower expressive Python programming.

::: tip Logical operators are used to evaluate Boolean values and return a Boolean result. It always return True or False. They are often used in conjunction with comparison operators to create complex conditions. It is important to note that logical operators are short-circuiting, meaning that they stop evaluating as soon as they reach a result. :::

The following table lists the logical operators in Python:

OperatorDescriptionExample
andReturns True if both statements are truex < 5 and x < 10
orReturns True if one of the statements is truex < 5 or x < 4
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)

The and operator returns True if both operands are True. Otherwise, it returns False. The following example demonstrates how to use the and operator in Python:

operators.py
# and operator
x = 10
y = 5
z = x < 10 and y > 1
t = x < 10 and y < 1
print(z)
print(t)

Output:

command
C:\Users\Your Name> python operators.py
True
False

In the above example, we have used the and operator to combine two conditions. Since both conditions are True, the result of the and operator is True. The result of the and operator is then assigned to the variable z. The value of z is then printed to the console.

The or operator returns True if one of the operands is True. Otherwise, it returns False. The following example demonstrates how to use the or operator in Python:

operators.py
# or operator
x = 10
y = 5
z = x < 10 or y < 1
t = x > 10 or y < 1
print(z)
print(t)

Output:

command
C:\Users\Your Name> python operators.py
True
False

In the above example, we have used the or operator to combine two conditions. Since one of the conditions is True, the result of the or operator is True. The result of the or operator is then assigned to the variable z. The value of z is then printed to the console.

The not operator returns True if the operand is False. Otherwise, it returns False. The following example demonstrates how to use the not operator in Python:

operators.py
# not operator
x = 10
y = 5
z = not(x < 10 and y < 1)
t = not(x > 10 and y < 1)
print(z)
print(t)

Output:

command
C:\Users\Your Name> python operators.py
True
True

In the above example, we have used the not operator to reverse the result of the and operator. Since the result of the and operator is False, the result of the not operator is True. The result of the not operator is then assigned to the variable z. The value of z is then printed to the console.

Logical operators in Python are short-circuiting, meaning that they stop evaluating as soon as they reach a result. This is useful when you want to check multiple conditions and stop evaluating as soon as one of them is True. The following example demonstrates how to use short-circuiting in Python:

operators.py
# Short-circuiting
x = 10
y = 5
z = x < 10 and y < 1
t = x > 10 and y < 1
print(z)
print(t)

Output:

command
C:\Users\Your Name> python operators.py
False
False

In the above example, we have used the and operator to combine two conditions. Since the first condition is False, the result of the and operator is False. The result of the and operator is then assigned to the variable z. The value of z is then printed to the console.

Logical operators can be combined to create complex conditions. The following example demonstrates how to combine logical operators in Python:

operators.py
# Combining logical operators
x = 10
y = 5
z = x < 10 and y < 1 or x > 10 and y < 1
t = x < 10 and y < 1 or x > 10 and y > 1
print(z)
print(t)

Output:

command
C:\Users\Your Name> python operators.py
False
True

In the above example, we have combined the and and or operators to create complex conditions. Since one of the conditions is True, the result of the or operator is True. The result of the or operator is then assigned to the variable z. The value of z is then printed to the console.

Python is lazy with and / or: it stops as soon as the answer is certain. and stops at the first False (the whole thing is already False); or stops at the first True. Everything after the stopping point is never evaluated — which is why x is not None and x.value is safe:

sketch Short-circuit evaluation p5.js
and stops at the first False; or stops at the first True — the rest is never evaluated.

Logical operators in Python are essential tools for creating flexible and responsive code. Whether you’re combining conditions, controlling the flow of your programs, or making decisions based on multiple criteria, logical operators provide the means to navigate the complexities of Boolean values.

As you continue your Python journey, experiment with different combinations of logical operators, explore their applications in real-world scenarios, and use them to create dynamic and expressive code. For more insights and practical examples, check out our tutorials on Python Central Hub!


pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading