# Python 3 - Operators Precedence Example

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

The following table lists all operators from highest precedence to the lowest.

Sr.No. Operator & Description
1

**

Exponentiation (raise to the power)

2

~ + -

complement, unary plus and minus (method names for the last two are +@ and -@)

3

* / % //

Multiply, divide, modulo and floor division

4

+ -

5

>> <<

Right and left bitwise shift

6

&

Bitwise 'AND'

7

^ |

Bitwise exclusive `OR' and regular `OR'

8

<= < > >=

Comparison operators

9

<> == !=

Equality operators

10

= %= /= //= -= += *= **=

Assignment operators

11

is is not

Identity operators

12

in not in

Membership operators

13

not or and

Logical operators

Operator precedence affects the evaluation of an expression.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because the operator * has higher precedence than +, so it first multiplies 3*2 and then is added to 7.

Here, the operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom.

## Example

#!/usr/bin/python3

a = 20
b = 10
c = 15
d = 5

print ("a:%d b:%d c:%d d:%d" % (a,b,c,d ))
e = (a + b) * c / d       #( 30 * 15 ) / 5
print ("Value of (a + b) * c / d is ",  e)

e = ((a + b) * c) / d     # (30 * 15 ) / 5
print ("Value of ((a + b) * c) / d is ",  e)

e = (a + b) * (c / d)    # (30) * (15/5)
print ("Value of (a + b) * (c / d) is ",  e)

e = a + (b * c) / d      #  20 + (150/5)
print ("Value of a + (b * c) / d is ",  e)

## Output

When you execute the above program, it produces the following result −

a:20 b:10 c:15 d:5
Value of (a + b) * c / d is  90.0
Value of ((a + b) * c) / d is  90.0
Value of (a + b) * (c / d) is  90.0
Value of a + (b * c) / d is  50.0
python_basic_operators.htm