# Python Numbers | Type Conversion, Decimal, Random and Mathematics

Here, we have discussed about python numbers, type of python numbers, Type Conversion and Mathematics. In python, number data type are used to store..

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

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.

Example:

x = 3

y = 1409021202575130

z = -6769927

print(type(x))

print(type(y))

print(type(z))

Output

<class 'int'>

<class 'int'>

<class 'int'>

### Float

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.

Example:

x = 4.15

y = 1.7

z = -20.69

print(type(x))

print(type(y))

print(type(z))

Output

<class 'float'>

<class 'float'>

<class 'float'>

### Complex

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.

Example:

x = 9+5j

y = 5j

z = -5j

print(type(x))

print(type(y))

print(type(z))

Output

<class 'complex'>

<class 'complex'>

<class 'complex'>

## Type Conversion

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 + y

print(z)

Output

9.6

### 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

print(float(a))

b = 4.6

print(int(b))

c = '2'

print(type(int(c)))

d = '4.6'

print(type(float(c)))

e = 7

print(complex(e))

f = 5.5

print(complex(f))

Output

6.0

4

<class 'int'>

<class 'float'>

(7+0j)

(5.5+0j)

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-b

print(c)

Output

0.19999999999999996

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.

import decimal

a = decimal.Decimal('1.2')

b = decimal.Decimal('1.0')

c = a-b

print(c)

Output

0.2

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:

import random
print(random.random())

Output

0.8028778466361499

## Python Mathematics

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:

import math

x = 8.5

# returning the ceil of 8.5

print ("The ceil of 8.5 is : ", end="")

print(math.ceil(x))

Output

The ceil of 8.5 is : 9

## Conclusion

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.