Numbers and Math
Computers are famously good at math. Today you’ll make Python do arithmetic for you — and learn the single most common beginner gotcha: the difference between the text "3" and the number 3.
Python is a calculator
The math symbols are mostly what you’d expect:
| Symbol | Means | Example | Result |
|---|---|---|---|
+ | add | 5 + 3 | 8 |
- | subtract | 5 - 3 | 2 |
* | multiply | 5 * 3 | 15 |
/ | divide | 7 / 2 | 3.5 |
// | whole divide | 7 // 2 | 3 |
% | remainder | 7 % 2 | 1 |
** | power | 2 ** 3 | 8 |
Try it 🎯
- Print how many minutes are in a week:
7 * 24 * 60. - Use
%to check if 18 is even:18 % 2(a result of 0 means even).
Math with variables
Store numbers in variables and compute with them. Mix numbers and text in print using commas:
The big gotcha: input gives you text
Here’s the thing that trips up everyone. input() always hands back a string, even if the user types digits. So you can’t do math with it directly:
Type 3 and 4. You get 34, not 7! Because a and b are strings, + glued them together (just like last lesson).
The fix: int() and float()
To turn text into a number, wrap it in int() (whole numbers) or float() (decimals):
Now 3 and 4 give 7. The int(...) converts the text into a real number before the math happens. Remember this — it’s behind countless beginner bugs.
Try it 🎯
Change int to float and try entering 2.5 and 1.5.
A real calculator
Ask for two numbers and show several results at once:
Predict it 🔮
What does this print? Look carefully at whether int() is used. (Hint: think about strings vs numbers.)
(It prints 105 — both are strings, so + glues them. To get 15, convert: int(x) + int(y).)
Fix the bug 🐞
This should tell the user their age next year, but it crashes — you can’t add a number to text:
(age is text from input. Convert it: age = int(input("How old are you? ")). Then age + 1 works.)
Your mission 🚀
Write an “age in days” program: ask the user’s age (as a number), then print roughly how many days they’ve been alive (age × 365):
What you learned today
- Python does math with
+ - * /, plus//(whole divide),%(remainder),**(power). input()always returns text — convert it withint(...)orfloat(...)to do math.- Mixing up
"3"(text) and3(number) is the most common beginner bug. Now you know it!
Next time we meet True and False — how the computer answers yes/no questions, the first step toward making decisions. ✅
Comments