Python Numbers
Python provides three built-in numeric data types to represent different kinds of numbers: int
, float
, and complex
.
Integers (int
)
Integers are whole numbers without any decimal points. They can be positive, negative, or zero.
For example:
x = 22 # A positive integer
y = -10 # A negative integer
z = 0 # A zero integer
print(type(x)) # <class 'int'>
The type()
function returns the data type of the given object.
Integers in Python have arbitrary precision, meaning they can be as large as your computer’s memory allows. You don’t need to worry about overflow errors like in some other langauges with fixed-size integer types.
For example:
# A large integer in Python
large_number = 9999999999999999999999999999999999999999
Floating-point Numbers (float
)
Floats (floating-point numbers) are numbers that have a decimal point or are written in exponential (scientific) notation.
For example:
x = 22.5 # A positive float
y = -3.7 # A negative float
z = 0.0 # A zero float
Scientific Notation
Python’s float
supports scientific notation, where a number is represented in power of ten.
For example:
x = 2e2
print(x) # Output: 200.0
In the above example, e
stands for the exponent which means “times ten to the power of”. So, 2e2
is equivalent to 2 × 10²
, which is 200.0
. Also notice that the scientific notation in Python always results in a float
.
Complex Numbers (complex
)
Complex numbers represents numbers of the form a + bj
, where a
is the real part, b
is the imaginary part, and j
is the imaginary unit.
For example:
complex_num = 3 + 4j
Type Conversion in Python
When working with numbers, you may need to convert one data type to another. You can easily do this using the built-in functions.
int()
: Converts a number or numeric string to an integer (truncates decimals if converting from a float).
float()
: Converts a number or numeric string to a float.
complex()
: Converts numbers or strings to a complex number.
For example:
# Converting to int
x = int(3.14) # 3 (truncates decimal)
y = int("55") # 55
z = int(True) # 1
# Converting to float
a = float(3) # 3.0 (adds decimal part)
b = float("55.27") # 55.27
c = float(False) # 0.0
# Converting to complex
i = complex(5) # (5+0j)
j = complex(3.9) # (3.9+0j)
k = complex(2, 3) # (2+3j)
l = complex ("3-2j") # (3-2j)
You cannot convert complex numbers to int
or float
directly. Doing so will raise a TypeError
.
Implicit Type Conversion (Automatic Type Conversion)
Python automatically converts numeric types during operations when necessary. For example, integers are converted to floats to maintain precision and avoid data loss.
For example:
x = 5 # int
y = 3.2 # float
sum = x + y
print(sum) # Output: 8.2
In the above example, Python automatically converts the integer x
to float
(5.0
) during the addition. This is because mixing int
and float
in an arithmetic operation results in a float
to preserve precision.
If an operation involves a complex number, the result will also be complex.
For example:
a = 5 # int
b = 2 + 3j # complex
# Adding integer and complex number (integer is converted to complex)
c = a + b
print(c) # Output: (7 + 3j)
Random Numbers (random
Module)
Python’s random
module offers a variety of functions for generating random numbers and performing random operations.
random()
: Returns a random floating-point number between 0.0
and 1.0
(excluding 1.0
).
randint(a, b)
: Returns a random integer between a
and b
(inclusive).
choice(sequence)
: Returns a random element from a non-empty sequence like a list, tuple, or string.
For example:
import random
# Returns a random float between 0.0 and 1.0 (exlusive)
x = random.random()
print(x) # Output: 0.7401202624893238
# Returns a random integer between 1 and 3 (inclusive)
y = random.randint(1, 3)
print(y) # Output: 3
# Returns a random element from the list
z = random.choice([5, 10, 15, 20])
print(z) # Output: 15
Python’s math
Module
Python’s math
module offers a comprehensive set of mathematical functions essential for performing a wide variety of numerical computations. We will explore some of the most commonly used ones.
math.sqrt(x)
: Returns the square root of x
(as a float).
math.pow(x, y)
: Returns x
raised to the power of y
(same as the **
operator, but always returns a float).
math.ceil(x)
: Rounds x
up to the nearest integer.
math.floor(x)
: Rounds x
down to the nearest integer.
For example:
import math
print(math.sqrt(9)) # Output; 3.0
print(math.pow(2, 3)) # Output: 8.0
print(math.ceil(5.1)) # Output: 6
print(math.floor(5.9)) # Output: 5