Python Operators

Python operators can be used to perform operations on variables and values. There is a different type of operations which can be performed.

Python Operators

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

Python Operators

Python Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operators.

Operator Name Example
+ Addition x + y
Subtraction x – y
* Multiplication x * y
/ Division x / y
% Modulus x%y
** Exponentiation x ** y
// Floor Division x // y

Example

a = 9
b = 4  
# Addition of numbers 
add = a + b 
# 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   
# print results 
print(add) 
print(sub) 
print(mul) 
print(div1) 
print(div2) 
print(mod)

Output
13
5
36
2.25
2
1

Python Assignment Operators

Assignment operators help us to assign values to variables.

Operator Example
= x = 5
+ = x += 5
 – = x -= 5
* = x *= 5
/ = x /= 5
% = x %= 5
// = x //= 5
** = x **= 5
& = x &= 5
| = x | = 5
  ^ = x ^ = 5
>>= x >> =5
<<= x <<= 5

Example

a = 21
b = 10
c = 0
c = a + b
print "Value of c is ", c
c += a
print "Value of c is ", c 
c *= a
print "Value of c is ", c 
c /= a 
print "Value of c is ", c 
c  = 2
c %= a
print "Value of c is ", c
c **= a
print "Value of c is ", c

c //= a
print "Value of c is ", c

Output
Value of c is 31
Value of c is 52
Value of c is 1092
Value of c is 52
Value of c is 2
Value of c is 2097152
Value of c is 99864

Python Comparison Operators

Comparison Operators are used for comparing two values.

Operator Name Example
= =   equal x = = y
! = Not equal x!= y
>   Greater than x > y
<   Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Example

a = 21
b = 10
c = 0
if ( a == b ):
   print "Line 1 - a is equal to b"
else:
   print "Line 1 - a is not equal to b"
if ( a != b ):
   print "Line 2 - a is not equal to b"
else:
   print "Line 2 - a is equal to b"

if ( a <> b ):
   print "Line 3 - a is not equal to b"
else:
   print "Line 3 - a is equal to b"
if ( a < b ):
   print "Line 4 - a is less than b" 
else:
   print "Line 4 - a is not less than b"
if ( a > b ):
   print "Line 5 - a is greater than b"
else:
   print "Line 5 - a is not greater than b"
a = 5;
b = 20;
if ( a <= b ):
   print "Line 6 - a is either less than or equal to  b"
else:
   print "Line 6 - a is neither less than nor equal to  b"

if ( b >= a ):
   print "Line 7 - b is either greater than  or equal to b"
else:
   print "Line 7 - b is neither greater than  nor equal to b"

Output
a is not equal to b
a is not equal to b
a is not equal to b
a is not less than b
a is greater than b
a is either less than or equal to b
b is either greater than or equal to b

Python Logical Operators

Logical operators used for comparing conditional statements.

Operator Description Example
And Returns True if both statements are true x< 6 and x < 15
  or Returns True if one of the statements is true x < 8 or x < 3
not Reverse the result, returns False if the result is true not(x<4 and x < 11)

Example

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 Identity Operators

Identity operators used to compare the objects, not if they are equal, even if they are having the same object, with the same memory location.

Operator Description Example
is Returns true if both variables are the same object x is y
  is not Returns true if both variables are not the same object x is not y

Example

#!/usr/bin/python
a = 20
b = 20
if ( a is b ):
   print "a and b have the same identity"
else:
   print "a and b do not have the same identity"
if ( id(a) == id(b) ):
   print "a and b have the same identity"
else:
   print "a and b do not have the same identity"
b = 30
if ( a is b ):
   print "a and b have the same identity"
else:
   print "a and b do not have same identity"

if ( a is not b ):
   print "a and b do not have same identity"
else:
   print "a and b have same identity"

Output
a and b have the same identity
a and b have the same identity
a and b do not have the same identity
a and b do not have the same identity

Python Membership Operators

Membership operators are used to testing that if a sequence is present in an object.

Operator Description Example
in Returns True if a sequence with the specified value is present in the object x in y
 not in Returns True if a sequence with the specified  value is not present in the object x not in y

Example

#!/usr/bin/python
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
   print "a is available in the given list"
else:
   print "a is not available in the given list"
if ( b not in list ):
   print "b is not available in the given list"
else:
   print "b is available in the given list"
a = 2
if ( a in list ):
   print "a is available in the given list"
else:
   print "a is not available in the given list"

Output
a is not available in the given list
b is not available in the given list
a is available in the given list

Python Bit-wise Operators

Bitwise operators used for comparing binary numbers.

Operator Name Description
  & AND Sets each bit to 1 if both bits are 1
   | OR Sets each bit to 1 if one of two bits is 1
  ^ XOR Sets each bit to 1 if only one of two bits is 1
~   NOT Inverts all the bits
  <<   Zero left shift left by pushing zeros in from the right let the leftmost bits fall off
>>   Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

Example

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