This is another interesting article on Python Programming Language.
Here, we will learn about python numbers, type of python numbers, Type Conversion and Mathematics.
What are Python Numbers?
In python, number data type are used to store numeric values. The number data type are immutable data types which means of we change a value slightly then it will result into a whole new allocated object.
Types of Python Numbers
In python, there are three types of number data type which are explained as follows;
Int also known as integer is s whole number which can be zero, positive and negative but it doesn't include decimal or fraction parts. In python, there is no limit in the length of integers value. The programmers use int() function to convert a string or float into integer.
x = 3
y = 1409021202575130
z = -6769927
<class 'int'><class 'int'>
Float numbers are real numbers with floating point representation. In simple words, we can say that floating numbers are those real numbers with decimal place. We can create float numbers by direct entering decimal point or by using division operation. We can also represent a floating point literal by using notation e or E which is the short for exponential notation.
x = 4.15
y = 1.7
z = -20.69
<class 'float'><class 'float'>
Those number in which both real an imaginary parts are used, are Known as complex numbers such as 9 + 5j is a complex number here, 9 is the real component and 5 multiplied by j is the imagery part.
x = 9+5j
y = 5j
z = -5j
<class 'complex'><class 'complex'>
We can convert the form one number into another form which is known as type conversion or also known as coercion. There are two ways with which we can perform coercion;
Using Arithmetic Operations
We can convert the type of number if one of them is already float by using arithmetic operations like addition, multiplication, substraction, etc. But this method does not work with complex numbers.
Example of Type Conversion using Arithmetic Operations:
x = 4.6
y = 5
z = x + yprint(z)
Using Built-in Functions
We can convert the types of number by using the in-built functions like float(), int() and complex() explicitly.
Example of Type Conversion using built-in functions:
a = 6
b = 4.6
c = '2'
d = '4.6'
e = 7
f = 5.5print(complex(f))
Complex numbers can't be converted into int and float data types. And complex built-in function are not applicable on strings.
Decimal Numbers in Python
In python there is decimal module which is used to carry out the decimal calculations. Let's first understand with the example:
a = 1.2
b = 1.0
c = a-bprint(c)
Here, we can see that the output is coming unexpected which is not wrong because only the binary numbers are understandable by the computer system and sometimes it becomes difficult to store decimal fractions correctly.
a = decimal.Decimal('1.2')
b = decimal.Decimal('1.0')
c = a-bprint(c)
Decimal module is useful when we need precise decimal representation and for defining the required accuracy.
Random Numbers in Python
In python, we also get random module with which we can generate pseudo-random numbers. It is useful in selecting random elements from a sequence and we can also create random numbers with the random module.
Example of random numbers in Python:
Python also comes with a math module which is useful in carrying out various mathematical operations like probability, trigonometry, logarithms, statistics, etc.
Example of Python Mathematics:
x = 8.5
# returning the ceil of 8.5
print ("The ceil of 8.5 is : ", end="")print(math.ceil(x))
The ceil of 8.5 is : 9
Above we have discussed about python numbers, type of python numbers, Type Conversion and Mathematics. In python, number data type are used to store numeric values. There are three types of number data type i.e., int, float and complex. Python also comes with a math module which is useful in carrying out various mathematical operations.