Python Operators .
In this session i have discussed python Operators .
Operators is a symbol that perform some action on the values of variable like + operator is used to perform the addition action .
Java supports following types of operators .
1Arithmetic Operators .
2Relational Operators .
3Logical Operators .
4Assignment Operators .
5Identity Operators.
6Membership Operators .
7Bitwise Operators .
Arithmetic Operators :
The Python Arithmetic operators perform all the mathmetical and logical operation like addition , subtraction , multiplication , division etc .
Operator  x=20 y=10  
Plus or unary plus  Add the two numbers  x+y //30  
Substraction or unary minus  Subtract the first number from second number  xy //10  
Multiplication .  Multiplies the two numbers  x*y //200  
Division.  Divide the two numbers  x/y // 2.0  
Modulo division .  Return the modulus  x%y // 0  
Floor division .  Return the floor value after divide  x//y // 2  
Exponent .  It calculates the first operand power to second operand  x**2 // 100  
. 
Example :Write a python program that will add the two numbers .
x=20
y=10
sum=x+y
print("Sum %d"%(sum))
OUTPUT : Sum : 30
Example : Write a python program that will subtract the two numbers .
x=20
y=10
sub=xy
print("Sub %d"%(sub))
OUTPUT : Sub : 10
Example : Write a python program that will multiplies the two numbers .
x=20
y=10
mul=x*y
print("Mul %d"%(mul))
OUTPUT : Mul : 200
Example :Write a python program that will divide the two numbers .
x=21
y=10
div=x/y
print("Div %d"%(div))
OUTPUT : Div : 2.1
Example : Write a python program that will returns the modulus .
x=21
y=10
mod=x%y
print("Mod %d"%(mod))
OUTPUT : Mod : 1
Example : Write a python program that demonstrate floor division operator.
x=21
y=10
div=x//y
print("Div %d"%(div))
OUTPUT : Div : 2
Example : Write a python program that demonstrate exponent operator.
x=10
y=2
result=x**y
print("Result %d"%(result))
OUTPUT : Result : 100
Relational Operators :
Relational operators is used to compare two quantities to take certain decision . For example you may compare the price of two items .
is less than  Check if the left side operand less to right side operand then condition become true  
is less than or equal to  Check if the left operand value is less than or equal to right side operand value then condition become true  
is greater than  Check if left side operand value is greater than right side operand value then condition becomes true  
is greater than or equal to  Check if left side operand value is greater then or equal to right side operand value then condition become true  
is equal to  Check if the left side operand value is same to right side of operand value then condition become true  
is not equal to  Check if the left side operand value is not equal to right side of operand value then condition become true  
. 
Example : Write a python program that will check the x value is greater than y value .
x=20
y=10
if x>y:
print("x is greater than y")
OUTPUT : x is greater than y
Example : Write a python program that will check the condition x value is greater then or equal to y value.
x=20
y=10
if x>=y:
print("x is greater than or equal to y")
OUTPUT : x is greater than or equal to y
Example : Write a python program that will check the condition x value is less then y value .
x=10
y=20
if x<y:
print("x is less than y");
OUTPUT x is less than y
Example : Write a python program that will check the condtion x value is less then or equal to y value .
x=10
y=20
if x<=y:
print("x is less than or equal to y")
OUTPUT : x is less than or equal to y
Example :Write a python program that will check the condition x value is equal to y value .
x=10
y=10
if x==y:
print("x and y values are same")
OUTPUT : x and y values are same
Example : Program that will check condtion x is not equal to y value .
x=10
y=20
if x!=y:
print("x and y values are not same")
OUTPUT : x and y values are not same
Logical Operators :
Logical operators are used to evaluate the two or more conditions . It is combine the relational expression . You can use the any kind of expression . If condition is true then it will return 1 other wise 0 .
Logical AND  It return true if both operads are non zero  (x==10) and (y==20) //true  
Logical OR  It returns true if any operand is non zero  (x==10) or (y==10) //true  
Logical NOT  It reverse the condition if operads is true then it become false  not(x>y) //true  
. 
Example : Write a java program that will check the condition if both condition is true then it will execute the statement inside the if .
x=10
y=20
if(x==10)and(y==20):
print("Both condition true")
OUTPUT : Both condition true
Example : Write a python program that will check the condition if any one condition is true then the statement inside the if will execute .
x=10
y=20
if (x==10)or(y==30):
print("Single Condition true")
OUTPUT : Single Condition true
Example : Program that will check the condition if condtion is false then the statement inside the if will execute .
x=10
y=20
if not x>y
println("Welcome")
OUTPUT : Welcome
Assignment Operators :
Assignment operators are used to assign the value to variable in python program . The usual assignment operator is = operator . has some short hand assignment operators as given below .
operator 
operator 
. 
Example :
x=20
x=x+1
print("X : %d"%(x))
OUTPUT : X : 21
Example :
x=20
x=x1
println("X : %d"%(x))
OUTPUT : X : 19
Example :
x=20;
x=x*x;
print("X : %d"%(x))
OUTPUT : X : 400
Example :
x = 20
n = 1
x = x / (n+9)
print("X : %d"%(x))
OUTPUT : 2
Example :
x = 20
y = 3
x = x % y;
print("X : %d"%(x))
OUTPUT : 2
Example :
x = 20
y = 3
x = x ** y;
print("X : %d"%(x))
OUTPUT : 8000
Example :
x = 20
y = 3
x = x // y;
print("X : %d"%(x))
OUTPUT : 6
Identity Operators :
It is evaluated to be true if the reference present at both sides point to the same object.  
It is evaluated to be true if the reference present at both side do not point to the same object.  
. 
Example : Write a python program that will demonstrate use of is operator .
x=10
if(type(x) is int):
print("true")
else:
print("false")
OUTPUT : true
Example : Write a python program that will demonstrate use of is not operator .
x=10.5
if(type(x) is not int):
print("true")
else:
print("false")
OUTPUT : true
Membership Operators :
Python membership operators are used to check the membership of value inside a data structure. If the value is present in the data structure, then the resulting value is true otherwise it returns false.
It is evaluated to be true if the first operand is found in the second operand (list, tuple, or dictionary).  
It is evaluated to be true if the first operand is not found in the second operand (list, tuple, or dictionary).  
. 
Example : Write a python program that will demonstrate use of in operator .
list1=[1,2,3,4,5]
list2=[5,2,6,7,8,9]
for item in list1:
if item in list2:
print("overlapping")
else:
print("not overlapping")
OUTPUT : overlapping overlapping not overlapping
Example : Write a python program that will demonstrate use of not in operator .
list1=[1,2,3,4,5]
list2=[5,2,6,7,8,9]
for item in list1:
if item not in list2:
print("not found")
OUTPUT : not found not found
Bitwise Operator :
Before read the bitwise operator please try to understand the following table .
. 
Binary AND operator  It is used to add bit of first operand with bit of second operand if both are 1 it will be one other wise 0.  
Binary OR operator  It is used to add bit of first operand with bit of second operand if at least one corresponding bit of two operands is 1 it will be 1 other wise 0 .  
Binary XOR operator  It is 1 if other two corresponding operands are opposite Please read the first table .  
Binary Ones complement operator  It is change the 1 to 0 and 0 to 1 please read the first table .  
Left shift operator  It is used to shifts all bits towards left by specified bits  

Right shift operator  It is used to shifts all bits towards right by specified bits  
. 
Bitwise AND(&) Operator :
Lets take a simple example x=12 , y=25 . First we change the values in binary format .
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
& 
00001000 = 8 (In decimal)
x=12
y=25
print(x&y)
OUTPUT : 8
Bitwise OR() Operator :
Lets take a simple example x=12 , y=25 . First we change the values in binary format .
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
 
00011101 = 29 (In decimal)
x=12
y=25
print(xy)
OUTPUT : 29
Bitwise XOR(^) Operator :
Lets take a simple example x=12 , y=25 . First we change the values in binary format .
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
^ 
00010101 = 21 (In decimal)
x=12
y=25
print(x^y)
OUTPUT : 21
Bitwise Complement(~) Operator :
So let 's take a look at how we get ~12 = 13: .
Convert into binary format : 00001100 Simply flip all the bits and we get: 11110011 Well, what's 13 look like in two's complement? Start with positive 13: 00001101, flip all the bits to 11110010, and add 1 to become negative value (13), 11110011. So if you simply invert the bits in 12, you get the two's complement representation of 13.
x=12
print(~x)
OUTPUT : 13
Bitwise RightShift(<<) Operator :
Lets take a simple example x=12. First we change the values in binary format .
12 = 00001100 (In Binary)

12<<1 = 00011000 (right shift by one bit)
x=12
print(x<<1)
OUTPUT : 24
Bitwise LeftShift(>>) Operator :
Lets take a simple example x=12. First we change the values in binary format .
12 = 00001100 (In Binary)

12>>1 = 00000110 (right shift by one bit)
x=12
print(x>>1)
OUTPUT : 6