05 - Python Operators And Expressions

 

To describe Python operators first we need to refer to something basic from the math, terms called expression, operator and operand. For example, if we have 12 + 18, this is called expression, 12 and 18 are called operands and + sign is called operator.

Now when basic terminology is clear, next relevant thing is to list all type of operators that are supported within the Python:

  • Arithmetic Operators
  • Comparison Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Below are described all types of operators along with the examples.

5.1 Arithmetic Operators

* If the type of the operand is integer then only the whole part of a division is returned, digits after the decimal point are removed and operation acts similar to Floor Division

x = 15
y = 7
print x + y        # 22
print x - y         # 8
print x * y        # 105
print x / y        # 2
print x % y       # 1       15/7 = 2 -> 2*7 =14 -> 15-14 = 1, remainder is 1
print x ** y      # 170859375
print x // y       # 2

To understand better how division and floor division works with float data types, check the example below.

x = 15.2
y = 7
print x / y                    # 2.17142857143
print round(x/y, 2)      #2.17   round function rounds result of division to N decimal places
print x % y                   # 1.2    15.2/7 = 2 -> 2*7 =14 -> 15.2 -14 = 1.2, remainder is 1.2
print x // y                   # 2.0    15.2/7 = 2.2 -> after removing digits after decimal point 2.0

Like us on Facebook