top of page

Operators


Python supports the following operators:

  • Arithmetic Operators.

  • Relational Operators.

  • Assignment Operators.

  • Logical Operators.

  • Membership Operators.

  • Identity Operators.
     

Arithmetic Operators

x = 1
y = 2

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)

op:

x + y = 2
x - y = 0
x * y = 1
x / y = 1.0
x // y = 1
x ** y = 1
x + y = 3
x - y = -1
x * y = 2
x / y = 0.5
x // y = 0
x ** y = 1

x = 1
y = 2

Relational Operators


x = 1
y = 2

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)

op:

('x > y =', False)                                                                                                                                                              
('x < y =', True)                                                                                                                                                               
('x <= y =', True)                                                                                                                                                              
('x >= y =', False)                                                                                                                                                             
('x != y =', True)                                                                                                                                                              
('x == y =', False)   

 

Assignment Operators


x = 1
print x
x+=5
print x
x-=5
print x
x*=5
print x
x/=5
print x

 

op:

1                                                                                                                                                                               
6                                                                                                                                                                               
1                                                                                                                                                                               
5                                                                                                                                                                               
1  

Logical Operators

x=5>4 and 3>2  
print x 
x=5>4 or 3<2  
print x  
x=not(5>4)  


print x  

op:

True                                                                                                                                                                            
True                                                                                                                                                                            
False

Membership Operators

x=1  
y=2  


list=[1,2,3,4,5];

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

op:

    
x is in given list                                                                                                                                                              
y is in given list

Identity Operators
 

a=20  
b=20  
if( a is b):  
    print  "a is b" 
else:  
    print "a is not b"  
b=10  
if( a is not b):  
    print  "a,b are not same"  
else:  
    print "a,b are same" 

op:

a is b                                                                                                                                                                          
a,b are not same  
    
a= "hai" 
b= "hai"
if( a is b):  
    print  "a is b" 
else:  
    print "a is not b"  
b="hello"  
if( a is not b):  
    print  "a,b are not same"  
else:  
    print "a,b are same"


op:


a is b                                                                                                                                                                          
a,b are not same

© 2015 Thirumurugan

bottom of page