Python Number Types with Examples

What is Python Numbers?

The Python Numbers data types store numeric values. Because they are immutable, changing the value of a number data type results in a freshly allocated object. Python Number objects are produced when a value is assigned to them.

Python supports three types of numbers: complex numbers, integers and floating point numbers (decimals).

Every numbers in Python is an object of the first class. It implies that each numeric data type is implemented via a class definition. Using the type() method, we may determine the data type of a number or the class to which a number belongs.

For instance, Python programming defines entire numbers as integers. The number’s data type can be confirmed as follows.

num1 = 1234
num2 = 12.34


<class 'int'>
<class 'float'>
Python Program to Swap Two Numbers
Python Program to Swap Two Numbers

The first number is an integer, thus the returned data type is class 'int'. The second number is a decimal, therefore the returned data type is class 'float'.

Now, we will examine each native Python number data types individually.

Number Data Types in Python

There are three number data types in Python:

  1. Integer
  2. Float
  3. Complex

1. Integer Data Type in Python

Python’s Integer Data Types represent whole numbers like 0 and negative integers like -1. Defining an integer variable is as simple as assigning it a number.

var1 = 751
var2 = -8712
print (var1)
print ("The var1 data type is {}".format(type(var1)))
print (var2)
print ("The var2 data type is {}".format(type(var2)))


The var1 data type is <class 'int'>
The var2 data type is <class 'int'>

Integer data types in Python do not have a maximum value, unlike other computer languages. You can define a number of any size you choose, but the size is limited by the capabilities of your computer. For example, here we have built a number that contains 90 digits.

# 90 digit integer
var1 = 963021548778451203698745632103698521478521548915820542014778621036985102587695184747474714
print ("Here is the 99 digit integer with {} type".format(type(var1)))
print (var1)


Here is the 99 digit integer with <class 'int'> type

Additionally, Python offers integer data types for various number systems, such as binary, octal, and hexadecimal.

We can represent binary numbers in Python by prefixing the number with 0b, as in 0b1110, hexadecimal numbers by prefixing the number with 0x, as in 0x1117, and octal numbers by prefixing the number with 0o, as in 0o1117.

var1 = 1117
var2 = 0x1117
var3 = 0o1117
var4 = 0b1110

print("Data Type of 1117 is {}".format(type(var1)))
print("Data Type of 0x1117 is {}".format(type(var2)))
print("Data Type of 0o1117 is {}".format(type(var3)))
print("Data Type of 0b1110 is {}".format(type(var4)))


Data Type of 1117 is <class 'int'>
Data Type of 0x1117 is <class 'int'>
Data Type of 0o1117 is <class 'int'>
Data Type of 0b1110 is <class 'int'>

You may note that the data type of every number, regardless of the number system, is int. Each binary, octal, and hexadecimal number is an instance of the int class.

2. Float Data Type in Python

Float Data Types in Python is also known as floating-point number. Python employs double-precision floating-point numbers to represent decimal or real numbers. A number with floating-point precision may be positive, negative, or zero. Floating-point numbers contain one or more digits after the decimal point, much as decimal numbers.

var1 = 123.14
print (var1)
print ("The var1 data type is {}".format(type(var1)))


The var1 data type is <class 'float'>

Floats also representing real numbers, are represented with a decimal point separating the integer and fractional components. Floats may alternatively be expressed in scientific notation, where E or e represents the power of 10 (2.5e2 = 2.5 x 102 = 250).

var1 = 35e3
var2 = 12E4
var3 = -87.7e100



<class 'float'>
<class 'float'>
<class 'float'>

3. Complex Numbers in Python

The complex numbers data type is also supported by Python. In Python, a complex number is represented as a + i b, where a and b are numeric values representing the real and imaginary parts of the complex number, respectively. Any English letter may be substituted for the letter i.

var1 = 6+5j
var2 = 9j
var3 = -7j



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

As seen in the above example, the returned data type is “complex“.

Using the complex() function is another method for defining complex numbers. The complex() function receives the real and imaginary components of the needed complex number and returns a complex number. The actual component will be passed as the first argument, while the imaginary part will be passed as the second parameter. Additionally, the real part is required while the imaginary part is optional.

var1= complex(3,2)


(3+2j) <class 'complex'>

The "real" and "imag" characteristics of a complex number provide access to the real and imaginary components, respectively.

var1= complex(3,2)
realnum = var1.real
imagnum = var1.imag
print("Real number is ", realnum)
print("Imaginary number is ", imagnum)


Real number is  3.0
Imaginary number is  2.0

The conjugate() method can also be used to locate the conjugate of a complex number. The conjugate() method returns the conjugate complex of a complex integer in the following format:

var1= complex(3,2)
conjugateNum= var1.conjugate()
print("Conjugate is", conjugateNum)


Conjugate is (3-2j)

Fraction Numbers in Python

Using the fraction numbers module, fractions have been explicitly implemented in Python. Import the fractions module using the import statement below to use the fraction data type in Python.

import  fractions

The Fraction() function of the fractions module can be used to define a fraction object using integers in Python. Two integers are required as input. The first input is regarded as the numerator, while the second input is regarded as the denominator. After execution, the Fraction() function returns a numerator/denominator fraction object.

import fractions

var1 = fractions.Fraction(1,2)
print("var1 answer is",var1)


var1 answer is 1/2
<class 'fractions.Fraction'>

Using the Fraction() method, you may also convert a floating-point number to a fraction.

import fractions

var1 = fractions.Fraction(0.5)
print("var1 answer is",var1)


var1 answer is 1/2
<class 'fractions.Fraction'>

Mathematical Functions

The following list below is the Mathematical Built-in Functions that perform Mathematical calculations in Python.

No.Function & Returns ( description )
The absolute value of x: the (positive) distance between x and zero.
The ceiling of x: the smallest integer not less than x
3cmp(x, y)
-1 if x: < y, 0 if x == y, or 1 if x > y
The exponential of x:ex
The absolute value of x.
The floor of x: the largest integer not greater than x
The natural logarithm of x, for x > 0
The base-10 logarithm of x for x > 0.
9max(x1, x2,...)
The largest of its arguments: the value closest to positive infinity
10min(x1, x2,...)
The smallest of its arguments: the value closest to negative infinity
The fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float.
12pow(x, y)
The value of x**y.
13round(x [,n])
 rounded to n digits from the decimal point. Python rounds away from zero as a tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0.
The square root of x for x > 0
Mathematical Functions

If you require additional information, please refer to the Python Mathematical Functions Documentation.

Trigonometric Functions

The following list below is the Trigonometric Functions that perform Trigonometric calculations in Python.

No.Function and Description
Return the arc cosine of x, in radians.
Return the arc sine of x, in radians.
Return the arc tangent of x, in radians.
4atan2(y, x)
Return atan(y / x), in radians.
Return the cosine of x radians.
6hypot(x, y)
Return the Euclidean norm, sqrt(x * x + y * y).
Return the sine of x radians.
Return the tangent of x radians.
Converts angle x from radians to degrees.
Converts angle x from degrees to radians.
Python Numbers – Trigonometric Functions

Check the Python Documentation for additional details about Trigonometric Functions.

Mathematical Constants

The list below is the two Mathematical constants in Python.

NoConstant and Description
The mathematical constant pi.
The mathematical constant e.
Python Numbers – Mathematical Constants

Random Numbers Functions in Python

The list below is the most commonly used Random Numbers Functions in Python. This is used in simulations, games, security, testing, and privacy applications.

NoFunction and Description
A random item from a list, tuple, or string.
2randrange ([start,] stop [,step])
A randomly selected element from range(start, stop, step)
A random float r, such that 0 is less than or equal to r and r is less than 1
Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.
Randomizes the items in a list without moving them. Returns None.
6uniform(x, y)
A random float r, such that x is less than or equal to r and r is less than y
Random Numbers Functions

Python Documentation has additional information on Random Numbers.


In summary, you learned about Type conversion of Python Numbers, Mathematical Functions as well as Trigonometric Functions, and Random Numbers functions and you can do a few exercises to put what you learned into practice using our Python Compiler. I hope that this tutorial helped you understand how to use Numbers in Python.

You are one step closer to creating stronger, more effective codes now that you are aware of what Python Numbers are. In your code, start implementing all the functions you have studied today.

In the next post, “Python Strings,” you’ll learn about the different functions of Python strings and their uses.

Leave a Comment