Python Logical Operators Complete Tutorial

In this section, we will learn what logical operators are and how to use them in Python.

Note: we’re assuming you already familiar with operators in general as well as the comparison operators.

What is Logical Operators in Python?

The logical operators are used to combine the result of multiple boolean operations and decide what the final result should be!

An example of boolean operations we can point to comparison operators that always return either the value True or False.

Note: in short, the operands of the logical operators should be a boolean value. Now how this value is coming that depends on the design of the program. For example, this value could be the result of calling a function or an expression that returns a boolean value, etc.

This is mainly useful when the execution of a block of instructions depends on the result of more than just one condition! In a situation like this, we can use logical operators to combine these boolean results and get the final result.

For example, let’s say there’s an if statement that its condition depends on two comparisons (The two comparisons should result True so that the body of the if statement gets executed). In such case, we can involve the logical operator to combine the result of the two comparisons and then if both of them resulted True, then the final result becomes True as well!

List of Logical operators in Python

Here’s the list of logical operators.

Operator Description
and Using this operator, we can combine the result of two expressions and decide whether the final result should be True or False.
or Using this operator, we can combine the result of two boolean expressions and decide whether the final result should be True or False
not Using this operator, we can negate the result of an expression that results either True or False

Python and Operator:

The `and` operator takes two boolean expressions as its left and right operand. If both of them resulted True then the final result will be True as well.

Otherwise, if even one of the operands resulted False, then the final result will be False as well.

Python and Operator:

Left-boolean-expression and Right-boolean-expression

Python and Operator Example:

a = 20
b = 30
c = 40
d = 50
if a<b and c <d:
    print("The variable a and b are less than c and d respectively")

Output:

The variable a and b are less than c and d respectively

As you can see, the condition in the `if` statement is the combination of two comparison operations! Here, we’ve combined these two comparisons using the `and` operator.

Now here because the result of both operations is True then the final result of the `and` operator will be True as well.

But remember, if either of these two comparison operations resulted False then the final value of the `and` operator would be False as well.

Python or Operator:

The `or` operator is used to check the result of two boolean expressions (expressions that result either True or False). Now if either of these boolean expressions resulted True, then the final result of this operator will be True.

But if the entire expressions resulted False, then the final result will be False as well.

Basically, this operator looks for only one expression to become True!

Python or Operator:

Left-boolean-expression or Right-boolean-expression

Python or Operator Example:

a = 20
b = 30
c = 40
d = 50
if a>b or c <d:
    print("This is the body of the if statement")

Output:

This is the body of the if statement

Note that in the condition of the `if` statement, the first expression resulted False (because the value of the `a` variable is less than the value of the `b` variable. But the result of the second comparison is True. So here because we have one True result, then the final result will be True as well. That’s why the body of the `if` statement ran.

Python not Operator (AKA Reverse the Result Operator):

The `not` operator is used to reverse the result of a boolean expression. This means if the expression resulted False, the `not` operator will turn that into True and if the result of an expression was True then the `not` operator turns it into False.

Python not Operator:

not expression

We put the boolean expression on the right side `not` operator.

Python not Operator Example:

a = 20
b = 30
c = 40
d = 50
if not a>b :
    print("This is the body of the if statement")

Output:

This is the body of the if statement

Note that the comparison in the `if` statement resulted False. But because we’ve used the `not` operator, then the final result became True (it reversed the result).

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies