Numbers are essential in Python for performing calculations, and they are classified into three basic data types:
This guide will take you through each of these data types in detail. Let’s dive in!
Definition: Integers are whole numbers without decimal values. They can be positive, negative, or zero. In Python, integers have unlimited size, constrained only by the system’s memory.
Special Number Systems: Python supports binary (base 2), octal (base 8), and hexadecimal (base 16) representations for integers. Prefixes for these systems are:
0b
or 0B
0o
or 0O
0x
or 0X
x = 1
y = 882399773218279
z = -125634
a = 0b1100
print(type(x)) # <class 'int'>
print(type(y)) # <class 'int'>
print(type(z)) # <class 'int'>
print(a) # 12
print(type(a)) # <class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
12
<class 'int'>
Definition: Floating-point numbers are real numbers containing a decimal point, separating the integer and fractional parts. They are accurate up to 15 decimal places.
Scientific Notation: Python supports scientific notation using E
or e
.
23e2
is equivalent to 23 × 10^2
.x = 12.3
y = 12.9829379485794548679
z = -18.96
print(type(x)) # <class 'float'>
print(type(y)) # <class 'float'>
print(type(z)) # <class 'float'>
print(x) # 12.3
print(y) # 12.982937948579455
print(z) # -18.96
<class 'float'>
<class 'float'>
<class 'float'>
12.3
12.982937948579455
-18.96
Definition: Complex numbers are written in the form a + bj
, where a
is the real part and b
is the imaginary part. Note that Python uses j
for the imaginary unit instead of i
.
x = -5j
y = 2 + 4j
z = 22j
print(type(x)) # <class 'complex'>
print(type(y)) # <class 'complex'>
print(type(z)) # <class 'complex'>
<class 'complex'>
<class 'complex'>
<class 'complex'>
Python provides inbuilt functions to convert between number data types:
int()
– Converts any data type to an integer.float()
– Converts any data type to a float.complex(real, imaginary)
– Converts to a complex number.b = 13.16
c = 2 + 17j
a = 5
# Converting float to int
print(int(b))
# Converting int to float
print(float(a))
# Converting int to complex
print(complex(a))
# Converting float to complex
print(complex(b))
# Converting to complex with real and imaginary parts
print(complex(a, b))
13
5.0
(5+0j)
(13.16+0j)
(5+13.16j)
Python provides many inbuilt functions for mathematical calculations. Some popular functions include:
abs()
– Returns the absolute value.pow()
– Computes power.round()
– Rounds off a number to a given precision.math.sqrt()
– Computes square root (requires importing math
).import math
x = 25
# Absolute value
print(abs(-10))
# Power
print(pow(2, 3))
# Rounding
print(round(12.3456, 2))
# Square root
print(math.sqrt(x))
10
8
12.35
5.0
Numbers in Python are versatile and offer a wide range of functionalities. From basic arithmetic to advanced type conversion and mathematical functions, Python’s number system is designed to handle diverse computational needs efficiently.
Use this guide as a quick reference to master numbers in Python, and enhance your programs with precision and creativity!
Click here to know more our program!