Learn
01.5 | ARITHMETIC OPERATORS
01 | AN OPERATOR DESCRIBES AN ACTION
An arithmetic operator combines numeric values to produce a result. The values it operates on are called operands. In 8 + 3, + is the operator and 8 and 3 are its operands.
You can use literals, variables or expressions as operands. Python calculates an expression before print displays it or assignment stores it.
Keep the problem’s meaning beside the symbols. Adding three kilometres to two kilometres makes sense; adding a ticket count to a temperature usually does not, even if Python can calculate the numbers.
first_distance = 8
second_distance = 3
total_distance = first_distance + second_distance
print("Total kilometres:", total_distance)EXPECTED OUTPUT
Total kilometres: 11
02 | ADD, SUBTRACT AND MULTIPLY
+ adds, - subtracts and * multiplies numeric values. Multiplication is written with an asterisk, not the letter x or the typographic multiplication sign. A negative literal such as -3 uses a minus sign to indicate its value.
Use subtraction for a difference or remaining amount. Use multiplication for equal groups, such as three sessions of twenty minutes. Choose descriptive result names so the formula’s purpose is visible.
sessions = 3 minutes_per_session = 20 total_minutes = sessions * minutes_per_session remaining_minutes = 90 - total_minutes print(total_minutes, remaining_minutes)
EXPECTED OUTPUT
60 30
03 | / GIVES TRUE DIVISION
/ divides and returns a float for these ordinary integer and float operands. 17 / 5 gives 3.4. Even 20 / 5 produces 4.0 rather than an int result.
Use it for a mean or a fractional quantity. If three scores total 25, their mean is 25 / 3, not the number of complete groups.
Division by zero raises ZeroDivisionError. A task such as dividing a bill must establish a non-zero number of people. A prompt alone does not enforce that requirement.
print(17 / 5) print(20 / 5)
EXPECTED OUTPUT
3.4 4.0
04 | // FINDS THE FLOOR QUOTIENT
// is floor division. For non-negative whole-number grouping examples, it gives the number of complete groups. Seventeen items in capacity-five boxes fill three complete boxes, with items left over.
“Floor” means rounding the quotient down towards negative infinity. It is not simply truncation towards zero: -17 // 5 gives -4, not -3. The main grouping tasks here use non-negative quantities and positive capacities.
Integer operands produce an int result; using float operands can produce a float floor-division result such as 3.0. Floor division also fails for a zero divisor.
print(17 // 5) print(-17 // 5)
EXPECTED OUTPUT
3 -4
05 | % FINDS THE REMAINDER
% is the modulo operator. For seventeen items grouped in fives, 17 % 5 gives two leftover items. It is not the fractional part 0.4 of ordinary division, and it is not a percentage operator.
For positive integer grouping values, dividend = quotient × divisor + remainder. Here 17 = 3 × 5 + 2. That relation provides a useful check.
For negative values Python’s floor and remainder rules still apply: -17 = (-4) × 5 + 3. Begin with positive groups before exploring these cases.
items = 17
capacity = 5
print("Complete groups:", items // capacity)
print("Leftover items:", items % capacity)EXPECTED OUTPUT
Complete groups: 3 Leftover items: 2
06 | ** RAISES TO A POWER
** performs exponentiation. 2 ** 3 means 2 multiplied by itself three times, giving eight. It is not 2 × 3. Use powers for quantities such as the area of a square: side ** 2.
Python’s ^ operator is not exponentiation; it is a bitwise operation outside this beginner lesson. Choose ** for powers.
side = 4
area = side ** 2
print("Area in square units:", area)EXPECTED OUTPUT
Area in square units: 16
07 | PRECEDENCE DECIDES WHAT COMBINES FIRST
Parentheses group an expression explicitly. Exponentiation has higher precedence than multiplication, division, floor division and modulo; those have higher precedence than addition and subtraction.
2 + 3 * 4 gives fourteen because multiplication is evaluated before addition. (2 + 3) * 4 gives twenty because the parentheses group the sum.
Operators at the multiplication/division level generally associate left to right: 20 / 5 * 2 gives eight. Powers associate right to left: 2 ** 3 ** 2 means 2 ** (3 ** 2), giving 512. Use parentheses when the intended grouping might be unclear.
print(2 + 3 * 4) print((2 + 3) * 4) print(20 / 5 * 2)
EXPECTED OUTPUT
14 20 8.0
08 | AVERAGES NEED THE WHOLE TOTAL
To average two values, add both before dividing by two. Without parentheses, first + second / 2 halves only the second value before adding the first.
For 8 and 12, the mean is ten. The incorrectly grouped expression gives fourteen. Python has obeyed its rules in both cases; the second formula does not match the intended calculation.
This is a logic error: the code can run while answering the wrong question. Test with small values whose expected answer you know.
first = 8 second = 12 print((first + second) / 2) print(first + second / 2)
EXPECTED OUTPUT
10.0 14.0
09 | UPDATE A VALUE YOU ALREADY HAVE
To accumulate a running total, use total = total + new_value. The right side reads the old total and adds the new amount, then assignment replaces the total with that combined result.
total += new_value is a compact update form for these numbers. Learn the full version first so the shorthand has a meaning. If total has not been assigned before use, Python raises NameError.
Resetting total to zero before every addition would discard earlier contributions. Keep the old amount when the problem requires accumulation.
total = 0 total = total + 8 total += 5 print(total)
EXPECTED OUTPUT
13
10 | FLOAT RESULTS AND READABLE DISPLAY
Floating-point numbers approximate many decimal fractions. A calculation such as 0.1 + 0.2 can display 0.30000000000000004. It does not mean Python performed random arithmetic; it reflects finite binary representation.
An f-string format such as :.2f displays two digits after the decimal point. It makes an output readable, but does not turn floating-point arithmetic into exact decimal arithmetic or change the original value.
amount = 0.1 + 0.2
print(amount)
print(f"Displayed amount: {amount:.2f}")EXPECTED OUTPUT
0.30000000000000004 Displayed amount: 0.30
11 | EXPERIMENT WITH AN OPERATOR
Use the demo with small non-negative whole numbers. Choose an operation, predict the result and then check. This constrained teaching tool mirrors the operators shown here; run your complete scripts in Python.
12 | BUILD A GROUPING REPORT
Write a program asking for a non-negative whole number of items and a positive whole-number group size. Report complete groups and leftover items. State these input assumptions clearly for this first version.
Test 17 and 5, 20 and 5, and 0 and 5. Check that exact division gives zero remainder. Deliberately try a zero group size to observe the error, then explain why the program needs a rule preventing it.
Extension: calculate a square’s area, a two-score mean and total practice time in separate clearly labelled sections. Use parentheses to express the intended formulas.