In this article we will learn about Python numeric data types, what are different numeric data types available in Python and how to use those numeric data types. So let’s get started.

Table of Contents
Introduction to Python Numeric Data Types
In Python programming language three data types handle numbers. These three numeric data types are
- int
- float
- complex
The variables for numeric data types can be created based on assigned values or they can be even set to specific numeric data types.
Here is the source code to demonstrate Python numeric data types
var1 = 150 # int value var2 = 200.5 # float value var3 = 5j # complex value print(type(var1)) print(type(var2)) print(type(var3))
Below is the output of the above code
<class 'int'>
<class 'float'>
<class 'complex'>
As you can see from the above output numeric data types are created based on the value assigned to the variables.
Understanding Python int – Python Numeric Data Types
Python int or integer is a whole number that can be a positive or negative number e.g. 25, 340, -120, 9287262, etc. The int numeric data type can be of unlimited length and cannot contain decimals
Here is the source code to demonstrate the use of Python int – Numeric Data Type
var1 = 15 var2 = 399872635352423674 var3 = -83725524 var4 = 1.0 print(type(var1)) print(type(var2)) print(type(var3)) print(type(var4))
Below is the output of the above code
<class 'int'>
<class 'int'>
<class 'int'>
<class 'float'>
As you can see from the above output that the variables var1, var2 & var3 are of type int. The variable var4 is not int but it is float as we have assigned a value with decimal in it.
You can also explicitly set the data type of the variable to int when you are assigning value to a variable using the int() function as shown in the code below
var1 = int(200)
Understanding Python float – Python Numeric Data Types
Python float or the floating point number is a number that contains decimals e.g. 1.5, 23656.325, -526.58, etc. It can be a positive or negative number with decimals. The use of decimals makes them different from int. Python float can be accurate up to 15 decimal places.
Python float can also hold scientific numbers with an e followed by a positive or negative integer that indicates the power of 10 e.g. 6e3, 5.2e-4, etc.
Here is the source code to demonstrate the use of Python float – Numeric Data Type
var1 = 4.52 var2 = 256.689 var3 = -569452.5198 var4 = 5e4 print(type(var1)) print(type(var2)) print(type(var3)) print(type(var4))
Below is the output of the above code
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
As you can see from the above output all the variables are of type float as per the value assigned to the variables.
You can also explicitly set the data type of the variable to float when you are assigning value to a variable using the float() function as shown in the code below
var1 = float(148)
Understanding Python Complex – Python Numeric Data Types
Python complex numbers are represented in two parts i.e. a real part and an imaginary component like 8+6j (+j).
Here is the source code to demonstrate the use of Python complex – Numeric Data Type
var1 = 6+8j var2 = 14+23j var3 = 5j var4 = -5j print(type(var1)) print(type(var2)) print(type(var3)) print(type(var4))
Below is the output of the above code
<class 'complex'>
<class 'complex'>
<class 'complex'>
<class 'complex'>
As you can see from the above output all the variables are of type complex as per the value assigned to the variables.
You can also explicitly set the data type of the variable to complex when you are assigning value to a variable using the complex() function as shown in the code below
var1 = complex(2+5j)
How to Perform Numeric Data Type Conversions in Python
In Python programming also you can convert numeric data values from one type to another. There are functions available for conversion int(), float() & complex().
Here is the source code to demonstrate how to perform numeric data type conversions in Python.
var1 = 125 # int var2 = 34.7 # float var3 = 4+6j # complex var4 = float(var1) #conversion from int to float var5 = int(var2) #conversion from float to int var6 = complex(var1) #convert from int to complex var7 = complex(var2) #convert from float to complex print(var4) print(var5) print(var6) print(var7) print(type(var4)) print(type(var5)) print(type(var6)) print(type(var7))
Below is the output of the above code
125.0
34
(125+0j)
(34.7+0j)
<class 'float'>
<class 'int'>
<class 'complex'>
<class 'complex'>
As you can see from the above output the int has converted to float, the float has converted to int and both int & float have been converted to complex.
In Python programming you cannot convert complex numbers into int or float as that is not possible. If you try to do that conversion then it will throw an error
Be careful while performing types conversions as you may lose actual values which can cause an issue e.g. you can see from the above output that while converting float to int the value changed from 34.7 to 34
How to Generate Random Numbers in Python
Random numbers may be at times required for program logic in applications like games, cryptography, etc. Python does not have a direct function for random number generation instead a built-in module is available in Python for random number generation.
This built-in Python module named random can be used to generate random numbers in Python.
Here is the source code to demonstrate how to generate random numbers in Python
import random print(random.randrange(100, 10000))
Note that every time you run the above code it will return a different number as it will return a random number between the specified range i.e. 100 to 10000
Summary
Python numeric data types store numeric values in them. Number objects are created when you assign a value to a numeric data type variable.
In this article, we learned about Python Numeric Data Types i.e. how to use int, float and complex Data Types. We also explored how to perform various operations on Numeric Data Types in Python programming language.
You can also check my series on Python for Beginners – Series: Python Tutorial – Learn Python Programming for Beginners
References – Python Documentation
Please provide your suggestions & feedback in the comments section below
Hope you found this article useful. Please support the Author
