Last updated: Monday 1st March 2021, 12:00 PT, AD

 

PLEASE REFRESH THIS PAGE TO GET THE LATEST VERSION


 

Python Operator Precedence

 

 

When writing Python expressions, it's usually best to indicate the order of operations by using parentheses (brackets).

 

If parentheses are not used in an expression, the computer will perform the operations in an order determined by the precedence rules. . .

 

 

 

See also Chapter 2.7 of this online pdf textbook.

 

 

 

Precedence Rules

 

Highest precedence

( )      (anything in brackets is done first)

**     (exponentiation)

-x, +x

*, /, %, //

+, -

relational operators: <, >, <=, >=, !=, ==

logical not

logical and

logical or

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.5
8.5  *  3  is  25.5
25.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 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.5
8.5 * 3  is  25.5
25.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.).

 
 
See Section 5.1 of this online textbook for an example of when you might use the modulus operator.
 
x = 19 % 4 + 15 / 2  * 3
print (x)

Click here for info on when you might want to use the modulus operator.

 
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  3
15 / 2  is  7.5
7.5  *  3   is  22.5
22.5  +  3  is  25.5
 
 
 
 
Example 4
 
x = (15 + 6) - 10 * 4
print (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  21
10 * 4  is  40
21 - 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

 

 

 

 

 







End of Python_Precedence.htm