Number data types store numeric values. They are immutable data types, which means that changing the value of a number data type results in a newly allocated object.
Different types of Number data types are :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Let’s see each one of them:
int (Integers) are the whole number, including negative numbers but not fractions. In Python, there is no limit to how long an integer value can be.
Example 1: Creating int and checking type
Example 2: Performing arithmetic Operations on int type
Addition: 11 Subtraction: 3 Division: 4 Multiplication: 15 Modulus: 0 Exponent: 36
This is a real number with floating-point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation. . Some examples of numbers that are represented as floats are 0.5 and -7.823457.
They can be created directly by entering a number with a decimal point, or by using operations such as division on integers. Extra zeros present at the number’s end are ignored automatically.
Example 1: Creating float and checking type
As we have seen, dividing any two integers produces a float.
A float is also produced by running an operation on two floats, or a float and an integer.
Example 2: Performing arithmetic Operations on float type
Addition: 8.7 Subtraction: 2.3 Division: 1.71875 Multiplication: 17.6
Note: The accuracy of a floating-point number is only up to 15 decimal places, the 16th place can be inaccurate.
A complex number is a number that consists of the real and imaginary parts. For example, 2 + 3j is a complex number where 2 is the real component, and 3 multiplied by j is an imaginary part.
Example 1: Creating Complex and checking type
Example 2: Performing arithmetic operations on complex type
Addition: (3+8j) Subtraction: (-1+8j) Division: (1.307692307692308+0.5384615384615384j) Multiplication: (-13+13j)
Type Conversion between numbers
We can convert one number into the other form by two methods:
- Using Arithmetic Operations: We can use operations like addition, subtraction to change the type of number implicitly(automatically), if one of the operands is float. This method is not working for complex numbers.
Example: Type conversion using arithmetic operations
- Using built-in functions: We can also use built-in functions like int(), float() and complex() to convert into different types explicitly.
Example: Type conversion using built-in functions
2.0 5 <class 'int'> <class 'float'> (5+0j) (6.5+0j)
When we convert float to int, the decimal part is truncated.
- We can’t convert a complex data type number into int data type and float data type numbers.
- We can’t apply complex built-in functions on strings.
Decimal Numbers in Python
Arithmetic operations on the floating number can give some unexpected results. Let’s consider a case where we want to add 1.1 to 2.2. You all must be wondering that the result of this operation should be 3.3 but let’s see the output given by Python.
You can the result is unexpected. Let’s consider another case where we will subtract 1.2 and 1.0. Again we will expect the result as 0.2, but let’s see the output given by Python.
You all must be thinking that something is wrong with Python, but it is not. This has little to do with Python, and much more to do with how the underlying platform handles floating-point numbers. It’s a normal case encountered when handling floating-point numbers internally in a system. It’s a problem caused when the internal representation of floating-point numbers, which uses a fixed number of binary digits to represent a decimal number. It is difficult to represent some decimal numbers in binary, so in many cases, it leads to small roundoff errors.
In this case, taking 1.2 as an example, the representation of 0.2 in binary is 0.00110011001100110011001100…… and so on. It is difficult to store this infinite decimal number internally. Normally a float object’s value is stored in binary floating-point with a fixed precision (typically 53 bits). So we represent 1.2 internally as,
Which is exactly equal to :
For such cases, Python’s decimal module comes to the rescue. As stated earlier the floating-point number precision is only up to 15 places but in the decimal number, the precision is user defined. It performs the operations on the floating-point numbers in the same manner as we learnt in the school. Let’s see the above two examples and try to solve them using the decimal number.
We can use decimal module for the cases –
- When we want to define the required accuracy on our own
- For financial applications that need precise decimal representations
Note: For more information about decimal numbers in Python and the functions provided by this module, refer to Decimal Functions in Python
Random Numbers in Python
Python provides a random module to generate pseudo-random numbers. This module can create random numbers, select a random element from a sequence in Python, etc.
Example 1: Creating random value
Example 2: Selecting random element from string or list
Note: For more information about random numbers, refer to our Random Number tutorial
The math module of Python helps to carry different mathematical operations trigonometry, statistics, probability, logarithms, etc.
The ceil of 3.5 is : 4 The floor of 3.5 is : 3 The value of 3.5**2 is : 12.25 The value of log2 of 3.5 is : 1.8073549220576042 The value of sqrt of 3.5 is : 1.8708286933869707 The value of sine of 3.5 is : -0.35078322768961984
Note: For more information about Python’s math module, refer to our math module tutorial.