6.5.1 – Arithmetic in programs

6.5.1 – Arithmetic in programs

Choose arithmetic operators from a program’s requirements, distinguish three kinds of division, and control calculation order. Trace results and test small calculation programs against independently worked answers.

The arithmetic operator toolkit

An arithmetic expression combines values and operators to calculate a result. A program can store that result, display it or use it in another calculation. Python supports all seven operators required here:

OperatorMeaningExampleResult
+Addition8 + 513
-Subtraction13 - 58
*Multiplication4 * 624
/True division15 / 43.75
//Floor/integer division15 // 43
%Modulus/remainder15 % 43
**Exponentiation3 ** 481

Use * for multiplication, not the written multiplication cross. Exponentiation repeats multiplication: 3 to the fourth power is 3 multiplied by itself four times, not 3 multiplied by 4. Python's ^ does not mean exponentiation.

Python
books = 18
new_books = 7
damaged_books = 3
usable_books = books + new_books - damaged_books
shelf_spaces = 4 * 6
cube_cells = 3 ** 3
print(usable_books)
print(shelf_spaces)
print(cube_cells)
Output
22
24
27

Each right-hand expression is evaluated before its result is assigned. Name an intermediate result when it has a useful role; usable_books describes what 22 means.

Three kinds of division

Suppose 53 items are packed in boxes holding 8. True division answers the exact quotient question. Integer division answers how many complete boxes fit. Modulus answers how many items remain.

Python
items = 53
capacity = 8
exact_boxes = items / capacity
complete_boxes = items // capacity
left_over = items % capacity
print(exact_boxes)
print(complete_boxes)
print(left_over)
Output
6.625
6
5

Six complete boxes use 48 items, leaving 5. Multiplying the complete-box count by capacity and adding the remainder reconstructs 53. Modulus is a remainder operator, not a percentage operator.

/ returns a float even for exact division: 12 / 3 is 4.0. // floors the quotient, rather than rounding to the nearest integer. For non-negative counts this gives complete groups; zero items produce zero groups and zero remainder. An exact multiple produces remainder zero.

For negative values, flooring goes towards negative infinity: -7 // 3 is -3, and -7 % 3 is 2. The reconstruction still works: -3 groups of 3 plus 2 equals -7. By contrast, int(-7 / 3) truncates towards zero and gives -2. This is why truncation and floor division cannot be treated as interchangeable for every domain. The packing examples use non-negative integers and a positive capacity.

No division operator can use zero as its divisor here. Choose a stated valid domain or add an appropriate guard once selection has been taught; do not silently assume zero capacity is meaningful.

Expression order and parentheses

Python follows operator precedence rather than evaluating every expression from left to right. Parentheses group an expression first, then exponentiation binds before multiplication/division/modulus, which bind before addition/subtraction.

PriorityOperations
FirstParenthesised groups
Then**
Then*, /, //, %
Then+, -

Operations on the same arithmetic row normally group left to right. Exponentiation chains group right to left; explicit parentheses make the intended grouping clearer. Unary minus also needs care: -3 ** 2 is -9, whereas (-3) ** 2 is 9 because the negative base is grouped.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account