Python Programming Basic Operators

Python Programming Arithmetic Operators

Python supports addition (+), subtraction (-), multiplication (*), division operators (/), modulo (%), Exponent (**) and Floor Division (//) operators.
#!/usr/bin/python    
                                        
a = 3   
b = 2                                   
                                        
c = a + b           
print("addition of %d and %d is %d" % (a, b, c))     
                                        
c = a - b                                   
print("subtraction of %d and %d is %d" % (a, b, c))      
                                        
c = a * b                                                                       
print("multiplication of %d and %d is %d" % (a, b, c))    
                                                    
c = a / b                                          
print("division of %d and %d is %d" % (a, b, c))    
                                                 
c = a % b                                    
print("%d modulo %d is %d" % (a, b, c))
                                       
c = a**b                               
print("%d power of %d is %d" % (a, b, c))             
                                       
c = a//b                               
print("%d floor division %d is %d" % (a, b, c))     

output:
addition of 3 and 2 is 5
subtraction of 3 and 2 is 1
multiplication of 3 and 2 is 6
division of 3 and 2 is 1
3 modulo 2 is 1
3 power of 2 is 9
3 floor division 2 is 1
 

Comparison Operators:

executes the code in 'while block' multiple times till condition fails. equal (==), not equal (!=, <>), less than (<), greater than (>), less than or equal (<=), greater than or equal (>=).
                                                                                
#!/usr/bin/python                                                               
                                                                                
a = 15                                                                          
b = 15                                                                          
                                                                                
if ( a == b ):                                                                  
   print("%d is equal to %d" % (a, b))                                          
                                                                                
a=15                                                                            
b=10                                                                            
                                                                                
if (a != b):                                                                    
   print("%d is not equal to %d" % (a, b))                                      
                                                                                
if (a <> b):                                                                    
   print("%d is not equal to %d" % (a, b))                                      
                                                                                
if (a < b):                                                                     
   print("%d is less than %d" % (a, b))                                         
                                                                                
if (a > b):                                                                     
   print("%d is greater than %d" % (a, b))                                      
                                                                                
a = 15;                                                                         
b = 25;                                                                         
if (a <= b):                                                                    
   print("%d is either less than or equal to  %d" % (a, b))                     
                                                                                
if (b >= a):                                                                    
    print("%d is either greater than or equal to  %d" % (b, a)) 

    
output:
15 is equal to 15
15 is not equal to 10
15 is not equal to 10
15 is greater than 10
15 is either less than or equal to  25
25 is either greater than or equal to  15

 

Python Programming Assignment Operators:

#!/usr/bin/python                                                               
                                                                                
a = 2                                                                           
b = 5                                                                           
                                                                                
c = a + b                                                                       
print("c is (a + b): %d" % c)                                                   
                                                                                
c += a                                                                          
print("c is (c + a): %d" % c )                                                  
                                                                                
c *= a                                                                          
print("c is (c * a): %d" % c)                                                   
                                                                                
c /= a                                                                          
print("c is (c / a): %d" % c)                                                   
                                                                                
b %= a                                                                          
print("b is (b modulo a): %d" % b)                                              
                                                                                
b = 5                                                                           
b **= a                                                                         
print("b is (b ** a): %d" % b)                                                  
                                                                                
b = 5                                                                           
b //= a                                                                         
print("b is (b // a): %d" % b) 
    
output:
c is (a + b): 7
c is (c + a): 9
c is (c * a): 18
c is (c / a): 9
b is (b modulo a): 1
b is (b ** a): 25
b is (b // a): 2

Python Programming Bitwise Operators

AND (&), OR (|), XOR (^), Ones compliments (~), Left Shift (<<) and Right Shift (>>)
#!/usr/bin/python                                                               
                                                                                
a = 15                                                                          
b = 20                                                                          
                                                                                
print("bitwise AND of %d and %d is %d " % (a, b, a & b))                        
                                                                                
print("bitwise OR of %d and %d is %d " % (a, b, a | b))                         
                                                                                
print("bitwise XOR of %d and %d is %d " % (a, b, a ^ b))                        
                                                                                
print("bitwise ones compliment of %d is %d " % (a, ~a))                         
                                                                                
print("bitwise Left shift of %d by number of bits  %d is %d " % (a, b, a << b)) 
                                                                                
print("bitwise Right shift of %d by number of bits %d is %d " % (a, b, a >> b))                                                                                                                                                                        
    
output:
bitwise AND of 15 and 20 is 4 
bitwise OR of 15 and 20 is 31 
bitwise XOR of 15 and 20 is 27 
bitwise ones compliment of 15 is -16 
bitwise Left shift of 15 by number of bits  20 is 15728640 
bitwise Right shift of 15 by number of bits 20 is 0 

Python Programming Logical Operators

and, or, not
#!/usr/bin/python                                                               
                                                                                
a = 15                                                                          
b = 20                                                                          
                                                                                
print("logical %d and %d is %d " % (a, b, a and b))                             
                                                                                
print("logical %d or %d is %d " % (a, b, a or b))                               
                                                                                
print("logical %d not %d is %d " % (a, b, not(a or b)))      


output:
logical 15 and 20 is 20 
logical 15 or 20 is 15 
logical 15 not 20 is 0 
 

Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Free online programming tutorials

Email Facebook Google LinkedIn Twitter
^