In this tutorial, we will discuss Number one of the data type in python. We will also see how can we convert other data types to Number and also perform some operators on Numbers.

Table of Content

  • Number Datatypes
  • Type conversion
  • Python decimals
  • Python Fractions
  • Python Mathematics

Number Data Type in Python

Every digit (0-9) in python with or without decimal fall under Number data type. Python has different types of numbers such as integers (int), float (float) and complex.

Integers and floats in python mainly serve the same purpose the only difference is integers are those which do not have decimals while floats are decimal numbers.

For example:

If we write 60 it is an integer but 60.0 will become afloat though both are same but belong to different numbers type.

On the other hand, the complex is a special kind of numbers though we do not use complex often like float and integer, they come very handily when we discuss the real and imaginary concept of mathematics.

We write complex numbers in the form of x + y j where x and y could be an integer or float numbers and j is used to represent iota. Here x represents the real number and y represent the imaginary number.

Let’s understand it with an example.

i = 55
f = 55.0
c = 55 + 5j



<class 'int'>
<class 'float'>
<class 'complex'>

Binary, Octal, and Hexadecimal:

In python, we can convert a binary or hexadecimal or Octal to integers by using the prefix 0b for binary, 0o for Octal and 0x for Hexadecimal.

Let’s understand with an example:




Type Conversion:

In python, we can covert the data type of one variable to another by using some special functions. This conversion is also known as coercion. The type conversion is divided into two parts.

  • Implicit type conversion
  • Explicit type conversion

In Implicit type conversion, the python interpreter itself convert the type of variable without the user consent.

For example

>>> 9+9.0

In Explicit type conversion, user or developer can explicitly change the data type of the variable by using the functions int(), float() and complex(). Even using these function we can convert string to number only if that string contains only digits.

Let’s convert some data types Explicitly

>>> int(7.0)
7              #output

>>> float(7)
7.0          #output

>>> complex(2,3)
(2+3j)    #output

>>> complex("3+2j")

Python Decimal Module:

In Python when it comes to the decimal numbers the first thing strike in our mind is float because by default float deal with the number having decimal values. Though when we perform some operation on float numbers, it not reliable every time there are many sorts coming seen in float such as conventionally or by default float store 15 places after the decimal it does not matter if you had written those 15 numbers or not float assume that they are there.

for example, let’s perform an arithmetic operation on floats number and see the output.

>>> 1.1+2.2

In the above example, we add 1.1 with 2.2 the answer is supposed to 3.3 but we are getting a huge number of decimal numbers.

Python comes with a decimal module which is used to alter the floating numbers and by using this module we can use the float.

Let’s understand it with an Example

from decimal import Decimal




Decimal or Float?

Though Float and Decimal use to represent the same values, sometimes there are some conflicts in values so to be more precise use decimal instead of float.

Python Fraction module

Python has a module which is known as fractions which is used to represent the number in fraction form. When we use the fraction module, we can represent the number in the form of numerator and denominator.

Let’s understand it with an example

from fractions import Fraction

f1 = fractions.Fraction(3,2)           #where 3 is numerator and 2 is denominator
f2 = fractions.Fraction(7,5)




Python Math Module

Python Math module provides many functions and concepts of math that comes very handy when we write a programme with probability and statistics.

Let’s understand it with an example:

import math



Leave a Reply

Your email address will not be published. Required fields are marked *