Look at the last bill you paid. It has whole numbers, such as the quantity of items, and decimals, such as the price and the tax. Any program that works with money, marks, distances, temperatures or measurements has to store numbers like these and do sums on them. Python makes this easy, but it stores different kinds of numbers in different ways, and knowing which is which saves you from confusing bugs.
Python has three main number types: int for whole numbers, float for numbers with a decimal point, and complex for numbers with a real and an imaginary part. You do not have to declare the type. Python looks at the value and decides. You can check the type at any time and convert from one type to another, but as you will see, not every conversion is allowed.
In this tutorial, we go through int, float and complex with real output from Python 3.13. We then cover how to check a type, how type conversion works, and why converting a complex number to int or float fails. We add the number functions you will use every day, the fact that True and False are numbers too, and the decimal and fractions modules for exact maths. We finish with the common mistakes, a quick reference and the complete script.
When you store a number in a variable, Python gives it one of three types, based on how you write it.
| Type | What it holds | Example | Typical use |
|---|---|---|---|
| int | Whole numbers, positive or negative, no decimal point | 10, -5, 0 | Counts, ages, quantities |
| float | Numbers with a decimal point | 14.942, 2.5e3 | Prices, measurements |
| complex | A real part and an imaginary part | 12+5j | Engineering, signal work |
An int is a whole number with no decimal part. It can be positive, negative or zero.
Python has no fixed limit on how big an int can get. It is limited only by the memory of your computer. Most languages overflow at some point, but Python does not:
You can put underscores inside a large number to make it easy to read. Python ignores them. You can also write an int in binary, octal or hexadecimal, and convert back with the bin, oct and hex functions.
A float is a number with a decimal point. Use it for prices, weights, distances and anything that is not always a whole number.
For very large or very small numbers, you can use scientific notation. The letter e means times ten to the power of.
Floats are stored in binary, and some decimals cannot be stored exactly in binary, in the same way one third cannot be written exactly in decimal. This is true in almost every programming language, not just Python.
The error is tiny, but it matters when you compare floats with the equals operator. Round the result, or use math.isclose, which returns True for 0.1 + 0.2 and 0.3.
A complex number has a real part and an imaginary part. In Python the imaginary part is written with the letter j, not i as in most maths books.
Python stores a complex number as two floats. You can read each part, and Python can do the maths on complex numbers directly:
Notice that the real and imaginary parts print as 12.0 and 5.0, floats, even though we typed whole numbers. The abs of a complex number is its size, so abs(3 + 4j) is 5.0. Most beginners will not need complex numbers often, but they show up in physics, electronics and signal processing.
Use the type function to see what Python thinks a value is. Use isinstance when you want a yes or no answer inside an if statement.
You can change a number from one type to another using the functions int, float, complex and str. This is called type conversion, or casting. Two of the four conversions below fail when given a complex number, so we show the error too.
Converting a float to an int cuts off the decimal part. It does not round. So 2.6 becomes 2 and minus 2.6 becomes minus 2. Now a complex number:
A complex number has no single whole number value, so Python refuses. If you only need one part, use int(y.real).
The same rule applies. Use y.real or y.imag to get a float out of a complex number.
With one number, complex gives a number with an imaginary part of zero. With two numbers, they become the real and imaginary parts.
The result looks the same on screen, but it is now text and not a number. You cannot do maths on it until you convert it back.
A very common error is passing the text "3.7" straight to int. Python raises a ValueError. Convert to float first, then to int.
These built in functions work on numbers, and you will use them almost every day.
| Function | What it does | Example and result |
|---|---|---|
| abs(x) | Removes the minus sign | abs(-7) gives 7 |
| pow(x, y) | x to the power y | pow(2, 5) gives 32 |
| round(x, n) | Rounds to n decimals | round(3.14159, 2) gives 3.14 |
| divmod(a, b) | Quotient and remainder together | divmod(17, 5) gives (3, 2) |
| max, min, sum | Largest, smallest, total | sum([1, 2, 3]) gives 6 |
| math.floor(x) | Rounds down | math.floor(2.7) gives 2 |
| math.ceil(x) | Rounds up | math.ceil(2.1) gives 3 |
| math.sqrt(x) | Square root | math.sqrt(16) gives 4.0 |
Look at the last line. The round function gives 2 for 2.5 and 4 for 3.5. Python rounds a number that sits exactly halfway to the nearest even number. This is called banker’s rounding, and it surprises people who expect 2.5 to become 3.
In Python, True and False are a subtype of int. True behaves as 1 and False as 0. That is why the three types are not quite the whole story.
When you cannot accept the small float error, for example in money calculations, Python has two modules that give exact answers. Pass the number as text to Decimal, so that it never goes through a float.
Use Decimal for money and Fraction when you need exact ratios. Both are slower than float, so use them only where exactness matters.
int(2.6) gives 2, because it cuts off the decimal. Use round if you want 3.real or imag part.float first.0.1 + 0.2 is not exactly 0.3. Use math.isclose."5" is not the number 5. Convert it first.| Type | Created by | Check or convert |
|---|---|---|
| int | w = 10 | type(w), int(“42”), int(2.6) |
| float | q = 14.942 | type(q), float(2), float(“3.14”) |
| complex | c = 12 + 5j | type(c), c.real, c.imag, complex(2, 3) |
| text | s = str(2.6) | str(5 + 6j) |
Here is the script that produces every output on this page. Copy it into a file, run it with Python 3, and compare the results.
Numbers look simple, but a few small facts decide whether your program is right: int cuts and does not round, complex cannot become int or float, floats carry a tiny error, and True is the number 1. Run the script above, change the values and read the errors, and you will remember these rules far better than by reading them. Then move on to Python strings and lists, where numbers are used as positions and counts.