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:

SymbolMeansExampleResult
+add5 + 38
-subtract5 - 32
*multiply5 * 315
/divide7 / 23.5
//whole divide7 // 23
%remainder7 % 21
**power2 ** 38
 

Try it 🎯

  1. Print how many minutes are in a week: 7 * 24 * 60.
  2. 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 with int(...) or float(...) to do math.
  • Mixing up "3" (text) and 3 (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