Python Numeric Data Types
Python provides three built-in numeric data types: int, float and complex. These data types are used to represent whole numbers, decimal numbers and complex numbers.
Types of Numeric Data
| Data Type | Description | Example |
|---|---|---|
int |
Whole numbers without a decimal point | 25 |
float |
Numbers containing a decimal point | 25.5 |
complex |
Numbers containing real and imaginary parts | 3 + 4j |
1. Integer (int)
The int data type represents whole numbers.
Integers can be positive, negative or zero.
a = 25
b = -10
c = 0
print(a)
print(b)
print(c)
25 -10 0
Checking Integer Type
number = 100
print(type(number))
<class 'int'>
Large Integers
Python integers can represent very large whole numbers. There is no fixed 32-bit or 64-bit integer limit for normal Python integers; the practical limit depends on available memory.
number = 123456789012345678901234567890
print(number)
123456789012345678901234567890
2. Floating-Point (float)
The float data type represents numbers containing a decimal
point. It is commonly used for measurements, prices, percentages and
other decimal calculations.
price = 99.50
temperature = 36.5
percentage = 85.75
print(price)
print(temperature)
print(percentage)
99.5 36.5 85.75
Checking Float Type
value = 25.75
print(type(value))
<class 'float'>
Scientific Notation
Python allows floating-point numbers to be written using scientific
notation. The letter e or E represents a
power of 10.
x = 2.5e3
y = 4.2e-2
print(x)
print(y)
2500.0 0.042
3. Complex Numbers (complex)
A complex number consists of a real part and an
imaginary part. Python uses j to represent
the imaginary part.
z = 3 + 4j
print(z)
print(type(z))
(3+4j) <class 'complex'>
Real and Imaginary Parts
The real and imag attributes can be used
to access the real and imaginary parts of a complex number.
z = 5 + 3j
print("Real part:", z.real)
print("Imaginary part:", z.imag)
Real part: 5.0 Imaginary part: 3.0
Arithmetic Operations on Integers
Python supports several arithmetic operations on numeric values.
a = 10
b = 3
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Power:", a ** b)
Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3.3333333333333335 Floor Division: 3 Modulus: 1 Power: 1000
Arithmetic Operations on Float Values
a = 10.5
b = 2.5
print(a + b)
print(a - b)
print(a * b)
print(a / b)
13.0 8.0 26.25 4.2
Arithmetic Operations on Complex Numbers
Complex numbers support addition, subtraction, multiplication and division.
z1 = 2 + 3j
z2 = 4 + 5j
print("Addition:", z1 + z2)
print("Subtraction:", z1 - z2)
print("Multiplication:", z1 * z2)
Addition: (6+8j) Subtraction: (-2-2j) Multiplication: (-7+22j)
Using abs() with Numeric Values
The abs() function returns the absolute value of a number.
For a complex number, it returns its magnitude.
print(abs(-25))
print(abs(-12.5))
z = 3 + 4j
print(abs(z))
25 12.5 5.0
Using pow()
The pow() function can be used to calculate the power
of a number.
print(pow(2, 5))
print(pow(10, 2))
32 100
Using round()
The round() function is used to round a floating-point
value.
number = 12.6789
print(round(number))
print(round(number, 2))
13 12.68
Comparing Numeric Values
Numeric values can be compared using comparison operators such as
==, !=, >, <,
>= and <=.
a = 20
b = 10
print(a > b)
print(a == b)
print(a != b)
True False True
Checking Numeric Data Types
The type() function can be used to identify the numeric
data type of a value.
a = 10
b = 10.5
c = 2 + 3j
print(type(a))
print(type(b))
print(type(c))
<class 'int'> <class 'float'> <class 'complex'>
Key Differences
| Feature | int | float | complex |
|---|---|---|---|
| Example | 10 |
10.5 |
2 + 3j |
| Decimal Part | No | Yes | May contain real and imaginary parts |
| Imaginary Part | No | No | Yes |
| Common Use | Counting and whole numbers | Measurements and decimal calculations | Complex mathematical calculations |
Important Points
- Python has three main built-in numeric data types:
int,floatandcomplex. intrepresents whole numbers.floatrepresents decimal numbers.complexrepresents numbers with real and imaginary parts.- Python uses
jfor the imaginary part of a complex number. - The
type()function can be used to check the type of a numeric value. - Numeric values can be used with arithmetic and comparison operators.
int, float and
complex as its built-in numeric data types.
These types allow Python programs to perform integer, decimal
and complex-number calculations.