Logical Operators in Python with Examples: Comprehensive Guide

What is an Operator in Python?

Operators in Python are special symbols that perform arithmetic or logical computations. The value that the operator works on is called the “operand.”

What does the operator do in Python?

Operators in Python are used to manipulate data and perform various computations.

Python provides a wide range of operators that serve different purposes.

Different Types of Operators in Python

Here are the different types of operators in Python:

  • Logical Operators
  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Bitwise Operators
  • Assignment Operators
  • Membership Operators
  • Identity Operators

What are logical operators in Python?

The Python logical operators are and, or, and not. They are used to combine and manipulate boolean values.

OperatorMeaningExample
andIf both operands are true, then this is true.a and b
orTrue if one of the operands is true.a or b
notIf the operand is false, the condition is true (complements the operand)not a

Logical Operator Example:

a = True
b = False

print('a and b is',a and b)

print('a or b is',a or b)

print('not a is',not a)

Output:

a and b is False
a or b is True
not a is False

How to use logical operators in Python?

Here’s how to use logical operators based on the truth table for the logical operators “and”, “or”, and “not” in Python.

Logical AND (operator: and):

| Operand 1 | Operand 2 | Result |
|-----------|-----------|--------|
|   False   |   False   | False  |
|   False   |   True    | False  |
|   True    |   False   | False  |
|   True    |   True    | True   |

Logical OR (operator: or):

| Operand 1 | Operand 2 | Result |
|-----------|-----------|--------|
|   False   |   False   | False  |
|   False   |   True    | True   |
|   True    |   False   | True   |
|   True    |   True    | True   |

Logical NOT (operator: not):

| Operand | Result |
|---------|--------|
|  False  |  True  |
|  True   | False  |

These truth tables illustrate the output that each logical operator will produce based on the truth values of its operands.

Here are other operators in Python you can utilize.

Comparison Operators in Python

Value comparison is done using comparison operators. Depending on the criteria, it returns True or False.

OperatorMeaning
>Less than – True if the left operand is less than the right
<Greater than or equal to – True if the left operand is greater than or equal to the right
==Equal to – True if both operands are equal
!=Not equal to – True if operands are not equal
>=Less than or equal to – True if the left operand is less than or equal to the right
<=Less than or equal to – True if left operand is less than or equal to the right

Comparison Operator Example:

a = 10
b = 12

# Output: a > b is False
print('a > b is',a>b)

# Output: a < b is True
print('a < b is',a<b)

# Output: a == b is False
print('a == b is',a==b)

# Output: a != b is True
print('a != b is',a!=b)

# Output: a >= b is False
print('a >= b is',a>=b)

# Output: a <= b is True
print('a <= b is',a<=b)

Output:

a > b is False
a < b is True
a == b is False
a != b is True
a >= b is False
a <= b is True

Python Arithmetic Operators

Arithmetic operators are used in mathematics to accomplish operations such as addition, subtraction, multiplication, and so on.

OperatorMeaningExample
+Add two operands or unary plusa + b + 2
Subtract the right operand from the unary minus.a – b – 2
*Multiplies the values on either side of the operator.a * b
/Division Operators divide numbers and return a quotient.a / b
%Modulus – the remainder of the division of the left operand by the right operanda % b (remainder of a/b)
//Floor division – a division that returns a whole number moved to the left on the number linea // b
**Exponent – left operand raised to the power of righta**b (a to the power b)

Arithmetic Operator Example:

a = 15
b = 4

# Output: a + b = 19
print('a + b =',a+b)

# Output: a - b = 11
print('a - b =',a-b)

# Output: a * b = 60
print('a * b =',a*b)

# Output: a / b = 3.75
print('a / b =',a/b)

# Output: a // b = 3
print('a // b =',a//b)

# Output: a ** b = 50625
print('a ** b =',a**b)

Output:

a + b = 19
a - b = 11
a * b = 60
a / b = 3.75
a // b = 3
a ** b = 50625

Python Bitwise Operators

Bitwise operators in Python operate bit by bit and operate on bits.

For example a = 60 and b = 13.

In this case, their values in binary form would be a = 0011 1100 and b = 0000 1101, respectively.

OperatorMeaningExample
&Bitwise ANDa & b = 0 (0000 0000)
|Bitwise ORa | b = 14 (0000 1110)
~Bitwise NOT~a = -11 (1111 0101)
^Bitwise XORa ^ b = 14 (0000 1110)
>>Bitwise right shifta >> 2 = 2 (0000 0010)
<<Bitwise left shifta << 2 = 40 (0010 1000)

Bitwise Operator Example:

a = 60            # 60 = 0011 1100 
b = 13            # 13 = 0000 1101 
c = 0

c = a & b;        # 12 = 0000 1100
print ("Line 1 - Value of c is ", c)

c = a | b;        # 61 = 0011 1101 
print ("Line 2 - Value of c is ", c)

c = a ^ b;        # 49 = 0011 0001
print ("Line 3 - Value of c is ", c)

c = ~a;           # -61 = 1100 0011
print ("Line 4 - Value of c is ", c)

c = a << 2;       # 240 = 1111 0000
print ("Line 5 - Value of c is ", c)

c = a >> 2;       # 15 = 0000 1111
print ("Line 6 - Value of c is ", c)

Output:

Line 1 - Value of c is  12
Line 2 - Value of c is  61
Line 3 - Value of c is  49
Line 4 - Value of c is  -61
Line 5 - Value of c is  240
Line 6 - Value of c is  15

Assignment Operators in Python

In Python, assignment operators are used to give variable values.

For example, a += 5 adds 5 to a variable and then assigns it. It has the same meaning as a = a + 5.

OperatorExampleEquivalent to
=a = 5a = 5
+=a += 5a = a + 5
-=a -= 5a = a – 5
*=a *= 5a = a * 5
/=a /= 5a = a / 5
%=a %= 5a = a % 5
//=a //= 5a = a // 5
**=a **= 5a = a ** 5
&=a &= 5a = a & 5
|=a |= 5a = a | 5
^=a ^= 5a = a ^ 5
>>=a >>= 5a = a >> 5
<<=a <<= 5a = a << 5
Assignment Operators in Python

Assignment Operator Example:

a = 25
b = 5
c = 0

c = a + b
print ("Line 1 - Value of c is ", c)

c += a
print ("Line 2 - Value of c is ", c)

c *= a
print ("Line 3 - Value of c is ", c)

c /= a 
print ("Line 4 - Value of c is ", c)

c  = 2
c %= a
print ("Line 5 - Value of c is ", c)

c **= a
print ("Line 6 - Value of c is ", c)

c //= a
print ("Line 7 - Value of c is ", c)

Output:

Line 1 - Value of c is  30
Line 2 - Value of c is  55
Line 3 - Value of c is  1375
Line 4 - Value of c is  55.0
Line 5 - Value of c is  2
Line 6 - Value of c is  33554432
Line 7 - Value of c is  1342177

Membership Operators

The membership operators in Python are in and not in. They are used to determine if a value or variable can be found in a sequence (string, list, tuple, set, and dictionary).

In a dictionary, we can only check if the key is there, not if the value is there.

OperatorMeaningExample
inTrue if the value or variable is in the sequence.5 in a
not inTrue if value or variable is not found in the sequence5 not in a

Membership Operator Example:

a = 'Hello world'
b = {1:'a',2:'b'}

# Output: True
print('H' in a)

# Output: True
print('hello' not in a)

# Output: True
print(1 in b)

# Output: False
print('a' in b)

Output:

True
True
True
False

Here, ‘H’ is in a, but ‘hello’ is not in a. (remember, Python is case sensitive). In the same way, 1 is the key, and ‘a’ is the value in dictionary b. So, ‘a’ in b returns False.

Identity Operators

In Python, the identity operators are is and is not. They are used to see if two values (or variables) are located on the same part of memory.

Even if two variables are identical, this does not indicate that they represent the same object.

OperatorMeaningExample
isTrue if the operands are identical (refer to the same object)a is True
is notTrue if the operands are not identical (do not refer to the same object)a is not True

Identity Operator Example:

a1 = 5
b1 = 5
a2 = 'Hello'
b2 = 'Hello'
a3 = [1,2,3]
b3 = [1,2,3]

# Output: False
print(a1 is not b1)

# Output: True
print(a2 is b2)

# Output: False
print(a3 is b3)

Output:

False
True
False

Here, we can see that a1 and b1 are both integers with the same value. This means that they are both equal and the same. The same is true for a2 and b2 (strings).

But a3 and b3 are both lists. They are the same, but not the same because the interpreter puts them in different places in his or her memory, even though they are the same.

Python Precedence

The table below displays operator precedence from most to least important.

OperatorDescription
**Exponentiation (raise to the power)
~ + –Complement, unary plus and minus (method names for the last two are +@ and -@)
* / % //Multiply, divide, modulo, and floor division
+ –Addition and subtraction
>> <<Right and left bitwise shift
&Bitwise ‘AND’td>
^ |Bitwise exclusive `OR’ and regular `OR’
<= < > >=Comparison operators
<> == !=Equality operators
= %= /= //= -= += *= **=Assignment operators
is is notIdentity operators
in not inMembership operators
not or andLogical operators
Python Operators Precedence

Summary

In summary, Operators in Python are what make Python work. Operators can be used to do a lot of different things, like add two numbers or give a variable a value.

Now that we know what operators are, we are one step closer to writing better and more efficient codes.

So, start using all the operators you’ve learned today in your code.


Leave a Comment