Arithmetic¶
~15 min ยท prerequisite: Variables and types
What you will be able to do¶
- Use the seven arithmetic operators:
+,-,*,/,//,%, and**. - Predict the result of an expression that mixes operators, using the order of operations.
- Recognize when a result is a float, and why
0.1 + 0.2is not exactly0.3.
The idea¶
An operator is a symbol that performs an action on values. The arithmetic operators work on numbers, and an expression is any combination of values and operators that produces a result. The expression 7 + 3 has the value 10.
Here are the operators you will use most:
+adds two numbers, and-subtracts.*multiplies, and**raises a number to a power, so2 ** 10is 1024./divides and always gives a float, so7 / 2is3.5, even though 7 and 2 are integers. Also8 / 2is4.0, not4.//is floor division. It divides and rounds down to the nearest whole number, so7 // 2is3.%is the remainder operator. It gives what is left after division, so7 % 2is1.
Floor division and remainder work as a pair. For positive whole numbers, a // b counts the full groups of b in a, and a % b counts what is left over.
When an expression has several operators, Python follows the order of operations. ** is done first, then *, /, //, and %, and finally + and -. Operators at the same level are done from left to right, except **, which is done from right to left. Parentheses override all of these rules, and they make your intent clear to the next reader.
Mixing an integer and a float gives a float. Floats are stored in binary, so some decimal values cannot be represented exactly. The result of 0.1 + 0.2 is 0.30000000000000004. For this reason, check float results with a small tolerance instead of with ==.
Worked example¶
seconds = 3725
hours = seconds // 3600
minutes = seconds % 3600 // 60
rest = seconds % 60
print(hours, minutes, rest)
Line by line, this is what Python does:
seconds = 3725stores the integer 3725.hours = seconds // 3600divides 3725 by 3600 and rounds down. The result is1, so there is one full hour.minutes = seconds % 3600 // 60evaluates in the usual order. Firstseconds % 3600gives125, the seconds left after the full hour. Then125 // 60gives2, so there are two full minutes.rest = seconds % 60gives the remainder after dividing by 60. The result is5, so there are five seconds left.print(hours, minutes, rest)shows1 2 5, with a space between each value.
Try it¶
Each block checks a group of results with assert. If any result is different from what the comment says, the check fails.
Common mistakes¶
-
Expecting
/to give a whole number. The following check fails:/gives3.5. Use//when you want the whole part. -
Dividing by zero. Writing
print(10 / 0)gives:Check the divisor before you divide.
-
Using
^for powers. In Python,^is not the power operator. The following check fails:2 ^ 3evaluates to1, because^means a different operation. Use2 ** 3for powers. -
Adding a number to text. Writing
print(2 + "3")gives:Convert the text with
int("3")first. -
Comparing floats with
==. Printing0.1 + 0.2shows0.30000000000000004, not0.3. Compare with a small tolerance, as in the third block above.
Practice¶
- Compute
15 // 4and15 % 4by hand. Then check your answers withassert. - Write an
assertthat checks3 * 4 + 2equals 14, and another that checks3 * (4 + 2)equals 18. - Write an
assertthat checks2 ** 5equals 32. Then write an assert that checks32 / 2equals16.0. - Given
minutes = 135, compute how many full hours are in it and how many minutes are left over. Assert both values. - Stretch: a number is even when
n % 2 == 0. Write an assert that checks 14 is even and 15 is not. Then write three lines that compute the sum of the digits of 472 using//and%, and assert the sum is 13.
Solution
15 // 4is3and15 % 4is3.
-
python assert 3 * 4 + 2 == 14 assert 3 * (4 + 2) == 18 -
python assert 2 ** 5 == 32 assert 32 / 2 == 16.0 -
python minutes_total = 135 hours = minutes_total // 60 leftover = minutes_total % 60 assert hours == 2 assert leftover == 15 -
```python assert 14 % 2 == 0 assert 15 % 2 != 0
n = 472 digit_a = n % 10 n = n // 10 digit_b = n % 10 n = n // 10 digit_c = n % 10 assert digit_a + digit_b + digit_c == 13 ```
The digits are 2, 7, and 4, and their sum is 13.
Recap¶
- The operators are
+,-,*,/,//,%, and**. Division with/always gives a float. - Python evaluates
**first, then*,/,//,%, and then+and-. Parentheses override this order. - Use
//and%together to split a number into whole groups and a remainder.