Pylerno
English

Arithmetic

Choose the correct arithmetic operator and use parentheses to express intent.

On this page

The concept

Python provides addition, subtraction, multiplication, true division, floor division, remainder, and exponentiation. / produces a floating-point result for ordinary integer inputs. // floors the quotient: -7 // 2 is -4, not -3. % gives the remainder associated with floor division. ** is exponentiation.

Evaluation order matters. Multiplication happens before addition, so 2 + 3 * 4 is 14. Parentheses make (2 + 3) * 4 equal 20. Augmented assignment, such as score += 5, updates a binding using its previous value. Use quotient and remainder together when decomposing a duration into larger units and a leftover part.

Worked example

python
print(10 + 3, 10 - 3, 10 * 3)
print(10 / 3, 10 // 3, 10 % 3)
print(2**4)
print(-7 // 2)
print(2 + 3 * 4)
print((2 + 3) * 4)
score = 10
score += 5
score *= 2
print(score)

Trace the result

  1. Compare 10 / 3, 10 // 3, and 10 % 3: they answer three different questions.
  2. The two expressions containing 2, 3, and 4 differ only in parentheses, yet their results differ.
  3. Trace score through 10, 15, and 30 as the augmented assignments execute.
text
13 7 30
3.3333333333333335 3 1
16
-4
14
20
30
Line-by-line walkthrough

Line 1: print(10 + 3, 10 - 3, 10 * 3)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 2: print(10 / 3, 10 // 3, 10 % 3)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 3: print(2**4)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 4: print(-7 // 2)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 5: print(2 + 3 * 4)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 6: print((2 + 3) * 4)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 7: score = 10

Evaluate 10, then bind or update score with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 8: score += 5

Update the current target using score += 5. Trace its value before and after this line; later statements see the updated state.

Line 9: score *= 2

Update the current target using score *= 2. Trace its value before and after this line; later statements see the updated state.

Line 10: print(score)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

Dividing by zero raises an exception. Also, do not confuse floor division with truncation toward zero for negative values.

Check your understanding

What does this program print?

python
print(17 % 5)
Choose one answer

Search documentation

Share this page