Numbers are not strings
Every app you use is quietly doing arithmetic all day: totaling a cart, splitting a bill, converting minutes of video into a progress bar. Getting numbers right — and knowing when a "number" is secretly text — is where real money bugs come from, so Python is strict about the difference.
Write a number in code without quotes and Python treats it as an actual number it can do math with. 42 is a number, "42" is a string that merely looks like one. Whole numbers are called integers (ints), and numbers with a decimal point like 3.75 are called floats.
The math symbols, called operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | add | 7 + 3 | 10 |
- | subtract | 10 - 4 | 6 |
* | multiply | 6 * 7 | 42 |
/ | divide | 15 / 4 | 3.75 |
// | divide, drop the remainder | 15 // 4 | 3 |
% | remainder after division | 15 % 4 | 3 |
Note * means multiply (there is no × key in code) and / always gives a float. The remainder operator % looks odd now but becomes surprisingly useful, for example checking whether a number is even.
Code exercise · python
Run it and match each output line to the operator table above.
Order of operations
Python follows the same precedence you learned in school math: multiplication and division happen before addition and subtraction, and parentheses override everything.
print(2 + 3 * 4) print((2 + 3) * 4)
The first line multiplies first (3 × 4 = 12, then + 2 gives 14). The second computes the parentheses first (5 × 4 = 20). When in doubt, add parentheses: they cost nothing and make your intent obvious to human readers, which matters just as much as being correct.
Quiz
What does print(10 + 2 * 3) output?
Code exercise · python
Your turn. Compute how many minutes there are in a week using one print with multiplication (60 minutes per hour, 24 hours per day, 7 days). Do not type the answer directly, make Python compute it.
Code exercise · python
Your turn. A movie is 500 minutes of footage. Using // and %, print how many whole hours that is (first line) and how many minutes are left over (second line). Make Python compute both — expected output: 8 then 20.