Course outline · 0% complete

0/27 lessons0%

Course overview →

Numbers and math

lesson 2-2 · ~12 min · 6/27

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:

OperatorMeaningExampleResult
+add7 + 310
-subtract10 - 46
*multiply6 * 742
/divide15 / 43.75
//divide, drop the remainder15 // 43
%remainder after division15 % 43

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.