Numeric Types in Python
Python provides several built-in numeric types to represent numbers, each suited for different purposes. The main numeric types are:
- int (Integer)
- float (Floating-point number)
- complex (Complex numbers)
1. Integer (int)
- Represents whole numbers without a fractional part.
- Can be positive, negative, or zero.
- In Python 3,
int can be arbitrarily large (limited only by available memory).
Examples:
a = 10
b = -5
c = 0
print(type(a)) # <class 'int'>
print(a + b) # 5
print(c * 10) # 0
2. Floating-Point Number (float)
- Represents real numbers with decimal points.
- Stored in double precision (64-bit) internally.
- Can represent numbers like
3.14, -0.001, or scientific notation like 1.5e2 (which is 150.0).
Examples:
pi = 3.14159
gravity = 9.8
negative_float = -0.5
scientific = 1.23e4 # 12300.0
print(type(pi)) # <class 'float'>
print(pi + gravity) # 12.94159
print(scientific) # 12300.0
3. Complex Numbers (complex)
- Numbers with a real and imaginary part.
- Represented as
a + bj where a is the real part and b is the imaginary part.
- Useful in fields like engineering, physics, and signal processing.
Creating complex numbers:
z1 = 3 + 4j
z2 = complex(2, -3)
print(type(z1)) # <class 'complex'>
print(z1.real) # 3.0 (real part)
print(z1.imag) # 4.0 (imaginary part)
print(z1 + z2) # (5+1j)
Numeric Type Conversion
You can convert between numeric types using built-in functions:
int(x) — converts x to integer (truncates float)
float(x) — converts x to float
complex(x) or complex(a, b) — converts to complex number
Examples:
x = 3.7
print(int(x)) # 3 (truncates decimal)
y = 5
print(float(y)) # 5.0
a = 2
b = 3
z = complex(a, b)
print(z) # (2+3j)
Operations with Numeric Types
Python supports all standard arithmetic operators:
x = 10 # int
y = 3.5 # float
print(x + y) # 13.5 (float result)
print(x - y) # 6.5
print(x * y) # 35.0
print(x / y) # 2.857142857142857
print(x // y) # 2.0 (floor division)
print(x % y) # 3.0 (modulus)
print(x ** 2) # 100 (exponentiation)
Summary
| Numeric Type |
Description |
Example |
int |
Whole numbers, no decimals |
42, -7, 0 |
float |
Numbers with decimal points |
3.14, -0.001 |
complex |
Numbers with real & imaginary parts |
3 + 4j, 2 - 3j |