Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

List of priorities

Since you're new to Python operators, we don't want to present the complete list of operator priorities right now. 

Instead, we'll show you a truncated form, and we'll expand it consistently as we introduce new operators. 

Look at the table below: 

Priority      Operator 

  1.        **
  2.        +, - (note: unary operators located next to the right of the power operator bind more strongly)   unary
  3.        *, / , // , %
  4.        +, -                                                                                                                                                                      binary

Note: we've enumerated the operators in order from highest (1) to the lowest (4) priorities. 

Try to work through the following expression: 

print(2 * 3 % 5)

Both operators (* and %) have the same priority, so the result can be guessed only when you know the binding direction. What do you think? What is the result? 

1

Operators and parentheses

Of course, you're always allowed to use parentheses, which can change the natural order of calculation. 

In accordance with the arithmetic rules, subexpressions in parentheses are always calculated first. 

You can use as many parentheses as you need, and they're often used to improve the readability of an expression, even if they don't change the order of the operations. 

An example of an expression with multiple parentheses is here:

print((5 * ((25 % 13) + 100) / (2 * 13)) // 2)

Try to compute the value that's printed to the console. What's the result of the print() function 

10.0

 

Key Takeaways

  1. An Expression is a combination of values (or variable, operators, calls to functions - you will learn about them soon) which evaluates to a certain value, e.g., 1 + 2 
  2.  Operators are special symbols or keywords which are able to operate on the values and perform (mathematical) operations, e.g., the * operator multiplies tow values: x * y
  3.  Arithmetic operators in Python: + (addition), - (subtraction), * (multiplication), / (classic division - always returns a float), % (modulus - divides left operand by right operand and returns the remainder of the operation, e.g., 5 % 2 = 1 ), ** (exponentiation - left operand raised to the power of right operand, e.g.d, 2 ** 3 = 2 * 2 * 2 = 8). // (floor/integer division - returns a number resulting from division - returns a number resulting from division, but rounded down to the nearest whole number, e.g., 3 ?? 2.0 = 1.0)
  4. unary operator is an operator with only one operand, e.g., -1, or +3
  5. binary operator is an operator with tow operands, e.g., 4 + 5, or 12 % 5
  6. Some operators act before others - the hierarchy of priorities:
    • ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​the ** operator (exponentiation) has the highest priority;
    • then the unary + and - (note: a unary operator to the right of the exponentiation operator binds more strongly, for example: 4 ** -1 equals 0.25
    • then *, /, //, and %
    • and, finally the lowest priority: the binary + and -
  7. Subexpressions in parentheses are always calculated first, e.g., 15 -1 * (5 * (1 + 2)) = 0
  8. The exponentiation operator uses right-sided binding, e.g., 2 ** 2 ** 3 = 256

​​​​​​​Exercise 1

What is the output of the following snippet? 

print((2 ** 4), (2 * 4.), (2 * 4))

16 8.0 8

Exercise 2

What is the output of the following snippet? 

print((-2 /4), (2 / 4), (2 // 4), (-2 //4))

-0.5 0.5​ 0 -1

Exercise 3

What is the output of this snippet?

print((2 % -4), (2 % 4), (2 ** 3 ** 2))

​​​​​-2 2 512