1. Arithmatic operators

Arithmatic operators are used to perform the basic arithmatic operations.

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Exponent
// Floor division
In [1]:
a=20
b=10
c=12
d=15

print('Addition ',a+b)
print('Subtraction ',a-b)
print('Multiplication ', a*b)
print('Division ', d/b)
print('Modulus ', a%c)
print('Floor division ', d//b)

del(a,b,c,d) # deleting the variables
Addition  30
Subtraction  10
Multiplication  200
Division  1.5
Modulus  8
Floor division  1

2. Assignment Operators

Python support the following assignment operators.

Operator Description
= Assigns values from right side operands to the left side operand
+= It adds right operand to the left operand and assign the result to left operand
-= It subtracts right operand from the left operand and assign the result to left
* = It multiplies right operand with the left operand and assign the result to left operand.
/= It divides left operand with the right operand and assign the result to left
%= It takes modulus using two operands and assign the result to left operand
** = Performs exponential (power) calculation on operators and assign value to the left operand
//= It performs floor division on operators and assign value to the left operand
In [2]:
a=20
b=10
c=12
d=15
print("a= ", a, "\nb= ",b , "\nc= ", c, "\nd= ",d)

a+=b
print("The value of a and b after a+=b is ")
print("a = ", a)
print("b = ", b)

a-=b
print("The value of a and b after a-=b is ")
print("a = ", a)
print("b = ", b)

a*=b
print("The value of a and b after a*=b is ")
print("a = ", a)
print("b = ", b)

a/=b
print("The value of a and b after a/=b is ")
print("a = ", a)
print("b = ", b)

c%=b
print("The value of c and b after c %= b is ")
print("c = ", c)
print("b = ", b)

d//=b
print("The value of d and b after d %= b is ")
print("d = ", d)
print("b = ", b)

del(a,b,c,d) # deleting the variables
a=  20 
b=  10 
c=  12 
d=  15
The value of a and b after a+=b is 
a =  30
b =  10
The value of a and b after a-=b is 
a =  20
b =  10
The value of a and b after a*=b is 
a =  200
b =  10
The value of a and b after a/=b is 
a =  20.0
b =  10
The value of c and b after c %= b is 
c =  2
b =  10
The value of d and b after d %= b is 
d =  1
b =  10

3. Relational Operators

Relational operators compare the values on either side of the operator to decide the relation

Operator Description
== Returns True only if both operands are equal
!= Returns True if both operands are not equal
> The condition becomes true if left side operand is bigger than the right side operand
>= The condition becomes true if left side operand is bigger than or equal to the right side operand
< The condition becomes true if left side operand is smaller than the right side operand
<= The condition becomes true if left side operand is smaller than or equal to the right side operand
In [3]:
left_var1=100
left_var2=15
right_var1=100
right_var2=10

print('left_var1 == right_var1 is ', left_var1==right_var1)
print('left_var1 == right_var2 is ', left_var1==right_var2)

print('left_var1 != right_var2 is ', left_var1!=right_var2)

print('left_var1 > right_var2 is ', left_var1 > right_var2)
print('left_var1 >= right_var1 is ', left_var1 >= right_var1)

print('left_var1 < right_var2 is ', left_var1 < right_var2)
print('left_var1 <= right_var1 is ', left_var1 <= right_var1)

del(left_var1,left_var2, right_var1, right_var2 )  # deleting the variables
left_var1 == right_var1 is  True
left_var1 == right_var2 is  False
left_var1 != right_var2 is  True
left_var1 > right_var2 is  True
left_var1 >= right_var1 is  True
left_var1 < right_var2 is  False
left_var1 <= right_var1 is  True

4. Bitwise Operators

Bitwise operator works on bits and perform bit by bit operation.

Operator Operation Description
& Binary AND Operator copies a bit to the result if it exists in both operands
| Binary OR It copies a bit if it exists in either operand
^ Binary XOR It copies the bit if it is set in one operand but not both
~ Binary Ones Complement It is unary and has the effect of flipping bits.
<< Binary Left Shift The left operand's value is moved left by the number of bits specified by the right operand
>> Binary Right Shift The left operand's value is moved right by the number of bits specified by the right operand

10 in binary is 00001010

4 in binary is 00000100

a & b is

00001010

00000100

===============

00000000
In [4]:
a=10 # in binary system, the decimal no 10 is written as 00001010
b=4 #  in binary system, the decimal no 4 is written as 00000100
print("a & b is ",a&b)       # 00001010 & 00000100 is 00000000
print("a ^ b is ",a^b)       # 00001010 ^ 00000100 is 00001110 which is 14 in decimal number system
print("~a is ",~a)           # if a= 00001010 then ~a= 11110101 which is -11
print("a << 1 is ",a<<1)     # 00001010 <<1 is 00010100 which is 20 in decimal number system
print("a >> 1 is ",a >> 1)   # 00001010 >> 1 is 00000101  which is 5 in decimal number system

del(a,b) # deleting the variables
a & b is  0
a ^ b is  14
~a is  -11
a << 1 is  20
a >> 1 is  5

5. Logical Operators

Operator Operation Description
and Logical AND If both the operands are true then condition becomes true
or Logical OR If any of the operands are true then condition becomes true
not Logical NOT Used to reverse the logical state of its operand
In [5]:
a=20
b=10
c=12
d=15
print((a>b) and (d>c))
print((a>b) and (c>d))
print((a>b) or (c>d))
print(not(a>b))
print(not(b>a))

del(a,b,c,d) # deleting the variables
True
False
True
False
True

6. Membership Operators

Python's membership operators test for membership in a sequence, such as strings, lists, tuples.

Operator Description
in Evaluate to true if the variables on either side of the operator point to the same object and false otherwise
not in Evaluates to true if it does not find a variable in the specified sequence and false otherwise
In [6]:
str="Hello world"
print('H' in str) # Returns True because H is in str
print('A' in str) # Returns False because A is not in str
print('A' not in str) # Returns True because A is not in str
print('e' not in str) # Returns False because we are checking 'e' is not in str but e is in str so it will be False

del(str) # deleting the variable str
True
False
True
False

7. Identity Operators

Identity operators compare the memory locations of two objects.

Operator Description
is Evaluate to true if the variables on either side of the operator point to the same object and false otherwise
is not Evaluates to false if the variable on either side of the operator point to the same object and true otherwise
In [7]:
a=10
b=10
c=5
print(a is b)
print(a is c)
print(a is not b)
print(a is not c)

del(a,b,c) # deleting the variables
True
False
False
True

8. Operator Precedence

Operator Description
( ) Parentheses
** Exponentiation
~ , + , - Complement, Unary plus and minus
* , / , % , // Multiply, Divide, Modulus, Floor division
+ , - Addition, Subtraction
>> , << Right and left bitwise shift
& Bitwise AND
^ , | Bitwise exclusive OR, regular OR
<= , <> , >= Comparision operators
<>, == , != Equality operators
= , %= , /= , //= , -= , += , * = , ** = Assignment operators
is , is not Identity operator
in , not in Membership operators
not, or, and Logical operators
In [8]:
(2+3)**2+8/4
Out[8]:
27.0
In [9]:
2+3**2+9/3
Out[9]:
14.0
In [10]:
2-3**2+9/3
Out[10]:
-4.0