# Operators in Python Operators in python are special symbols that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. Operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division & much more. Every programming language has it’s operators to perform different task.

Python divides the operators in the following groups:

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

#### Arithmetic Operators

Arithmetic Operators are used for mathematical operations

For example: –

``````x = 5
y = 4
print ('x + y =', x + y)
print ('x - y =', x - y)
print ('x * y =', x * y)
print ('x / y =', x/y)
print ('x // y =', x//y)
print ('x ** y =', x**y)
``````

Output

``````'x + y =', 9
'x - y =', 1
'x * y =', 20
'x / y =', 1
'x // y =', 1
'x ** y =', 625
``````

#### Comparison Operators

Comparison Operators are used to compare or decide the relation among them.

For example: –

``````x = 101
y = 121
print ('x > y is', x>y)
print ('x < y is', x<y)
print ('x == y is', x==y)
print ('x != y is', x!=y)
print ('x >= y is', x>=y)
print ('x <= y is', x<=y)
``````

Output

``````'x > y is', False
'x < y is', True
'x == y is', False
'x != y is', True
'x >= y is', False
'x <= y is', True``````

#### Logical Operators

Logical operators are used on conditional statements. Logical operators are used to check whether an expression is True or False.

For example

``````x = True
y = False
print ('x and y is', x and y)
print ('x or y is', x or y)
print ('not x is', not x)
``````

Output

``````'x and y is', False
'x or y is', True
'not x is', False
``````

#### Bitwise operators

Bitwise operators to perform Boolean logic on individual bits. Bitwise AND operator; Bitwise OR operator; Bitwise not operator; Bitwise XOR operator.

For example:

``````a = 6
b = 3
print ('a=',a,':', bin(a),'b=',b,':', bin(b))
c = 0
c = a & b;
print ("result of AND is ", c,':', bin(c))
c = a | b;
print ("result of OR is ", c,':', bin(c))
c = a ^ b;
print ("result of EXOR is ", c,':', bin(c))
c = ~a;
print ("result of COMPLEMENT is ", c,':', bin(c))
c = a << 2;
print ("result of LEFT SHIFT is ", c,':', bin(c))
c = a >> 2;
print ("result of RIGHT SHIFT is ", c,':', bin(c))
``````

Output

``````'a=', 6, ':', '0b110', 'b=', 3, ':', '0b11'
'result of AND is ', 2, ':', '0b10'
'result of OR is ', 7, ':', '0b111'
'result of EXOR is ', 5, ':', '0b101'
'result of COMPLEMENT is ', -7, ':', '-0b111'
'result of LEFT SHIFT is ', 24, ':', '0b11000'
'result of RIGHT SHIFT is ', 1, ':', '0b1'
``````

#### Python Membership Operators

Python offers two membership operators to check or validate the membership of a value.

For example:-

``````a = 5
b = 10
list = [1, 2, 3, 4, 5 ]
if ( a in list ):
print ("Line 1 - a is available in the given list")
else:
print ("Line 1 - a is not available in the given list")
if (b not in list):
print ("Line 2 - b is not available in the given list")
else:
print ("Line 2 - b is available in the given list")
``````

Output

``````Line 1 - a is available in the given list
Line 2 - b is not available in the given list
``````

#### Python Identity Operators

Python Identity Operators Example ; is, Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.

For example: –

``````a = 10
b = 10
print ('Line 1','a=',a ,':',id(a), 'b=',b ,':',id(b))
if ( a is b ):
print ("Line 2 - a and b have same identity")
else:
print ("Line 2 - a and b do not have same identity")
``````

Output

``````'Line 1', 'a=', 10, ':', 20839436, 'b=', 10, ':', 20839436
Line 2 - a and b have same identity
``````

#### Operators Precedence

The operator precedence in Python is listed below. It is in descending order (upper group has higher precedence than the lower ones).

Highest precedence to lowest precedence table