| Python operation | Arithmetic operator | Python expression |
|---|---|---|
| Addition | + |
f + 7 |
| Subtraction | – |
p - c |
| Multiplication | * |
b * m |
| Exponentiation | ** |
x ** y |
| True division | / |
x / y |
| Floor division | // |
x // y |
| Remainder (modulo) | % |
r % s |
*)¶*) multiplication operator:7 * 4
**)¶2 ** 10
1/2 or 0.5.9 ** (1 / 2)
/) vs. Floor Division (//) (1 of 3)¶/) divides a numerator by a denominator and yields a floating-point number.7 / 4
/) vs. Floor Division (//) (2 of 3)¶//) divides a numerator by a denominator, yielding the highest integer that’s not greater than the result.7 // 4
3 // 5
14 // 7
/) vs. Floor Division (//) (3 of 3)¶-13 / 4
-13 // 4
/ or // is not allowed and results in an exception.123 / 0
----> shows the code that caused the exception. z + 7
%) yields the remainder after the left operand is divided by the right operand.17 % 5
7.5 % 3.5
10 * (5 + 3)
10 * 5 + 3
- Expressions in parentheses evaluate first, so parentheses may force the order of evaluation to occur in any sequence you desire. Parentheses have the highest level of precedence. In expressions with nested parentheses, such as
(a / (b - c)), the expression in the innermost parentheses (that is,b - c) evaluates first.- Exponentiation operations evaluate next. If an expression contains several exponentiation operations, Python applies them from right to left.
- Multiplication, division and modulus operations evaluate next. If an expression contains several multiplication, true-division, floor-division and modulus operations, Python applies them from left to right. Multiplication, division and modulus are “on the same level of precedence.”
- Addition and subtraction operations evaluate last. If an expression contains several addition and subtraction operations, Python applies them from left to right. Addition and subtraction also have the same level of precedence.
**), which groups right-to-left. /) operator, which always yields a floating-point number. ©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 2 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.