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 and
and
, or
or
, and not
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
True
or False
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:
Operator | Description | Example |
---|---|---|
and and | Returns True True if both statements are true | x < 5 and x < 10 x < 5 and x < 10 |
or or | Returns True True if one of the statements is true | x < 5 or x < 4 x < 5 or x < 4 |
not not | Reverse the result, returns False False if the result is true | not(x < 5 and x < 10) not(x < 5 and x < 10) |
and Operator
and
and
Operator
The and
and
operator returns True
True
if both operands are True
True
. Otherwise, it returns False
False
. The following example demonstrates how to use the and
and
operator in Python:
# and operator
x = 10
y = 5
z = x < 10 and y > 1
t = x < 10 and y < 1
print(z)
print(t)
# and operator
x = 10
y = 5
z = x < 10 and y > 1
t = x < 10 and y < 1
print(z)
print(t)
Output:
C:\Users\Your Name> python operators.py
True
False
C:\Users\Your Name> python operators.py
True
False
In the above example, we have used the and
and
operator to combine two conditions. Since both conditions are True
True
, the result of the and
and
operator is True
True
. The result of the and
and
operator is then assigned to the variable z
z
. The value of z
z
is then printed to the console.
or Operator
or
or
Operator
The or
or
operator returns True
True
if one of the operands is True
True
. Otherwise, it returns False
False
. The following example demonstrates how to use the or
or
operator in Python:
# or operator
x = 10
y = 5
z = x < 10 or y < 1
t = x > 10 or y < 1
print(z)
print(t)
# or operator
x = 10
y = 5
z = x < 10 or y < 1
t = x > 10 or y < 1
print(z)
print(t)
Output:
C:\Users\Your Name> python operators.py
True
False
C:\Users\Your Name> python operators.py
True
False
In the above example, we have used the or
or
operator to combine two conditions. Since one of the conditions is True
True
, the result of the or
or
operator is True
True
. The result of the or
or
operator is then assigned to the variable z
z
. The value of z
z
is then printed to the console.
not Operator
not
not
Operator
The not
not
operator returns True
True
if the operand is False
False
. Otherwise, it returns False
False
. The following example demonstrates how to use the not
not
operator in Python:
# not operator
x = 10
y = 5
z = not(x < 10 and y < 1)
t = not(x > 10 and y < 1)
print(z)
print(t)
# 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:
C:\Users\Your Name> python operators.py
True
True
C:\Users\Your Name> python operators.py
True
True
In the above example, we have used the not
not
operator to reverse the result of the and
and
operator. Since the result of the and
and
operator is False
False
, the result of the not
not
operator is True
True
. The result of the not
not
operator is then assigned to the variable z
z
. The value of z
z
is then printed to the console.
Short-Circuiting
and
and
and or
or
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 True
True
. The following example demonstrates how to use short-circuiting in Python:
# Short-circuiting
x = 10
y = 5
z = x < 10 and y < 1
t = x > 10 and y < 1
print(z)
print(t)
# Short-circuiting
x = 10
y = 5
z = x < 10 and y < 1
t = x > 10 and y < 1
print(z)
print(t)
Output:
C:\Users\Your Name> python operators.py
False
False
C:\Users\Your Name> python operators.py
False
False
In the above example, we have used the and
and
operator to combine two conditions. Since the first condition is False
False
, the result of the and
and
operator is False
False
. The result of the and
and
operator is then assigned to the variable z
z
. The value of z
z
is then printed to the console.
Combining Logical Operators
and
and
and or
or
operators combined
Logical operators can be combined to create complex conditions. The following example demonstrates how to combine logical operators in Python:
# 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)
# 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:
C:\Users\Your Name> python operators.py
False
True
C:\Users\Your Name> python operators.py
False
True
In the above example, we have combined the and
and
and or
or
operators to create complex conditions. Since one of the conditions is True
True
, the result of the or
or
operator is True
True
. The result of the or
or
operator is then assigned to the variable z
z
. The value of z
z
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