In Python, the number data type is used to store numeric values. Numbers in Python are an immutable data type. Being an immutable data type means that if we change the value of an already allocated number data type, then that would result in a newly allocated object. In this module, we will delve deeper into the number data type. Following is the list of the topics that will be covered in this module:
Without any further ado, let’s get started.
The number data type is further categorized based on the type of numeric value that can be stored in it. If a variable in Python contains a numeric value, the data type of that variable will be under one of the categories of the number data type based on the type of value assigned to that variable.
The number data type is divided into the following five data types:
We will now understand each of these categories of the number data type, separately.
Python integers are nothing but whole numbers, whose range dependents on the hardware on which Python is run. Integers can be of different types such as positive, negative, zero, and long.
I = 123 #Positive Integer
J = -20 #Negative Integer
K = 0 #Zero Integer
L suffix is used for the representation of long integers in Python. Long integers are used to store large numbers without losing precision.
I = 99999999999L
In Python, we also have another number data type called octal and hexadecimal numbers.
To represent the octal number which has base 8 in Python, add a preceding 0 (zero) so that the Python interpreter can recognize that we want the value to be in base 8 and not in base 10.
I = 11
#Then in Octal we will write –
I = 011
To represent the hexadecimal number (base 16) in Python, add a preceding 0x so that the interpreter can recognize that we want the value to be in base 16 and not in base 10.
I = 0x11
Floating-point numbers symbolize the real numbers that are written with a decimal point dividing the integer and fractional parts.
Floating-point numbers may also come with scientific notation with E or e, indicating the power of 10.
5.6e2 that means 5.6 * 102.
I = 2.5
J = 7.5e4
Complex numbers are of the form, ‘a + bj’, where a is real part floating value and b is the imaginary part floating value, and j represents the square root of −1.
2.5 + 2j
Python offers a few built-in functions that let us convert numbers explicitly from one type to another. This process is called coercion. The conversion of one type of number to another becomes essential when performing certain operations that require parameters of the same type. For example, programmers may need to perform mathematical operations like addition and subtraction between values of different number types such as integer and float.
We can use the following built-in functions to convert one number type into another:
a = 3.5
b = 2
c = -3.5
a = int(a)
b = float(b)
c = int(c)
When converting a float data type into an integer data type, the value gets converted into an integer value closest to zero.
This brings us to the end of this module in Python Tutorial. The next module highlights the string data type in Python. Let’s meet there!
Download Interview Questions asked by top MNCs in 2019?