Numbers in Python | Integer, Float & Complex Number Data Types

Numbers in Python are very useful to perform calculations. There are 3 basic data types that belong to the Numbers category. They are:

1) Integers
2) Floating-point numbers
3) Complex numbers.

This article will take you through each of these data types in detail.

Numbers in Python

numbers in python

a) Integers

  • Integers are whole numbers without any decimal values. They can be either positive or negative.
  • In Python, integers are of unlimited size. An integer’s size is limited only by the memory available in the system.
  • Different number systems like Binary (base 2), Octal (base 8), Hexadecimal (base 16) are also considered to be of integer datatype. While working with numbers belonging to these number systems, you need to add these prefixes before the number: 

For Binary, ‘0b’ or ‘0B’ 
For Octal, ‘0o’ or ‘0O’
For Hexadecimal, ‘0x’ or ‘0X’

#declaring and assigning integers in python
x = 1
y = 882399773218279
z = -125634
a = 0b1100

<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>

b) Floating Point Numbers

  • Real numbers with a decimal point separating the integer and fractional parts are called floating-point numbers. They can be either positive or negative.
  • A floating-point number is accurate only up to 15 decimal places, i.e fractional part can contain only 15 digits.
  • Float values can also be scientific numbers with the notation ‘E’ or ‘e’ like this 23e2. 23e2 here is equivalent to 23 * 10^2.
#declaring and assigning float numbers in Python
x = 12.3
y = 12.9829379485794548679
z = -18.96

print (x)
print (y)
print (z)
<class 'float'>
<class 'float'>
<class 'float'>

c) Complex numbers

  • Complex numbers are represented in the form a + bj, where a is a real number and b is an imaginary number. Python will not accept complex numbers of the form a + jb.
#declaring and assigning complex numbers in Python
x = -5j
y = 2 + 4j
z = 22j

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

Type Conversion of Numbers in Python

As a programmer, you can convert a variable of any data type to Number data types in Python using the below Python inbuilt functions. Also, using these functions, you can convert a number from one Number data type to another i.e from int to float, float to decimal, decimal to int and so on.
  • int () : This function converts any data type to integer data type.
  • float () : This function converts any data type to float data type.
  • complex (real, imaginary) or complex (real) : This function converts any data type to complex data type.
Note: There is no ‘long’ integer in Python 3, int and long are unified now. This means int behave more like long. Sample program showing type conversion of Numbers in Python
b = 13.16
c = 2 + 17j

#converting float to int
print (int(b))

#converting int to float
print (float(a))

#converting int to complex
print (complex(a))

#converting float to complex
print (complex(b))

#converting to complex
print (complex(a, b))
(13.16 +0j)
(5 + 13.6j)

Inbuilt Functions Related to Numbers in Python

The below mentioned Python in-built functions will help make it easy to perform mathematical calculations. You can include these inbuilt functions in your program after importing the required module by using this syntax.

import modulename  #mandatory
x = 25
y = modulename.function ()  #syntax to call a function
print (y)

abs (x)Returns the absolute value of a function
ceil (x)Returns the ceiling value of x i.e the smallest integer not less than x
floor (x)Returns the floor value of x i.e the largest integer not greater than x
exp (x)Returns the value of ex i.e exponential of x
log10 (x)Returns the logarithm of x to base 10 i.e log10 x
min ()Returns the minimum among the given arguments
max ()Returns  the maximum among the given arguments
pow (x, y)Returns the value of x to the power of y i.e x^y
round (x, number of digits)Returns the rounded value of x
sqrt (x)Return the square root of x