Numbers in Python
Table of Contents
In the last lesson, we learned variable naming rules. Now letβs learn how Python works with numbers.
Numbers are everywhere.
Our age. Our height. The price of a coffee. The temperature outside. The score in a game.
And Python handles all of them. Easily.
But here is the thing. Python has more than one type of number. And they behave a little differently.
Let me show us.
π’ The Three Main Types of Numbers in Python
Python has three main types of numbers:
- Integers β whole numbers
- Floats β decimal numbers
- Complex numbers β we will barely touch these
Letβs look at the first two. They are the ones we will use every day.
π΅ Integers in Python
An integer is a whole number. No decimal point.
age = 25score = 100temperature = -5These are all integers. They can be positive, negative, or zero.
x = 10y = -20z = 0All integers. No decimal point.
Think of integers like counting things. One apple. Two books. Three cars. We cannot have half an apple in integer land. Well, we can in real life, but not in integers.
π‘ Floats in Python
A float is a number with a decimal point.
height = 5.9price = 19.99pi = 3.14159These are all floats. They have a decimal point.
βFloatβ is short for βfloating point number.β That is the technical name. But we do not need to remember that. Just remember β float means decimal.
Think of floats like measuring things. Our height is 5.9 feet. The temperature is 98.6 degrees. We can have parts of a whole.
π How to Tell Integers and Floats Apart
Here is the simple rule:
- Has a decimal point? It is a float.
- No decimal point? It is an integer.
x = 10 # integery = 10.0 # floatNotice that 10 and 10.0 are different types. Even though they have the same value. Python sees them differently.
We can check the type with the type() function:
print(type(10))print(type(10.0))Output
<class 'int'><class 'float'>int means integer. float means float. That is how Python labels them.
β Basic Math in Python
Python can do all the basic math we expect.
print(5 + 3) # 8print(10 - 4) # 6print(6 * 7) # 42print(20 / 5) # 4.0Wait. Look at that last one.
20 / 5 gave 4.0, not 4.
Why? Because division in Python always gives a float. Always. Even if the answer is a whole number.
This is important. Letβs remember it.
β Types of Division in Python
Python has two types of division.
Regular division /
Always gives a float.
print(10 / 2) # 5.0print(9 / 2) # 4.5print(10 / 3) # 3.3333333333333335Floor division //
Gives us the whole number part. It chops off the decimal.
print(10 // 2) # 5print(9 // 2) # 4print(10 // 3) # 3See? 9 // 2 is 4, not 4.5. It just removes the decimal part.
Think of it like this. Regular division is what our calculator shows. Floor division is how many times the number fits completely.
π§ Modulo β The Remainder
Python has an operator called modulo. It is written as %.
It gives us the remainder after division.
print(10 % 3) # 1Why? Because 3 goes into 10 three times (9), and 1 is left over.
print(10 % 2) # 0Because 2 goes into 10 exactly five times. No remainder.
Modulo is very useful. We will use it a lot later. For example, to check if a number is even or odd.
print(4 % 2) # 0 (even)print(5 % 2) # 1 (odd)If a number divided by 2 gives a remainder of 0, it is even. Otherwise, odd.
β‘ Power β Raising to a Power
We can raise a number to a power using **.
print(2 ** 3) # 8 (2 * 2 * 2)print(5 ** 2) # 25 (5 * 5)print(10 ** 0) # 1 (anything to the power 0 is 1)Simple.
π Python Numbers: A Real-Life Example
Think of a pizza.
We order 3 pizzas. Each pizza has 8 slices. How many slices do we have?
pizzas = 3slices_per_pizza = 8total_slices = pizzas * slices_per_pizzaprint(total_slices)Output
24Now imagine we have 10 friends. How many slices does each friend get?
friends = 10slices_each = total_slices // friendsprint(slices_each)Output
2Each friend gets 2 slices. And how many are left over?
leftover = total_slices % friendsprint(leftover)Output
44 slices left over. We can eat those ourselves.
That is how numbers work in real life. And Python handles them easily.
π§ Order of Operations in Python
Python follows the same math rules we learned in school.
That means multiplication and division happen before addition and subtraction.
print(2 + 3 * 4)What do we think this gives?
If we said 14, we are right.
Why? Because 3 * 4 happens first (12). Then 2 + 12 gives 14.
If we want to change the order, we use parentheses.
print((2 + 3) * 4)Now it gives 20. Because 2 + 3 happens first (5). Then 5 * 4 gives 20.
So letβs remember: parentheses first. Then multiplication and division. Then addition and subtraction.
Just like school.
π¬ Integers Can Be Very Large in Python
Here is something cool about Python.
In many languages, integers have a maximum size. If we go beyond that, things break.
In Python, integers can be as large as we want. Really. No limit.
big_number = 123456789012345678901234567890print(big_number)Output
123456789012345678901234567890Python handles it without any problem. That is one of the nice things about Python.
β οΈ Floats Have Limits
Floats are not perfect. They have a limit to how precise they can be.
For example:
print(0.1 + 0.2)What do we think this gives?
If we said 0.3, we are wrong.
It gives:
0.30000000000000004Why? Because computers store floats in a binary format. And some decimal numbers cannot be stored exactly in binary.
This is not a Python problem. It happens in every programming language. It is just how computers work.
For most everyday uses, this does not matter. But if we are doing money calculations, letβs be careful.
For now, letβs just remember β floats are not always exact. That is normal.
π Mixing Integers and Floats in Python
What happens if we mix them?
print(5 + 3.0)Output
8.0See? The result is a float. When we mix an integer and a float, the result is a float.
print(type(5 + 3)) # intprint(type(5 + 3.0)) # floatThat is the rule. Mixed operations give us a float.
π Converting Between Number Types
We can convert integers to floats and vice versa.
x = 10 # inty = float(x) # convert to floatprint(y) # 10.0
a = 10.9 # floatb = int(a) # convert to intprint(b) # 10Wait. int(10.9) gave 10, not 11.
Why? Because int() does not round. It just chops off the decimal part. Always.
If we want to round, use round().
print(round(10.9)) # 11print(round(10.4)) # 10round() rounds to the nearest whole number. int() just cuts off the decimal.
π§° Useful Math Functions in Python
Python has some built-in math functions.
print(abs(-5)) # 5 (absolute value)print(round(3.7)) # 4 (round to nearest)print(max(1, 5, 3)) # 5 (largest)print(min(1, 5, 3)) # 1 (smallest)print(sum([1, 2, 3])) # 6 (add all)These are handy. We will use them.
π‘ A Quick Tip About Python Numbers
Letβs be careful with division. Remember that / always gives a float. If we want a whole number, we use //.
And letβs be careful with int(). It does not round. It just removes the decimal.
Next, we will learn about Strings in Python so we can continue step by step.