Skip to content

Python Logical Operators

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 andand, oror, and notnot, 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 TrueTrue or FalseFalse. 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
andandReturns TrueTrue if both statements are truex < 5 and x < 10x < 5 and x < 10
ororReturns TrueTrue if one of the statements is truex < 5 or x < 4x < 5 or x < 4
notnotReverse the result, returns FalseFalse if the result is truenot(x < 5 and x < 10)not(x < 5 and x < 10)

and Operator

andand Operator

The andand operator returns TrueTrue if both operands are TrueTrue. Otherwise, it returns FalseFalse. The following example demonstrates how to use the andand 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)
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
command
C:\Users\Your Name> python operators.py
True
False

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

or Operator

oror Operator

The oror operator returns TrueTrue if one of the operands is TrueTrue. Otherwise, it returns FalseFalse. The following example demonstrates how to use the oror 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)
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
command
C:\Users\Your Name> python operators.py
True
False

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

not Operator

notnot Operator

The notnot operator returns TrueTrue if the operand is FalseFalse. Otherwise, it returns FalseFalse. The following example demonstrates how to use the notnot 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)
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
command
C:\Users\Your Name> python operators.py
True
True

In the above example, we have used the notnot operator to reverse the result of the andand operator. Since the result of the andand operator is FalseFalse, the result of the notnot operator is TrueTrue. The result of the notnot operator is then assigned to the variable zz. The value of zz is then printed to the console.

Short-Circuiting

andand and oror short-circuiting

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 TrueTrue. 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)
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
command
C:\Users\Your Name> python operators.py
False
False

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

Combining Logical Operators

andand and oror operators combined

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)
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
command
C:\Users\Your Name> python operators.py
False
True

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

Conclusion

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!

Was this page helpful?

Let us know how we did