int = a whole number: 0, 1, -42, 1_000_000.
float = a decimal number: 0.5, -3.14, 2.0.
You can check what kind a value is with type():
type(42) # <class 'int'>
type(3.14) # <class 'float'>
type(1_000_000) # <class 'int'> underscores are legal as thousand-separators
| Operator | What it does | Example | Result |
|---|---|---|---|
+ | Add | 3 + 4 | 7 |
- | Subtract | 10 - 3 | 7 |
* | Multiply | 5 * 6 | 30 |
/ | Divide (always returns a float) | 10 / 4 | 2.5 |
// | Integer divide (drops the remainder) | 10 // 4 | 2 |
% | Modulo (the remainder) | 10 % 4 | 2 |
** | Power | 2 ** 10 | 1024 |
round(3.14159, 2) # 3.14
round(1234.5678, 0) # 1235.0
total = 1234567.891
print(f"${total:,.2f}") # $1,234,567.89
The f"..." string is called an f-string. We cover it properly in the next lesson; for now know that you can pop variables and format hints into one inside curly braces.
Mostly it doesn't. Python converts between them automatically. Two practical gotchas:
10 / 4 is 2.5 (a float). 10 // 4 is 2 (an int). If you need a clean whole number — for example, "how many full pages" — use //.decimal library in the Finance scenarios module.bill = 87.45
tip_pct = 18
people = 3
tip = bill * tip_pct / 100
total = bill + tip
per_person = total / people
print(f"Bill: ${bill:,.2f}")
print(f"Tip ({tip_pct}%): ${tip:,.2f}")
print(f"Total: ${total:,.2f}")
print(f"Per person: ${per_person:,.2f}")
int = whole, float = decimal./ always returns a float; // truncates; % gives the remainder.round(x, 2) rounds; f"${x:,.2f}" formats with commas and two decimals.Print the first ten powers of two in a column, like this:
2^1 = 2
2^2 = 4
2^3 = 8
...
2^10 = 1024
Hint: use a for loop (we cover those in Module 4) or just write ten lines.