Numbers in Python
Table of Contents + −
In the last lesson you learned Variable Naming Rules, so you can give your data a good name. Now let’s put real values inside those names. Almost every program counts something, or adds up prices, or measures a distance. That means numbers. So let’s get comfortable with how Python handles them.
🤔 Why Number Types Matter
Here is the pain. You write a program to split a restaurant bill. The answer comes out as 33 when it should be 33.33. Or you try to store a huge number, and in another language it would overflow and break. The fix is simple. Python has a few number types. Once you know which one you are working with, these surprises stop being surprises.
A type is just the kind of value something is. A whole number is one kind. A decimal number is another kind. Python keeps them apart because the rules for each are a little different.
🔢 The Three Number Types
Python gives you three kinds of numbers. Think of them like different tools in a toolbox.
- int is for whole numbers. No decimal point. Like
7,-200, or0. - float is for numbers with a decimal point. Like
3.14,0.5, or-99.9. - complex is for math with imaginary numbers. You will rarely touch this one unless you do science or engineering work.
Here is a quick look at all three, so you can see them side by side.
age = 30 # int, a whole numberprice = 19.99 # float, has a decimal pointpoint = 2 + 3j # complex, the j marks the imaginary part
print(age)print(price)print(point)Output
3019.99(2+3j)The j in the last line is how Python writes the imaginary part of a complex number. Do not worry about it for now. Most days you will only ever use int and float.
Note
Python has no limit on how big an int can get. You can multiply huge numbers together and Python just keeps going. Many other languages crash or give wrong answers when a number gets too big. So this is a nice thing to have.
➕ Basic Arithmetic
You already know most of this from school. Python uses the normal math symbols for the everyday operations.
This example runs each basic operation and prints the result.
print(10 + 3) # additionprint(10 - 3) # subtractionprint(10 * 3) # multiplicationprint(10 % 3) # remainder (what is left over)Output
137301The first three are plain math. The last one, %, is called modulo. It gives you the remainder after dividing. So 10 % 3 is 1. That is because 3 goes into 10 three times with 1 left over. It sounds small, but it is handy. For example, a number is even when number % 2 equals 0.
➗ Division Has Two Flavors
Division is where people get confused, so let’s slow down here. Python gives you two ways to divide, and they do different things.
The single slash / is normal division. The double slash // throws away the decimal part and keeps only the whole-number result.
This example shows both kinds of division on the same numbers.
print(10 / 3) # normal divisionprint(10 // 3) # floor division, whole-number resultprint(10 / 2) # still gives a floatOutput
3.333333333333333535.0Look at the last line carefully. 10 / 2 is exactly 5, but Python prints 5.0, not 5. That is the key rule to remember.
Tip
The single slash / always gives a float, even when the numbers divide evenly. So 4 / 2 is 2.0, not 2. If you want a clean whole number, use // instead.
The double slash // is called floor division. “Floor” means it rounds down to the nearest whole number. So 10 // 3 is 3, because it drops the .333 part. This is great when you only care about how many whole groups fit. Think of how many full boxes you can pack.
🔼 Powers with the Double Star
Need to square a number, or raise it to a power? Python uses two stars ** for that.
This example raises numbers to different powers.
print(2 ** 3) # 2 to the power of 3, so 2 * 2 * 2print(5 ** 2) # 5 squaredprint(9 ** 0.5) # the square root, power of one halfOutput
8253.0So 2 ** 3 means 2 multiplied by itself 3 times. And here is a neat trick. Raising a number to the power of 0.5 gives you its square root. That is why 9 ** 0.5 comes out as 3.0.
🏷️ Checking the Type with type()
Sometimes you are not sure whether a value is an int or a float. Python has a built-in tool called type() that tells you. You hand it a value, and it tells you what kind it is.
This example checks the type of a few different numbers.
print(type(7))print(type(7.0))print(type(10 / 2))print(type(10 // 3))Output
<class 'int'><class 'float'><class 'float'><class 'int'>Read each line as “this value is of class int” or “class float”. Notice how 10 / 2 reports as a float, even though the answer is a clean 5. And 10 // 3 stays an int. So type() is a quick way to confirm what you actually have, instead of guessing.
🔄 Converting Between int and float
Often you have a number in one type, and you need it in another. Maybe you read a price as text and want a decimal. Or you have a float and want to chop off the decimal part. Python lets you convert with int() and float().
This example turns a float into an int, and an int into a float.
price = 19.99rounded_down = int(price) # turns float into intprint(rounded_down)
count = 5as_float = float(count) # turns int into floatprint(as_float)Output
195.0Here is the part to watch. When you use int() on a float, it does not round to the nearest whole number. It just drops everything after the decimal point. So int(19.99) becomes 19, not 20. Keep that in mind, or you will lose money in a price calculation without noticing.
Caution
int() cuts off the decimal, it does not round. So int(2.9) is 2, not 3. If you actually want to round to the nearest whole number, use round(2.9) instead, which gives 3.
⚠️ The Floating-Point Surprise
This one catches everybody at least once, so let me warn you early. Try adding 0.1 and 0.2 in Python.
This example adds two simple decimals and prints the result.
print(0.1 + 0.2)Output
0.30000000000000004Wait, what? You expected 0.3, right? You are not going crazy, and Python is not broken. The thing is, computers store decimals in binary. Some decimals cannot be written exactly in binary. So a tiny rounding error sneaks in. Almost every programming language does this, not just Python.
For now, just know it can happen. When you need an exact answer, like with money, you round the result yourself.
This example rounds the answer to two decimal places to get the clean value.
print(round(0.1 + 0.2, 2))Output
0.3The round() function takes the number first, then how many decimal places you want. So round(0.1 + 0.2, 2) gives you a clean 0.3.
⚠️ Common Mistakes
A few traps catch people again and again. Watch out for these.
- Expecting
/to give a whole number. It never does. Use//when you want a whole-number result.
# ❌ Avoid: expecting a clean integertotal = 10 / 2 # this is 5.0, a float
# ✅ Good: use floor division for a whole numbertotal = 10 // 2 # this is 5, an int- Thinking
int()rounds. It only cuts off the decimal part.
# ❌ Avoid: expecting int() to round upprice = int(9.99) # this is 9, not 10
# ✅ Good: use round() when you want the nearest whole numberprice = round(9.99) # this is 10- Comparing floats for an exact match. Because of the rounding surprise,
0.1 + 0.2 == 0.3is actuallyFalse. Round both sides first, or check that the difference is tiny.
✅ Best Practices
Keep these small habits, and your number code stays clean.
- Use
intfor things you count, like people, items, or clicks. Usefloatfor things you measure, like price, weight, or distance. - Reach for
//when you only need the whole-number part of a division. It says what you mean clearly. - When money is involved, round the final result with
round(value, 2)so cents come out right. - If you are unsure what type a value is, print
type(value)and check. It takes a second and saves confusion.
🧩 What You’ve Learned
✅ Python has three number types: int for whole numbers, float for decimals, and complex for imaginary math.
✅ int has no size limit, so big numbers never overflow.
✅ The single slash / always gives a float, the double slash // gives a whole-number result, and ** raises to a power.
✅ type() tells you whether a value is an int or a float.
✅ int() and float() convert between types, and int() cuts off the decimal instead of rounding.
✅ Decimals like 0.1 + 0.2 can have a tiny rounding error, so round the result when you need an exact answer.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does 10 / 2 give you in Python?
Why: The single slash always produces a float, so even an even division gives 5.0 instead of 5.
- 2
Which operator gives you a whole-number result from a division?
Why: The double slash // is floor division, which drops the decimal part and keeps the whole number.
- 3
What is the result of int(2.9)?
Why: int() cuts off the decimal part rather than rounding, so int(2.9) is 2, not 3.
- 4
Why does 0.1 + 0.2 print 0.30000000000000004?
Why: Computers store decimals in binary, and some values cannot be represented exactly, which adds a tiny rounding error.
🚀 What’s Next?
You can handle numbers now, so let’s move on to text. Next you will learn how Python stores and works with words and sentences.