Python Number Data Types

Python Numbers data type is used to store numeric values. The followings are Number data types in Python :-

Datatype Class of Data Type
Integer <class 'int'>
Floating point numbers <class 'float'>
Complex Number <class 'complex'>

Int Data Type

Int data type is used to store integers. It can be positive, negative or zero.

Let us understand this through an example :-

x = 12
y = 0
z = -9
print(type(x))
print(type(y))
print(type(z))
print(x)
print(y)
print(z)

The output of the following program will be -

<class 'int'>
<class 'int'>
<class 'int'>
12
0
-9

Float Data Type

A number having a decimal part is a floating-point number. It can be positive as well as negative. The floating-point number can be written in the form of a Fraction or in the form of an Exponent.

Let us understand fractional form through an example :-

a = 10.578
b = 2.0
c = -9.89
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))

The fractional form is when we use decimals after a number. The output of the following program :-

10.578
2.0
-9.89
<class 'float'>
<class 'float'>
<class 'float'>

Let’s take another example of floating point numbers, in form of exponent :-

x = 12e2
y = -2e5
print(type(x))
print(type((y))
print(x)
print(y)

Here, e refers to the power of 10 and we can also use E in python programming. The output of the following program will be :-

<class 'float'>
<class 'float'>
1200.0
-200000.0

Complex Data Type

Python Complex data type represents complex numbers in the form of X+Yj, where X is the real part of the number and Y is the imaginary part with the suffix j.

Let us understand this from an example -

x = 2+3j
y = 2j      #with zero real part
print(type(x))
print(type(y))

The output of the following program will be -

<class 'complex'>
<class 'complex'>

The real part and imaginary part of the complex number can be retrieved through -

  • python-variable.real
  • python-variable.imag

Let us understand this more clearly -

x = 1+3j
print(x.real)
print(x.imag)

Then the first line will return the real part and the second will return the imaginary part of the number. As we can see in the output of the following program below -

2.0
3.0
You may like to read: Type Conversion of numbers and library function for number data types