In Python, numbers are one of the most commonly used data types. They are used to store numeric values like whole numbers, decimals, and even very large mathematical values. Every time you perform calculations, measure values, count items, or work with financial figures, you are using numbers.
Python supports different types of numbers, and each type behaves differently. In this section, we will explore everything about Python numbers—from the basics to more advanced features—using simple examples so that beginners, intermediates, and even experts can benefit.
What Are Numbers in Python?
Numbers in Python are immutable objects used to store numerical values.
Immutable means once a number is created in memory, it cannot be changed—you can only create new ones.
Python supports three main types of numeric data:
- int - integers (whole numbers)
- float - floating point numbers (decimals)
- complex - complex numbers (a + bj)
Python also supports:
- Booleans (True = 1, False = 0) — a subtype of integers
- Big integers (very large ints without limit except memory)
- Scientific notation for very large or tiny floats
Because Python is a high-level language, it automatically determines the appropriate number type based on the value you assign.
1. Python Integers (int)
Integers are whole numbers without a decimal point.
Examples include:
-10
0
55
1492
99999999999999999999999
Key Features of Integers
- Can be positive or negative.
- No length limit (Python handles very large integers automatically).
- Used for counting, indexing, loops, arithmetic, and more.
Creating Integer Variables
x = 10
y = -50
z = 0
print(type(x))
print(type(y))
print(type(z))
Output
< class 'int' >
< class 'int' >
< class 'int' >
Integer Memory Behavior
Each time you create a new integer, Python assigns it a memory location.
Integers between -5 and 256 are pre-cached by Python for faster performance.
This explains why these numbers may sometimes have the same id().
Example
a = 10
b = 10
c = 300
d = 300
print(id(a), id(b)) # same id
print(id(c), id(d)) # usually different ids
2. Python Floats (float)
Floats are numbers with decimal points.
3.14
0.5
-10.75
Python stores floats using double-precision (64-bit) floating point format, following the IEEE-754 standard.
Creating Floats
x = 10.5
y = -2.25
z = 0.0
print(type(x))
print(type(y))
print(type(z))
Scientific Notation
Python allows you to write very large or very small numbers using scientific notation with e or E.
a = 5e3 # 5 x 10³ = 5000
b = 2.5e-4 # 2.5 x 10⁻⁴ = 0.00025
print(a)
print(b)
Floating Point Precision
Floats can suffer from rounding issues because they are stored in binary.
print(0.1 + 0.2)
Output
0.30000000000000004
3. Python Complex Numbers (complex)
Complex numbers consist of:
- real part, and
- imaginary part
Python uses the letter j to represent the imaginary part.
x = 3 + 5j
y = -2j
z = 10 - 4j
Checking type:
print(type(3+5j)) # < class 'complex' >
Accessing real and imaginary parts:
num = 4 + 7j
print(num.real) # 4.0
print(num.imag) # 7.0
Complex Number Operations
a = 2 + 3j
b = 1 + 4j
print(a + b)
print(a - b)
print(a * b)
print(a / b)
4. Type Conversion (Casting Numbers)
Python allows converting between number types:
int() — Convert to Integer
x = int(10.9) # becomes 10
y = int("20") # becomes 20
print(x, y)
float() — Convert to Float
x = float(10) # 10.0
y = float("20.5") # 20.5
print(x, y)
complex() — Convert to Complex
x = complex(10) # 10+0j
y = complex(3, 5) # 3+5j
print(x, y)
5. Number Operators in Python
Python supports many arithmetic operations.
Basic Arithmetic
| Operator | Description | Example |
|---|---|---|
| + | Addition | 10 + 5 = 15 |
| - | Subtraction | 10 - 5 = 5 |
| * | Multiplication | 10 * 5 = 50 |
| / | Division | 10 / 5 = 2.0 |
| // | Floor division | 7 // 2 = 3 |
| % | Modulus (remainder) | 7 % 2 = 1 |
| ** | Exponent (power) | 2 ** 3 = 8 |
Example
a = 15
b = 4
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
6. Built-in Number Functions
Python provides useful functions for working with numbers.
abs() — Absolute Value
print(abs(-10)) # 10
pow() — Exponent
print(pow(2, 3)) # 8
round() — Rounding Numbers
print(round(3.14159, 2)) # 3.14
divmod() — Division + Remainder
print(divmod(7, 3)) # (2, 1)
7. Random Numbers in Python
Python uses the random module to generate pseudo-random values.
import random
print(random.random()) # float between 0 and 1
print(random.randint(1, 10)) # int between 1 and 10
print(random.choice([1,2,3,4]))
8. Complex Number Functions (cmath Module)
Python provides a special module for complex math:
import cmath
print(cmath.sqrt(-1)) # 1j
9. Checking Number Types
Use isinstance() to check types:
x = 10
print(isinstance(x, int))
y = 10.5
print(isinstance(y, float))
z = 3+2j
print(isinstance(z, complex))
10. Boolean Numbers
Booleans are a subset of integers:
True == 1
False == 0
Example
print(True + True) # 2
print(False + 5) # 5
11. Large Integer Handling in Python
Python automatically converts large integers to unlimited precision.
x = 999999999999999999999999
y = x * x
print(y)
This is very useful for cryptography, scientific calculations, and algebraic problems.
Summary of Number Types
| Number Type | Description | Example |
|---|---|---|
| int | Whole numbers | 5, -10, 0 |
| float | Decimal numbers | 3.14, -0.5 |
| complex | Numbers with real & imaginary parts | 2+3j |
| bool | Subtype of int | True, False |
Conclusion
Python Numbers form the backbone of most operations in programming—from simple arithmetic to scientific calculations. In this chapter, we covered:
- Number types (int, float, complex)
- Type conversions
- Arithmetic operators
- Built-in number functions
- Random numbers
- Boolean behavior
- Large integers
- Complex number operations
With this foundation, you are ready to move into more advanced numeric operations in Python.