Python Numeric Data Types
Numeric Data Types
Numbers are at the heart of most programs — prices, quantities, scores, measurements, coordinates, and percentages are all numbers. Python supports three built-in numeric data types:
| Type | Description | Example |
|---|---|---|
int | Whole numbers (positive, negative, or zero) | 10, -890, 0 |
float | Numbers with a decimal point | 4.75, -0.5, 100.0 |
complex | Numbers with a real part and an imaginary part | 6 + 2j |
A numeric variable is created automatically when you assign a numeric value to it. You never need to declare the type.
Creating Numeric Variables
a = 10 # integer
b = 4.75 # float
c = 6 + 2j # complex
Python decides the type from how the number is written:
- No decimal point →
int - A decimal point (or scientific notation) →
float - A
jsuffix →complex
Checking the Type of a Number
Use the type() function to find out which numeric type a variable belongs to.
a = 10
b = 4.75
c = 6 + 2j
print(type(a))
print(type(b))
print(type(c))
Expected output:
<class 'int'>
<class 'float'>
<class 'complex'>
Integer Numbers (int)
An integer is a whole number without a decimal part. It can be positive, negative, or zero.
num1 = 25
num2 = -890
num3 = 9876543210123456789
print(type(num1))
print(type(num2))
print(type(num3))
Expected output:
<class 'int'>
<class 'int'>
<class 'int'>
Python Integers Have No Fixed Size Limit
In many languages, integers have a fixed size (for example, 32 or 64 bits), and values that are too large "overflow". Python integers grow as needed and are limited only by your computer's available memory:
big_number = 2 ** 100
print(big_number)
Expected output:
1267650600228229401496703205376
Writing Large Integers Readably
You can use underscores to group digits. Python ignores them; they only make the number easier to read.
population = 1_400_000_000
print(population)
Expected output:
1400000000
Common Uses of Integers
Counting items, indexing positions in a list, loop counters, ages, and IDs are all typically stored as integers.
Floating-Point Numbers (float)
A float is a number that contains a decimal point. Floats are used for values that need fractional precision, such as prices, measurements, and averages.
Decimal Values
value1 = 3.14
value2 = -0.5
value3 = 100.0
print(type(value1))
print(type(value2))
print(type(value3))
Expected output:
<class 'float'>
<class 'float'>
<class 'float'>
Notice that 100.0 is a float, not an integer, because it includes a decimal point — even though its value is a whole number.
Scientific Notation
Floats can also be written in scientific notation, using e or E to mean "times 10 to the power of".
| Written As | Meaning | Value |
|---|---|---|
4.2e3 | 4.2 × 10³ | 4200.0 |
9E2 | 9 × 10² | 900.0 |
-6.1e-4 | -6.1 × 10⁻⁴ | -0.00061 |
num1 = 4.2e3
num2 = 9E2
num3 = -6.1e-4
print(num1, num2, num3)
print(type(num1))
print(type(num2))
print(type(num3))
Expected output:
4200.0 900.0 -0.00061
<class 'float'>
<class 'float'>
<class 'float'>
Even 9E2, which has no decimal point, is a float, because scientific notation always produces a float.
Scientific notation is convenient for very large or very small values, such as distances in astronomy or measurements in physics.
Floating-Point Precision
Floats are stored in binary, and many decimal fractions (such as 0.1) cannot be represented exactly in binary. As a result, some calculations produce tiny rounding errors:
print(0.1 + 0.2)
Expected output:
0.30000000000000004
This is not a bug in Python; the same behavior occurs in almost every programming language. When displaying results, round them:
print(round(0.1 + 0.2, 2))
Expected output:
0.3
Tip: Avoid comparing floats with
==. Usemath.isclose(a, b)instead. For money calculations that must be exact, use thedecimalmodule from Python's standard library.
Complex Numbers (complex)
A complex number has a real part and an imaginary part. In Python, the imaginary part is written with the letter j (mathematics usually uses i).
x = 2 + 7j
y = -3j
z = 9 - 4j
print(type(x))
print(type(y))
print(type(z))
Expected output:
<class 'complex'>
<class 'complex'>
<class 'complex'>
Accessing the Real and Imaginary Parts
x = 2 + 7j
print(x.real)
print(x.imag)
Expected output:
2.0
7.0
Both parts are stored as floats. Complex numbers are mainly used in scientific, engineering, and signal-processing applications; most everyday programs only need int and float.
Arithmetic With Mixed Numeric Types
When you combine different numeric types in a calculation, Python automatically converts the result to the "wider" type: int → float → complex.
print(5 + 2.0)
print(type(5 + 2.0))
print(7 / 2)
print(7 // 2)
Expected output:
7.0
<class 'float'>
3.5
3
5 + 2.0mixes an int and a float, so the result is a float./(true division) always returns a float, even when both numbers are integers.//(floor division) returns the whole-number part of the division.
Converting Between Numeric Types
Python provides built-in functions to convert numbers from one type to another: int(), float(), and complex().
i = 12 # integer
f = 8.9 # float
p = float(i) # int to float
q = int(f) # float to int
r = complex(i) # int to complex
print(p)
print(q)
print(r)
print(type(p))
print(type(q))
print(type(r))
Expected output:
12.0
8
(12+0j)
<class 'float'>
<class 'int'>
<class 'complex'>
What to notice:
float(12)adds a decimal part:12.0.int(8.9)gives8, not9. Converting a float to an integer removes the decimal part; it does not round.complex(12)creates a complex number with an imaginary part of0.
Note: Complex numbers cannot be converted into integers or floats.
int(3 + 4j)raises aTypeError. To get a single number from a complex value, use its.realor.imagattribute, orabs()to get its magnitude.
Generating Random Numbers
Python does not have a built-in function named random(), but it includes a standard-library module called random for generating random numbers. A module is a file of ready-made code that you load with import.
import random
print(random.randint(1, 20))
Example output (your result will differ each time):
14
random.randint(1, 20) returns a random integer between 1 and 20, including both 1 and 20.
Other Useful random Functions
import random
print(random.random()) # Float from 0.0 up to (not including) 1.0
print(random.uniform(1.5, 3.5)) # Float between 1.5 and 3.5
print(random.choice([10, 20, 30])) # Random item from a list
Example output:
0.6394267984578837
2.871234019832644
20
Practical Example: Rolling a Die
import random
roll = random.randint(1, 6)
print("You rolled a", roll)
Example output:
You rolled a 4
Security note: The
randommodule is suitable for games and simulations, but not for passwords or security tokens. For security purposes, use thesecretsmodule.
Common Mistakes
| Mistake | Example | What Happens |
|---|---|---|
Expecting int() to round | int(8.9) | Returns 8, not 9. Use round(8.9) to get 9. |
Expecting / to return an int | 10 / 2 | Returns 5.0 (a float). Use // for integer results. |
Comparing floats with == | 0.1 + 0.2 == 0.3 | Returns False due to precision errors |
Using i for imaginary numbers | 2 + 3i | SyntaxError. Python uses j. |
| Converting a complex number to int | int(2 + 3j) | TypeError |
Related Concepts
- Python Type Casting — converting between numbers and strings
- Python Operators — arithmetic operators such as
+,-,*,/,//,%, and** - Python Math — built-in math functions and the
mathmodule