Operators in Python

Operators are particular symbols that are used to perform operations on operands. It returns result that can be used in application.

In other words we can also say that Operators are used to perform operation on operands.

Example

  1. 4 + 5 = 9  

Here 4 and 5 are Operands and (+) , (=) signs are the operators. This expression produces the output 9.

Types of Operators

Python supports the following operators

  1. Arithmetic Operators.
  2. Relational Operators.
  3. Assignment Operators.
  4. Logical Operators.
  5. Membership Operators.
  6. Identity Operators.
  7. Bitwise Operators.

Arithmetic Operators

The following table contains the arithmetic operators that are used to perform arithmetic operations.

OperatorsDescription
//Perform Floor division(gives integer value after division)
+To perform addition
To perform subtraction
*To perform multiplication
/To perform division
%To return remainder after division(Modulus)
**Perform exponent(raise to power)

Example

 
  1. >>> 10+20  
  2. 30  
  3. >>> 2010  
  4. 10  
  5. >>> 10*2  
  6. 20  
  7. >>> 10/2  
  8. 5  
  9. >>> 10%3  
  10. 1  
  11. >>> 2**3  
  12. 8  
  13. >>> 10//3  
  14. 3  
  15. >>>  

Relational Operators

The following table contains the relational operators that are used to check relations.

OperatorsDescription
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
==Equal to
!=Not equal to
<>Not equal to(similar to !=)

 

eg:

  1. >>> 10<20  
  2. True  
  3. >>> 10>20  
  4. False  
  5. >>> 10<=10  
  6. True  
  7. >>> 20>=15  
  8. True  
  9. >>> 5==6  
  10. False  
  11. >>> 5!=6  
  12. True  
  13. >>> 10<>2  
  14. True  
  15. >>>  

Assignment Operators

The following table contains the assignment operators that are used to assign values to the variables.

OperatorsDescription
=Assignment
/=Divide and Assign
+=Add and assign
-=Subtract and Assign
*=Multiply and assign
%=Modulus and assign
**=Exponent and assign
//=Floor division and assign

 

Example

  1. >>> c=10  
  2. >>> c  
  3. 10  
  4. >>> c+=5  
  5. >>> c  
  6. 15  
  7. >>> c-=5  
  8. >>> c  
  9. 10  
  10. >>> c*=2  
  11. >>> c  
  12. 20  
  13. >>> c/=2  
  14. >>> c  
  15. 10  
  16. >>> c%=3  
  17. >>> c  
  18. 1  
  19. >>> c=5  
  20. >>> c**=2  
  21. >>> c  
  22. 25  
  23. >>> c//=2  
  24. >>> c  
  25. 12  
  26. >>>  

Logical Operators

The following table contains the arithmetic operators that are used to perform arithmetic operations.

OperatorsDescription
andLogical AND(When both conditions are true output will be true)
orLogical OR (If any one condition is true output will be true)
notLogical NOT(Compliment the condition i.e., reverse)

Example

 
  1. a=5>4 and 3>2  
  2. print a  
  3. b=5>4 or 3<2  
  4. print b  
  5. c=not(5>4)  
  6. print c  

Output:

 
  1. >>>   
  2. True  
  3. True  
  4. False  
  5. >>>  

Membership Operators

The following table contains the membership operators.

OperatorsDescription
inReturns true if a variable is in sequence of another variable, else false.
not inReturns true if a variable is not in sequence of another variable, else false.

Example

 
  1. a=10  
  2. b=20  
  3. list=[10,20,30,40,50];  
  4. if (a in list):  
  5.     print “a is in given list”  
  6. else:  
  7.     print “a is not in given list”  
  8. if(b not in list):  
  9.     print “b is not given in list”  
  10. else:  
  11.     print “b is given in list”  

Output:

 
  1. >>>   
  2. is in given list  
  3. is given in list  
  4. >>>  

Identity Operators

The following table contains the identity operators.

OperatorsDescription
isReturns true if identity of two operands are same, else false
is notReturns true if identity of two operands are not same, else false.

Example

 
  1. a=20  
  2. b=20  
  3. if( a is b):  
  4.     print  a,b have same identity  
  5. else:  
  6.     print a, b are different  
  7. b=10  
  8. if( a is not b):  
  9.     print  a,b have different identity  
  10. else:  
  11.     print a,b have same identity  

Output

 
  1. >>>   
  2. a,b have same identity  
  3. a,b have different identity  
  4. >>>