Arithmetic Operators and Expressions
Arithmetic Operators
Almost every program calculates something: a total price, an average mark, the time left on a timer. Python does this arithmetic with seven operators, symbols that combine operands into a new value. Most behave exactly as on a calculator, but division, remainders and the order of evaluation follow rules you need to know before you can trust your results.
The seven operators
Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 2 | 9 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 7 * 2 | 14 |
/ | True division | 7 / 2 | 3.5 |
// | Floor division | 7 // 2 | 3 |
% | Modulo (remainder) | 7 % 2 | 1 |
** | Exponentiation | 7 ** 2 | 49 |
When both operands are integers, +, -, *, // and % produce an integer, and so does ** with a non-negative exponent. True division is different: / always returns a float, so 8 / 4 gives 2.0, not 2. If either operand is a float, the result is a float too: 7.0 // 2 gives 3.0.
Floor division and the remainder
Floor division divides and then rounds the quotient down to the nearest integer, towards negative infinity. The % operator, called modulo, returns what is left over. For any integers a and b with b=0, the two results always fit together:
Example: To split 100 minutes into hours and minutes, 100 // 60 gives 1 and 100 % 60 gives 40, so 100 minutes is 1 hour 40 minutes. Check: 60×1+40=100.
The same pair is handy for testing divisibility (n % 2 is 0 exactly when n is even) and for extracting digits (n % 10 is the last digit of a positive integer).
With negative numbers, rounding down matters. The exact quotient −7÷2 is −3.5, and rounding down gives −4, so -7 // 2 is -4. The identity then forces -7 % 2 to be 1, because 2×(−4)+1=−7. In Python, the remainder takes the sign of the divisor.
Operator precedence
Operator precedence is the ranking that decides which operation Python performs first when an expression mixes operators. From highest to lowest:
- Parentheses ( )
- Exponentiation **
- Unary minus, as in -x
- *, /, // and %
- + and -
Operators on the same level are evaluated left to right, except **, which groups right to left: 3 ** 2 ** 2 means 3 ** (2 ** 2), which is 3 ** 4, or 81.
Example: In 20 - 6 / 3 * 2, division and multiplication share a level, so work left to right: 6 / 3 gives 2.0, then 2.0 * 2 gives 4.0. Subtraction comes last, and 20 - 4.0 is 16.0. The result is a float because / produced one.
What to remember
- / always returns a float; // rounds the quotient down towards negative infinity.
- % gives the remainder, and a=b×(a//b)+(a%b) links the two.
- Precedence runs parentheses, **, unary minus, then * / // %, then + -.
- Same-level operators go left to right, except **, which goes right to left.
Example: Packing a snack order with integer arithmetic
A student council writes a Python program to plan an order of 230 granola bars. Boxes hold 24 bars each, and the bars will be shared among 8 classes. Work each part before revealing its solution.
a) Evaluate 230 // 24 and 230 % 24, and say what each value means for the order.
Floor division counts the complete boxes. The exact quotient is 230÷24≈9.58, and rounding down gives 230 // 24 = 9 full boxes.
Those nine boxes hold 9 × 24 = ________ bars, and subtracting that from 230 leaves 14. So 230 % 24 evaluates to 14: fourteen bars are left over for a partly filled box. Check with the identity: 24×9+14=230.
b) Compare 230 / 8 with 230 // 8 as the number of bars per class.
True division gives the exact share, 230 / 8 = 28.75, and it is a float. Floor division gives 230 // 8 = 28, the number of whole bars each class can actually receive.
Giving 28 bars to each of the 8 classes uses 8 × 28 = ________ bars, so 6 bars remain, which 230 % 8 confirms. Use // when only whole items make sense and / when a fractional average is what you want.
c) The program also contains the line points = 2 + 3 * 4 ** 2 // 5 - 1. Find the value stored in points, showing the order of operations.
Exponentiation has the highest precedence here, so 4 ** 2 gives ________ first.
Next come * and //, which share a level, so work left to right: 3 * 16 gives ________, then 48 // 5 gives ________ because 48÷5=9.6 rounds down to 9.
Finally + and -, again left to right: 2 + 9 gives ________, and 11 - 1 gives 10. The variable points stores the integer 10.
d) A stock adjustment of −17 bars (returned stock) is grouped into packs of 5 with -17 // 5 and -17 % 5. Evaluate both.
Unary minus outranks // and %, so the operand is −17. The exact quotient is −17÷5 = ________, and floor division rounds down towards negative infinity, giving −4, not −3. So -17 // 5 is −4.
The remainder must satisfy the identity: 5 × (−4) = ________, and −17−(−20)=3. So -17 % 5 is 3, which has the same sign as the divisor 5. Check: 5×(−4)+3=−17.
A workshop uses Python to calculate how many equal portions different quantities provide. Complete the table by evaluating each division using / and //. Enter the numerical value of each result as an exact integer or short decimal without units, and do not round. A whole-number result may be entered either way, for example 7 or 7.0.
| Calculation | Result using / | Result using // |
| 35 divided by 4 | ||
| 18 divided by 5 | ||
| 20 divided by 4 |
A student runs the following line of Python code. What value does it print?
print(-2 ** 2 + 10 // 4 * 3)