Python Integer Arithmetic: Add, Subtract and Multiply
Calculate with whole numbers
An integer (int) is a whole number, such as 12, 0 or -4. Use + for addition, - for subtraction and * for multiplication. These operations produce exact integers when both operands (the values used by the operator) are integers.
The outputs are 17, 7 and 60, on separate lines. Python uses *, not the multiplication symbol used on paper.
Track a stock balance
stock = 14stock = stock + 3 * 4stock = stock - 9print(stock)Three packs of four add twelve items; selling nine leaves seventeen. The name stock is rebound after each calculation, as in the previous chapter.
Subtraction can produce a negative result
The result -3 represents a shortage, not an error. Multiplication by zero gives zero. Python integers can grow beyond fixed machine-word sizes, subject to available resources; do not assume negative balances are automatically rejected.
Practice
Start with 30 items, receive five packs of two, then sell 12. Predict the remaining count before running your calculation. Next, consider quantities with fractional parts.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Integer arithmetic
Check your understanding of integer arithmetic.
What is the result?
print(16 + 3)A balance starts at 5 and decreases by 8. What does Python compute?
Which expression calculates the number of items in 7 packs of 4 items each?
What is printed?
print(9 * 0)Which statement updates stock by removing 6 items?