Numbers are used to store numeric values and perform mathematical operations such as addition, subtraction, multiplication and division. Python provides different numeric data types to work with whole numbers, decimal values and complex numbers.
a = 4
b = 4.5
c = 4j
print(type(a))
print(type(b))
print(type(c))
Output
<class 'int'> <class 'float'> <class 'complex'>
Types of Numbers
Python mainly supports the following number types:
1. Integer
An integer (int) represents whole numbers without decimal values. Integers can be positive, negative or zero. Python allows storing very large integer values without any fixed size limit.
x = 5 # Positive integer
y = -23 # Negative integer
z = 0 # Zero
Arithmetic Operations on Integers
Python supports different arithmetic operations on integer values such as addition, subtraction, multiplication and division.
print(5 + 3)
print(10 - 4)
print(7 * 6)
print(15 / 4)
print(15 % 4)
print(2 ** 3)
print(abs(-10))
print(round(3.14159, 2))
Output
8 6 42 3.75 3 8 10 3.14
2. Float
A float (float) represents numbers with decimal values. Float numbers can be positive, negative or zero. They are commonly used when more precise numeric values are needed, such as measurements or calculations involving decimals.
a = 3.14 # Positive float
b = -0.99 # Negative float
c = 0.0 # Float value representing zero
Arithmetic Operations on Float
Python supports different arithmetic operations on float values similar to integers.
print(3.5 + 2.2)
print(7.8 - 4.3)
print(5.5 * 2.0)
print(9.0 / 4.5)
print(9.0 // 4.5)
print(9.0 % 4.5)
print(2.5 ** 2)
print(abs(-7.4))
print(round(3.14159, 2))
Output
5.7 3.5 11.0 2.0 2.0 0.0 6.25 7.4 3.14
Note: Float values are accurate up to about 15 decimal places. Beyond that, small precision differences may occur.
3. Complex Numbers
A complex number (complex) consists of two parts: Real part and Imaginary part. The imaginary part is represented using j. Complex numbers are commonly used in scientific and mathematical calculations.
a = 2 + 3j
Here, 2 is the real part and 3j is the imaginary part.
Arithmetic Operations on Complex Numbers
Python supports different arithmetic operations on complex numbers.
print((3 + 4j) + (1 + 2j))
print((5 + 6j) - (2 + 3j))
print((2 + 3j) * (1 + 4j))
print((8 + 6j) / (2 + 3j))
print((1 + 1j) ** 2)
print(abs(3 + 4j))
print((3 + 4j).conjugate())
print((3 + 4j).real)
print((3 + 4j).imag)
Output
(4+6j) (3+3j) (-10+11j) (2.6153846153846154-0.9230769230769231j) 2j 5.0 (3-4j) 3.0 4.0
To know about Type Conversion, please refer to this article Type Conversion
Random Numbers
Python provides the random module to generate random numbers. Random values are useful in applications such as games, simulations, password generation and testing.
- random.randint(a, b) generates a random integer between a and b (both inclusive).
- random.uniform(a, b) generates a random floating-point number between a and b.
import random
print(random.randint(1, 100))
print(random.uniform(1.0, 10.0))
Output
29 3.8144379848390937
Note: Output may be different each time the program runs.
Special Numbers
Python provides special numeric values that are used in scientific calculations and situations where normal numbers cannot represent a result.
- NaN (Not a Number): Represents an undefined or invalid numeric result.
- Infinity: Represented using float('inf'), used for values greater than any finite number.
- Negative Infinity: Represented using float('-inf'), used for values smaller than any finite number.
import math
print(math.nan)
print(float('inf'))
print(float('-inf'))
Output
nan inf -inf