Numbers & Types
Integers, floats, None, type checking, and arithmetic — the numeric foundation.
We've been using numbers since Week 1, but I've noticed sometimes I get 5 and sometimes 5.0. In my spreadsheets every number just... is a number. What's the difference in Python?
Good eye. Python has two number types. An integer (int) is a whole number with no decimal point. A float is a number with a decimal point. They look similar, but they behave differently in ways that matter when you're processing real data:
type(5) # <class 'int'>
type(5.0) # <class 'float'>
type(-3) # <class 'int'>
type(0.1) # <class 'float'>
The type() function tells you exactly what kind of value you're dealing with. Think of it like checking a cell's format in your spreadsheet — is it a whole number or a decimal?
OK, so when does the type actually matter? Like, when would this trip me up?
Division is the big one. Regular division with / always returns a float, even if the result is a whole number. This catches people off guard:
10 / 2 # 5.0 (float, not int!)
10 / 3 # 3.3333333333333335
10 // 3 # 3 (integer division — rounds down)
10 % 3 # 1 (modulo — the remainder)
The // operator is floor division — it divides and drops the decimal. The % operator gives you the remainder. These two are a powerful pair: // tells you how many times something fits, % tells you what's left over.
Here's a real example. Say you're tracking time for a project report:
minutes = 137
hours = minutes // 60 # 2
leftover = minutes % 60 # 17
print(f"{hours}h {leftover}m") # "2h 17m"
You've probably done this math in your head before — "137 minutes, that's 2 hours and 17 minutes." Now Python does it for you.
Can I convert between ints and floats? Like, if I get 5.0 but I need 5?
Yes — use int() and float() to convert explicitly:
int(7.9) # 7 — truncates, does NOT round
int(7.1) # 7
float(5) # 5.0
round(7.9) # 8 — this one rounds properly
round(3.14159, 2) # 3.14 — round to 2 decimal places
Notice that int() chops off the decimal — it always moves toward zero. If you want proper rounding (like you'd expect from a spreadsheet's ROUND function), use round() instead.
What about None? I keep seeing it pop up. Is that like an empty cell?
That's actually a great analogy. None is Python's way of saying "nothing" or "no value." It's not zero, not an empty string — it's the absence of a value entirely. Just like how an empty cell in your spreadsheet isn't the same as a cell containing 0:
type(None) # <class 'NoneType'>
result = None
print(result) # None
result is None # True
result == 0 # False — None is not zero
Functions that don't explicitly return something return None. You'll see it used as a default value or a placeholder for "not yet computed" — like marking a cell as "pending" in your tracking sheet.
How do I check the type of something? Like if I want to make sure an input is a number before doing math on it?
Use isinstance(). It's more flexible than type() because it also works with inheritance (you'll learn about that later):
isinstance(5, int) # True
isinstance(5.0, float) # True
isinstance(5, (int, float)) # True — checks multiple types
isinstance("5", int) # False — it's a string, not an int
This is what you'd use in real code. Think of it as data validation — the same way you'd set up a validation rule in your spreadsheet to make sure a column only contains numbers.
And I can use all the normal math operators? Same ones I use in spreadsheet formulas?
All of them. Here's the full set:
| Operator | Meaning | Example |
|---|---|---|
+ | addition | 5 + 3 = 8 |
- | subtraction | 5 - 3 = 2 |
* | multiplication | 5 * 3 = 15 |
/ | division (float) | 5 / 3 = 1.666... |
// | floor division | 5 // 3 = 1 |
% | modulo | 5 % 3 = 2 |
** | exponentiation | 5 ** 3 = 125 |
Python follows standard math order of operations — ** first, then *, /, //, %, then + and -. Use parentheses when in doubt. Same instinct you have with spreadsheet formulas.
Now let's put it all together.
Practice your skills
Sign up to write and run code in this lesson.
Numbers & Types
Integers, floats, None, type checking, and arithmetic — the numeric foundation.
We've been using numbers since Week 1, but I've noticed sometimes I get 5 and sometimes 5.0. In my spreadsheets every number just... is a number. What's the difference in Python?
Good eye. Python has two number types. An integer (int) is a whole number with no decimal point. A float is a number with a decimal point. They look similar, but they behave differently in ways that matter when you're processing real data:
type(5) # <class 'int'>
type(5.0) # <class 'float'>
type(-3) # <class 'int'>
type(0.1) # <class 'float'>
The type() function tells you exactly what kind of value you're dealing with. Think of it like checking a cell's format in your spreadsheet — is it a whole number or a decimal?
OK, so when does the type actually matter? Like, when would this trip me up?
Division is the big one. Regular division with / always returns a float, even if the result is a whole number. This catches people off guard:
10 / 2 # 5.0 (float, not int!)
10 / 3 # 3.3333333333333335
10 // 3 # 3 (integer division — rounds down)
10 % 3 # 1 (modulo — the remainder)
The // operator is floor division — it divides and drops the decimal. The % operator gives you the remainder. These two are a powerful pair: // tells you how many times something fits, % tells you what's left over.
Here's a real example. Say you're tracking time for a project report:
minutes = 137
hours = minutes // 60 # 2
leftover = minutes % 60 # 17
print(f"{hours}h {leftover}m") # "2h 17m"
You've probably done this math in your head before — "137 minutes, that's 2 hours and 17 minutes." Now Python does it for you.
Can I convert between ints and floats? Like, if I get 5.0 but I need 5?
Yes — use int() and float() to convert explicitly:
int(7.9) # 7 — truncates, does NOT round
int(7.1) # 7
float(5) # 5.0
round(7.9) # 8 — this one rounds properly
round(3.14159, 2) # 3.14 — round to 2 decimal places
Notice that int() chops off the decimal — it always moves toward zero. If you want proper rounding (like you'd expect from a spreadsheet's ROUND function), use round() instead.
What about None? I keep seeing it pop up. Is that like an empty cell?
That's actually a great analogy. None is Python's way of saying "nothing" or "no value." It's not zero, not an empty string — it's the absence of a value entirely. Just like how an empty cell in your spreadsheet isn't the same as a cell containing 0:
type(None) # <class 'NoneType'>
result = None
print(result) # None
result is None # True
result == 0 # False — None is not zero
Functions that don't explicitly return something return None. You'll see it used as a default value or a placeholder for "not yet computed" — like marking a cell as "pending" in your tracking sheet.
How do I check the type of something? Like if I want to make sure an input is a number before doing math on it?
Use isinstance(). It's more flexible than type() because it also works with inheritance (you'll learn about that later):
isinstance(5, int) # True
isinstance(5.0, float) # True
isinstance(5, (int, float)) # True — checks multiple types
isinstance("5", int) # False — it's a string, not an int
This is what you'd use in real code. Think of it as data validation — the same way you'd set up a validation rule in your spreadsheet to make sure a column only contains numbers.
And I can use all the normal math operators? Same ones I use in spreadsheet formulas?
All of them. Here's the full set:
| Operator | Meaning | Example |
|---|---|---|
+ | addition | 5 + 3 = 8 |
- | subtraction | 5 - 3 = 2 |
* | multiplication | 5 * 3 = 15 |
/ | division (float) | 5 / 3 = 1.666... |
// | floor division | 5 // 3 = 1 |
% | modulo | 5 % 3 = 2 |
** | exponentiation | 5 ** 3 = 125 |
Python follows standard math order of operations — ** first, then *, /, //, %, then + and -. Use parentheses when in doubt. Same instinct you have with spreadsheet formulas.
Now let's put it all together.
Practice your skills
Sign up to write and run code in this lesson.