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, and "42" is a string that merely looks like one. Whole numbers are called integers, or ints, and numbers with a decimal point like 3.75 are called floats.
The math symbols are 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 that * means multiply, since there is no × key in code, and / always gives a float even when the division comes out even. The remainder operator % looks odd now but becomes surprisingly useful, for example checking whether a number is even.
Every operator on one screen
Each line matches one row of the operator table above.
print(7 + 3) print(10 - 4) print(6 * 7) print(15 / 4) print(15 // 4) print(15 % 4)
Output
10 6 42 3.75 3 3
The last three lines are the interesting ones, because all three divide 15 by 4 and give three different answers. / gives the exact 3.75, // gives the 3 whole times 4 fits, and % gives the 3 left over.
Those last two always belong together: 4 × 3 + 3 = 15. Whenever you split a quantity into whole groups, // counts the groups and % counts what did not fit, which is exactly the hours-and-minutes problem later in this lesson.
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.
Precedence in practice
print(10 + 2 * 3) outputs 16.
Multiplication happens before addition, so 2 * 3 is 6 first, and then 10 + 6 gives 16. Python read the line left to right but did not evaluate it left to right.
To get 36 you would write (10 + 2) * 3, forcing the addition first.
This is the same rule you learned in school arithmetic, which is convenient, and it is also the reason parentheses are worth adding even when they change nothing. 10 + (2 * 3) behaves identically to the original and tells the next reader what you meant.
Minutes in a week, computed
Rather than typing the answer, let Python multiply the three factors.
print(60 * 24 * 7)
Output
10080Reading the pieces
- Multiplications chain freely as
a * b * c, evaluated left to right, so 60 × 24 gives 1440 and that times 7 gives 10080. - There are no quotes anywhere on the line. Quotes would make it a string, and
"60" * 24would try to repeat text rather than multiply numbers. - Writing the factors out is better than writing
10080, because the line now explains itself. A reader sees minutes per hour, hours per day, and days per week, and can check the logic without trusting your arithmetic.
Hours and leftover minutes from 500
// and % split 500 minutes into whole hours and the remainder.
print(500 // 60) print(500 % 60)
Output
8 20
Reading the pieces
//gives the whole-number part of the division, dropping anything left over, so 500 minutes is 8 full hours.%gives exactly the part//dropped, which is 20 minutes.- The two answers reconstruct the original: 8 × 60 + 20 = 500. Any time you see
//and%used on the same pair of numbers, that identity is what makes the pair trustworthy. - This is how every duration display you have ever seen is built. A video player showing 8:20 ran this same pair of operations on a count of seconds.