Lessons

The Priority of the Mathematical Operations

in the Computer and the Python Language

1. When two operators have the same priority, the operations are performed from left to right. In other words, in this case, the priority is given to the left operator.

2. The expressions inside the parentheses are executed first. If there are multiple nested parentheses, the value of the innermost parentheses is calculated first.

3. Power operator

4. Multiplication and division and remainder operators

5. Addition and subtraction operators


Tip:

In the Python language, we represent the multiplication operator with the symbol *, the division operator with the symbol /, the integer division operator with the symbol //, the remainder operator with the symbol %, and the exponentiation operator with the symbol **. For addition and subtraction, we use the same common symbols in mathematics, i.e. + and -.


Example

A + B / C * D

The computer first calculates B/C. Then it multiplies the result of this division by D and finally adds the obtained value to A.


A + B / (C * D)

The computer first performs the multiplication inside the parentheses and then divides B by the result of this multiplication and finally adds the obtained value to A.