You stored "Hello" in a variable yesterday. Today the values are numbers. What's 5 + 3?
Eight. That part I can do.
And in Python it works exactly the same way. + for addition, - for subtraction, * for multiplication, / for division. You can write expressions and assign the result to a variable, just like with strings:
total = 5 + 3
print(total) # 8What about 5 / 2? In some languages that gives 2 because 5 and 2 are integers. Does Python round it?
Python 3 always gives the real answer: 2.5. The / operator returns a float (a number with a decimal point), even when both inputs are whole numbers. If you specifically want integer division — "how many whole 2s fit in 5?" — you use //, which gives 2. And % gives the remainder: 5 % 2 is 1.
So 5 is a different type from 5.0?
Two different types: int (whole) and float (decimal). Most of the time you don't have to think about which is which — Python promotes int to float automatically when they meet (3 + 0.5 becomes 3.5, a float). The distinction matters when you care about precision, but for everyday arithmetic, just write the numbers naturally.
And operator precedence — does 5 + 3 * 2 give 16 or 11?
Same rules as math class. Multiplication before addition: 5 + 3 * 2 is 5 + 6 = 11. Use parentheses when you want to override: (5 + 3) * 2 is 16.
So Python is a calculator that remembers values in variables.
That's a fine first model. Calculator + memory. From here you build everything.
int and floatPython has two number types you'll use constantly:
| Type | Examples |
|---|---|
int (integer, whole number) | 0, 42, -7, 1000000 |
float (decimal number) | 3.14, 0.5, -2.0, 1e6 |
Write the literal directly — no quotes, no constructors:
count = 5 # int
ratio = 0.75 # float| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | addition | 5 + 3 | 8 |
- | subtraction | 10 - 4 | 6 |
* | multiplication | 4 * 7 | 28 |
/ | division (always float) | 5 / 2 | 2.5 |
// | floor division (drops the decimal) | 5 // 2 | 2 |
% | modulo (remainder) | 7 % 3 | 1 |
** | exponent | 2 ** 8 | 256 |
The usual math rules: ** first, then * / // %, then + -. When in doubt, use parentheses — they're free and make intent obvious.
result = 5 + 3 * 2 # 11 — multiply first
result = (5 + 3) * 2 # 16 — parens force orderWhen an int meets a float, Python promotes the int to a float and returns a float:
print(3 + 0.5) # 3.5 (float)
print(2 * 1.0) # 2.0 (float, even though the math is whole)// and % come up in pairs — together they answer "how many times does X fit in Y, with what left over?":
total_minutes = 137
hours = total_minutes // 60 # 2
leftover = total_minutes % 60 # 17
print(hours, leftover) # 2 17Unlike many languages, Python ints have no maximum size. 2 ** 1000 runs fine and produces a 302-digit number. Float precision is finite (~15 significant decimal digits), but ints are unbounded. Don't worry about overflow until your numbers are absurdly huge.
You stored "Hello" in a variable yesterday. Today the values are numbers. What's 5 + 3?
Eight. That part I can do.
And in Python it works exactly the same way. + for addition, - for subtraction, * for multiplication, / for division. You can write expressions and assign the result to a variable, just like with strings:
total = 5 + 3
print(total) # 8What about 5 / 2? In some languages that gives 2 because 5 and 2 are integers. Does Python round it?
Python 3 always gives the real answer: 2.5. The / operator returns a float (a number with a decimal point), even when both inputs are whole numbers. If you specifically want integer division — "how many whole 2s fit in 5?" — you use //, which gives 2. And % gives the remainder: 5 % 2 is 1.
So 5 is a different type from 5.0?
Two different types: int (whole) and float (decimal). Most of the time you don't have to think about which is which — Python promotes int to float automatically when they meet (3 + 0.5 becomes 3.5, a float). The distinction matters when you care about precision, but for everyday arithmetic, just write the numbers naturally.
And operator precedence — does 5 + 3 * 2 give 16 or 11?
Same rules as math class. Multiplication before addition: 5 + 3 * 2 is 5 + 6 = 11. Use parentheses when you want to override: (5 + 3) * 2 is 16.
So Python is a calculator that remembers values in variables.
That's a fine first model. Calculator + memory. From here you build everything.
int and floatPython has two number types you'll use constantly:
| Type | Examples |
|---|---|
int (integer, whole number) | 0, 42, -7, 1000000 |
float (decimal number) | 3.14, 0.5, -2.0, 1e6 |
Write the literal directly — no quotes, no constructors:
count = 5 # int
ratio = 0.75 # float| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | addition | 5 + 3 | 8 |
- | subtraction | 10 - 4 | 6 |
* | multiplication | 4 * 7 | 28 |
/ | division (always float) | 5 / 2 | 2.5 |
// | floor division (drops the decimal) | 5 // 2 | 2 |
% | modulo (remainder) | 7 % 3 | 1 |
** | exponent | 2 ** 8 | 256 |
The usual math rules: ** first, then * / // %, then + -. When in doubt, use parentheses — they're free and make intent obvious.
result = 5 + 3 * 2 # 11 — multiply first
result = (5 + 3) * 2 # 16 — parens force orderWhen an int meets a float, Python promotes the int to a float and returns a float:
print(3 + 0.5) # 3.5 (float)
print(2 * 1.0) # 2.0 (float, even though the math is whole)// and % come up in pairs — together they answer "how many times does X fit in Y, with what left over?":
total_minutes = 137
hours = total_minutes // 60 # 2
leftover = total_minutes % 60 # 17
print(hours, leftover) # 2 17Unlike many languages, Python ints have no maximum size. 2 ** 1000 runs fine and produces a 302-digit number. Float precision is finite (~15 significant decimal digits), but ints are unbounded. Don't worry about overflow until your numbers are absurdly huge.
Create a free account to get started. Paid plans unlock all tracks.