PLEASE REFRESH
THIS PAGE TO GET THE LATEST VERSION
Highest precedence
** (exponentiation)
Lowest precedence
What the
above means is explained in the examples below:
Example 1
x = 17 / 2 * 3 + 2
print (x)
In the expression to evaluate the value of x, the operators / and * share equal precedence that is higher than that of the + operator. Hence the division and multiply are done before the addition, resulting in an output of 27.5, as explained here... 17 / 2 is 8.58.5 * 3 is 25.525.5 + 2 is 27.5 Example 2 x = 2 + 17 / 2 * 3
print (x)
In the expression to evaluate the value of x, the operators / and * share equal precedencethat is higher than that of the + operator. Hence the division and multiply are done before the addition, resulting in an output of 27.5, as explained here... 17 / 2 is 8.58.5 * 3 is 25.525.5 + 2 is 27.5 Example 3
The % (modulus or modulo) operator yields the remainder
from the division of the first argument by the second. The arguments may be
floating point numbers, e.g., 3.14 % 0.7 equals 0.34 (since 3.14 equals 4 * 0.7 +
0.34.),
or integer numbers, e.g., 5 % 2 equals 1 (since 5 equals 2
* 2 + 1.).
Click here for info on when you might want to use the modulus operator. x = 19 % 4 + 15 / 2 * 3print (x) In the expression to evaluate the value of x, the operators % / and * share equal precedence that is higher than that of the + operator. Hence the % (modulus), division and multiply are done before the addition, resulting in an output of 25.5, as explained here... 19 % 4 is 315 / 2 is 7.57.5 * 3 is 22.522.5 + 3 is 25.5 Example 4 x = (15 + 6) - 10 * 4print (x) In the expression to evaluate the value of x, the brackets have highest of all precedence, so is evaluated before anything else, then * is done, and lastly the - subtraction, resulting in an output of -19 (minus 19), as explained here... 15 + 6 is 2110 * 4 is 4021 - 40 is -19 Example 5
x = 17 / 2 % 2 * 3**3
print (x)
In the expression to evaluate the value of x, the exponentiation is done first (three to the power of 3 is 3 * 3 * 3 which equals 27) as it has higher precedence to the other operators (/ % and *). So, ... This is the order of evaluation:Exponentiation is done first and the expression then evaluates from left to right:3 to the power of 3 is 27.
the
expression is now 17 / 2 % 2 * 27
17 / 2 is 8.5
8.5 % 2 is
0.5
0.5 *
27 is 13.5