Tutorial

Types of Operators in Python

4 min read

Let’s discuss the types of operators in Python or Python operators.

Python has a different variety of operators that can be used to perform different types of operations on variables and values.

These operators include arithmetic operators, comparison operators, assignment operators, logical operators, etc.

Some of the important types of operators in Python are:

  • Arithmetic operators.
  • Assignment Operators.
  • Comparison Operators.
  • Logical Operators.
  • Bitwise Operators.

Arithmetic operators in Python

These operators are used to perform mathematical operations on numeric values.

The basic arithmetic operators include:

  • + for addition
  • for subtraction
  • * for multiplication
  • / for division
  • % for modulus (remainder)
  • ** for exponentiation or power

Example of arithmetic operator in Python

x = 10
y = 5
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
print(x ** y)

The output of this will be:
15
5
50
2
0
100000

Assignment operators in Python

Assignment operators are used to assign a value to a variable.

The basic assignment operator is equal to(=), but there are also other operators that can be used to perform an operation and assign the result to a variable in a single step.

Some examples of these assignment operators include-

  • += for addition and assignment
  • -= for subtraction and assignment
  • *= for multiplication and assignment
  • /= for division and assignment
  • %= for modulus and assignment
  • **= for exponentiation and assignment

Example of assignment operators in Python-

x = 5
y = 3

x += y
print(x)

x -= y
print(x) 

x *= y
print(x)  

x /= y
print(x) 

x %= y
print(x)  

x **= y
print(x) 

The output of this will be:
8
5
15
5
2
8

Comparison operators in Python

Comparison Python operators are used to compare values and evaluate a Boolean value (True or False).

The comparison operators include:

  1. == for equality
  2. != for inequality
  3. < for less than
  4. > for greater than
  5. <= for less than or equal to
  6. >= for greater than or equal to

Example of Python comparison operator-

x = 10
y = 5

print(x == y) 

print(x != y) 

print(x < y) 

print(x > y) 

print(x <= y) 

print(x >= y) 

The output of this will be:
False
True
False
True
False
True

Logical operators in Python

These operators are used to perform logical operations and evaluate a Boolean value (True or False).

The basic logical operators include:

  • and – True if both the operands are True.
  • or – True if at least one of the operands is True.
  • not – True if the operand is False and vice-versa.

Example of logical operator in Python-

if x > 0 and x < 10:
    print("x is between 0 and 10")

if x < 0 or x > 10:
    print("x is less than 0 or greater than 10")

if not x == 5:
    print("x is not equal to 5")

Here, x is a variable that can contain any value.

The if statements use the logical operators to check if x is between 0 and 10 (and), less than 0 or greater than 10 (or), or not equal to 5 (not).

Bitwise operators in Python

In Python, the bitwise operators &, |, ^, ~, <<, and >> are used to manipulate the individual bits in an integer. These bitwise Python operators are explained one by one.

Example of Python bitwise operator-

# Bitwise AND
x = 5
y = 3
result = x & y
print(result)

# Bitwise OR
x = 5
y = 3
result = x | y 
print(result) 

# Bitwise XOR
x = 5
y = 3
result = x ^ y 
print(result) 

# Bitwise NOT
x = 5 
result = ~x
print(result) 

# Bitwise Left Shift
x = 5  
result = x << 2 
print(result) 

# Bitwise Right Shift 
x = 20 
result = x >> 2
print(result) 

The output of this will be:
1
7
6
-6
20
5

Here, the & operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. If either bit is 0, the corresponding result bit is set to 0.

Further, the | operator compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1. If both bits are 0, the corresponding result bit is set to 0.

^ operator compares each bit of the first operand to the corresponding bit of the second operand. If the corresponding bits are the same, the corresponding result bit is set to 0, otherwise, it is set to 1.

The ~ operator flips all the bits of the operand, it is also known as 1’s complement.

<< Python Operator left shift the bits of the operand, it will shift the bits to left and fill the rightmost bits with 0.

>>
Python Operator right shifts the bits of the operand, it will shift the bits to the right and fill the leftmost bits with 0.