Saturday, February 9, 2019

Arithmetic Operators

Arithmetic Operators:

+----------+----------------------+
|    +     |    Addition          |
|    -     |    Subtraction       |
|    *     |    Multiplication    |
|    /     |    Division          |
|    **    |    Exponentiation    |
|    //    |    Floor Division    |
|    %     |    Remainder         |

+----------+----------------------+

Arithmetic operators in Python are used for basic arithmetic operations like addition, subtraction, division etc.

Following table shows some examples of +, -, * and / operators. 

+----------------------+----------------+
|       Examples       |     Result     |
+----------------------+----------------+
|        3 + 7         |       10       |
|        8 - 27        |      -19       |
|        7 * 5         |       35       |
|        81 / 3        |      27.0      |
|        5 / 2         |       2.5      |
+----------------------+----------------+

The % sign is called modulus and is used to find the remainder of a division.

+----------------------+----------------+
|       Examples       |     Result     |
+----------------------+----------------+
|        15 % 4        |       3        |
|       15.0 % 4       |      3.0       |
+----------------------+----------------+

The ** is called the exponentiation operator. To find the cube of 2 we write 2**3 as shown in the table below.

+----------------------+----------------+
|       Examples       |     Result     |
+----------------------+----------------+
|        3 ** 2        |        9       |
|       3.0 ** 2       |       9.0      |
+----------------------+----------------+

The // is used to for floor division or integer division if both the operands are integers. The examples below shows some results.

+----------------------+----------------+
|       Examples       |     Result     |
+----------------------+----------------+
|        5 // 2        |        2       |
|       5.0 // 2       |       2.0      |
+----------------------+----------------+

.

No comments: