Happy Holidays! from eMexo Technologies.. Call us (+91 9513216462) to get 30% discount now! An operator can be defined as a symbol responsible for a particular operation between two operands. The following are some symbols ( -+, *, /) used as Python operators. Each symbol or operator has its own type of operation. The value at which an operator performs each operation is called an operand. This article describes different types of Python operators.

## Types of Python Operators

Python language supports the following types of operators:

• Arithmetic operators
• Comparison operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators

## Arithmetic Operators

Arithmetic operators are used to performing mathematical operations such as addition, subtraction, multiplication, and division.

#### Consider the following example Program for Arithmetic Operator:

``````# Examples of Arithmetic Operator
a = 9
b = 4

# Subtraction of numbers
sub = a - b

# Multiplication of number
mul = a * b

# Division(float) of number
div1 = a / b

# Division(floor) of number
div2 = a // b

# Modulo of both number
mod = a % b

# Power
p = a ** b

# print results
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
``````

#### Output:

``````13
5
36
2.25
2
1
6561``````

## Comparison Operators

Python Comparison operators are also called Relational operators. They compare the values ​​on both sides of the operator and infer the relationships between the values. After comparison, returns a Boolean value, Such as either true or false.

#### Consider the following example program for Comparison Operators in Python

``````# Examples of Relational Operators
a = 13
b = 33

# a > b is False
print(a > b)

# a < b is True
print(a < b)

# a == b is False
print(a == b)

# a != b is True
print(a != b)

# a >= b is False
print(a >= b)

# a <= b is True
print(a <= b)
``````

#### Output:

``````False
True
False
True
False
True``````

## Python Assignment Operators

The assignment operator is used to assign a value to a Python variable. The assignment may be made directly, or the operator may first perform some mathematical operation and then assign the value to the operand.

#### Consider the following example program for Assignment Operators in Python

``````#!/usr/bin/python

a = 21
b = 10
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 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864``````

## Logical Operators in Python

Logical operators are primarily used in conditional statements. There are three types of logical operators: AND, OR, and NOT.

#### Consider the following example program for Logical Operators in Python

``````# Examples of Logical Operator
a = True
b = False

# Print a and b is False
print(a and b)

# Print a or b is True
print(a or b)

# Print not a is False
print(not a)
``````

#### Output:

``````False
True
False``````

## Python Bitwise Operators

Bitwise operators perform bitwise operations on the values ​​of two operands. These are used to operate on binary numbers.

#### Consider the following example program for Bitwise Operators in Python

``````# Examples of Bitwise operators
a = 10
b = 4

# Print bitwise AND operation
print(a & b)

# Print bitwise OR operation
print(a | b)

# Print bitwise NOT operation
print(~a)

# print bitwise XOR operation
print(a ^ b)

# print bitwise right shift operation
print(a >> 2)

# print bitwise left shift operation
print(a << 2)
``````

#### Output:

``````0
14
-11
14
2
40``````

## Python Membership Operators

The membership operator is used to test whether a value is available in a sequence. It can be any sequence like (Python string, Python list, Python set, Python taple, Python dictionary). There are two types of membership operators, in and not in.

#### Consider the following example program for Membership Operator

``````# Python program to illustrate
# not 'in' operator
x = 24
y = 20
list = [10, 20, 30, 40, 50]

if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")

if (y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
``````

#### Output:

``````x is NOT present in given list
y is present in given list``````

## Python Identity Operators

The Identity operator is used to compare the memory addresses of two different objects. The two types of Identity operators in Python are is and not.

#### Consider the following example program for Identity Operator

``````a = 10
b = 20
c = a

print(a is not b)
print(a is c)
``````

#### Output:

``````True
True``````

## Python Operator Precedence

Some expressions have multiple operators. To evaluate such expressions, there is a precedence rule called the Python operator precedence. Controls the order in which these operations are performed.

#### Consider the following example Program:

``````#!/usr/bin/python

a = 20
b = 10
c = 15
d = 5
e = 0

e = (a + b) * c / d       #( 30 * 15 ) / 5
print "Value of (a + b) * c / d is ",  e

e = ((a + b) * c) / d     # (30 * 15 ) / 5
print "Value of ((a + b) * c) / d is ",  e

e = (a + b) * (c / d);    # (30) * (15/5)
print "Value of (a + b) * (c / d) is ",  e

e = a + (b * c) / d;      #  20 + (150/5)
print "Value of a + (b * c) / d is ",  e``````

#### Output:

``````Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
Value of (a + b) * (c / d) is 90
Value of a + (b * c) / d is 50``````