Numbers (int, float, complex)
Learn how Python handles whole numbers, decimals, and the arithmetic operators that let you do real math in your programs.
Numbers are everywhere in programs — counting items in a cart, calculating a tip, measuring pixels on a screen. Python is exceptionally good at math, and it gives you several types of numbers to match real-world needs. In this lesson you will learn the three number types Python offers, how the arithmetic operators work, and a few quirks that trip up beginners.
See it in action
— step through the idea, then dive into the details below.Python Loves Numbers
Numbers are everywhere in code — scores, prices, pixels. Python gives you three built-in number types so you always have the right tool for the job.
#Integers — Whole Numbers
An integer (type int) is any whole number — positive, negative, or zero. There is no maximum size in Python; you can work with numbers as large as your computer's memory allows.
0,42,-7,1000000are all integers.- You never write a decimal point for an integer.
apples = 5
bananas = 12
total = apples + bananas
print(total)
print(type(total))Big numbers? Use underscores!
Python lets you write large integers with underscores as visual spacers — just like commas in everyday writing. 1_000_000 is the same as 1000000, but much easier to read at a glance.
world_population = 8_100_000_000
print(world_population)#Floats — Decimal Numbers
A float (type float) is a number with a decimal point. The name comes from floating-point, the way computers store decimal values internally.
3.14,0.5,-2.7,1.0are all floats.- Even
1.0is a float — the dot makes all the difference.
price = 9.99
tax_rate = 0.08
tax = price * tax_rate
print(round(tax, 2))
print(type(price))0.1 + 0.2 is not exactly 0.3
Try this in Python:
``python print(0.1 + 0.2) ``
You will see 0.30000000000000004. This is not a Python bug — it is how all computers store decimals in binary. Most of the time it does not matter, but if you need exact decimal arithmetic (e.g., for money), look into the decimal module later. For now, use round() when displaying results to users.
#Arithmetic Operators
Python supports the arithmetic you already know, plus a few extra operators that are especially useful in programming.
a = 17
b = 5
print(a + b) # addition
print(a - b) # subtraction
print(a * b) # multiplication
print(a ** b) # power (17 to the 5th)
print(a % b) # modulo (remainder)Modulo is like a clock
Imagine a clock with 5 hours. After hour 5 you wrap back to 0. 17 % 5 asks: after filling as many groups of 5 as possible into 17, what is left over? 17 = 3 * 5 + 2, so the answer is 2. Modulo is great for checking whether a number is even (n % 2 == 0) or cycling through a fixed set of options.
#Division — Three Ways
Division in Python comes in three flavours, and mixing them up is a classic beginner mistake.
a = 17
b = 5
print(a / b) # true division — always a float
print(a // b) # floor division — rounds DOWN to int
print(-17 // 5) # floor division goes toward negative infinity/ always gives a float
10 / 2 returns 5.0, not 5. If you need a whole number result, use // instead. Many beginners are surprised that dividing two integers can produce a float.
#Helpful Built-in Functions
print(abs(-42)) # absolute value
print(round(3.74159, 2)) # round to 2 decimal places
print(round(2.5)) # rounds to nearest even number#A Peek at the math Module
Python's built-in math module gives you access to powerful mathematical functions and constants. You bring it in with import math. We will cover modules properly in a later lesson — for now, here is a taste.
import math
print(math.pi) # the constant π
print(math.sqrt(144)) # square root
print(math.floor(3.9)) # round down
print(math.ceil(3.1)) # round up#Complex Numbers (just a quick look)
Python also has a built-in complex number type for situations like electrical engineering or advanced mathematics. You write them with a j suffix for the imaginary part. Most beginners will not need these for a long time — but it is nice to know Python has them.
c = 3 + 4j
print(c)
print(type(c))
print(c.real, c.imag)What does 15 // 4 evaluate to in Python?
Key takeaways
- `int` holds whole numbers of any size; `float` holds decimals.
- Use underscores in big literals like `1_000_000` to improve readability.
- `/` always returns a float; use `//` for integer (floor) division and `%` for the remainder.
- Floating-point arithmetic is not always exact — use `round()` when displaying results.
- The `math` module provides advanced functions like `sqrt()` and constants like `pi`.
What does this code print?
a = 17
b = 5
print(a / b)
print(a // b)
print(a % b)This code is supposed to print the integer result of dividing 20 by 4, but it prints the wrong type. What should be changed?
result = 20 / 4
print(result)
print(type(result))Complete the code so it prints the absolute value of -99 and then the result of 2 to the power of 8.
print((-99)) print(2 8)
Put these lines in the right order to import math and print the square root of 225 rounded to 1 decimal place.
result = math.sqrt(225)
import math
print(round(result, 1))
Write a small program that calculates the area and circumference of a circle. Ask for nothing — just hard-code a radius of 7. Use math.pi for π. Print both results rounded to 2 decimal places.
Formulas: - Area = π × r² - Circumference = 2 × π × r
Try it live — edit the code and hit Run to execute real Python: