Python Numeric Data Types | Detail Guide with Examples
There are four main Python Numeric Data Types:
You might be thinking why do I mention 5 data types in the above list. It has a reason and I will clear your doubt in this article.
I keep talking Python is far different from all other programming languages.
If you are taught for any of the programming languages, you might be aware of these data types. But there are some interesting facts about Python which is different from other programming languages.
In C programming, we explicitly mention the data type of the variable.
int count = 10; float perc = 80.45;
In Python programming, we don’t specify the data type of the variable explicitly.
count = 17; perc = 77.05;
Dynamic data types in Python:
The data type of the variable is identified at the run-time based on the type of value you are assigning to the variable.
Anytime in the program, you can change the data type of any variable by passing different value.
This is called dynamic data types in Python.
Now, let’s see in detail about numeric data types in Python.
Implementation of int in Python is similar to the long in C.
It has a precision limit of 32 bits. So one can set any integer value up to 2^32-1.
num = 20 type(num) #<type 'int'>
Here are some examples you can try on integer data type:
- Write a program to swap two int value without using any temp variable. (It is just one line of code in Python.)
How to find the maximum and minimum value accepted by the integer?
Finding max value of integer
import sys sys.maxint #2147483647
Finding min value of integer
You can calculate its minimum value by a simple code of line.
|import sys -sys.maxint - 1 #-2147483648
So then what will happen if we provide the value to the int variable out of its range?
As we know we don’t specify the data type of value while using a numeric variable in python. When you assign the value out of int size limit, the variable is converted to long int.
There is no precision limit to the long integer.
Yes, it’s right!
So you can enter any number without limit. When you assign value to the value greater than int max limit (i.e. 2147483647), it is treated at long int.
#World Total Population population =7514815137 type(population) #<type,'long'>
In fact, this limit depends on the size of memory (RAM). The size of the value in a variable cannot be greater than the size of actual memory space you are using for running python program.
But how can you test it out the limit range of long int?
To test this, I can’t suggest you to keep typing the integers. It may hurt your finger typing 😛
The smart way is to generate this number as an output of the program.
One of the clever examples is to find the factorial of 6000.
There are so many Python interviews; the interviewer asks to find out the factorial of a big number.
If you are coming from other programming languages, you might consider it as a big challenge. In many of the programming languages, finding the factorial of big number crashes your program if the factorial of the number goes out of the integer range limit.
But this is not the case for Python. You can find the factorial of any number as long as your memory is not exhausted. It is near to impossible to exhaust the memory limit unless you are using 128KB RAM 😀
Implementation of float in Python is similar to the double in C and C++.
Unlike to the long data type, float has max and min value limit.
How to get Max and Min possible value of the float?
You can use sys.float_info to get max and min possible value for float numeric type in Python.
import sys sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
A complex number is a bundle of the real and imaginary number. Here real and imaginary values are floating type numeric values.
You can pass both integers as well as float value to the real and imaginary part of the complex number.
comp = 10+7j type(comp) #<type, 'complex'> comp = 12.44+7.56j type(comp) #<type, 'complex'>
You will not find the use of this variable in general arithmetic operation. It is mostly used in programming for data science and for other scientific purposes.
Above four are the main data type of the numeric variable. There is also one numeric type called boolean.
It accepts the value 0 and 1. If you pass the value 0 or 1, the system can not identify if the variable is an integer or boolean type. It is simply treated as an int, so I will not consider it as the main data type.
int var = 1 type(var) #<type, 'int'>
It is nothing but the subtype of an integer.
Now let’s see, some of the frequently asked questions on Numeric Data types…
How does system identify the Python Numeric Data Types of the Variable?
It identifies based on the value and range of the value, you are providing to the variable.
- Using value passing to the variable
If you are providing integer value without decimal point it is considered as an int value. All the numerical values with a decimal point is a float in Python.
- Using a range of the value assigned to a variable
Integer and float value has a limit for the range of values assigned to it.
When the range of integer value goes beyond the limit the variable is converted into a long data type. This we will see in detail in long data type.
How to know the Datatype of a Numeric Variable?
You can simply get the type of a variable by using inbuilt type function as below.
count = 17; type(count) #<type 'int'> perc = 17.45; type(perc) #<type 'float'>
In your program, you may want to know the data type of the variable. And based on the type of variable, you may want to manipulate and perform the operation. You can refer Declaring and checking type of variable.
How to take the value for Python Numeric Data Types as an input from the user?
In above all the example, we have assigned a value to the variable. We can also ask the user to enter the numeric value and then store in a variable.
Steps followed for reading user input:
- Taking user input. The input taken from the user is always considered as a string.
- For taking the numeric value as a user input, we have to convert it into the numeric value.
Here is a complete tutorial about reading user input and saving it as a numeric value.
This simple guide to address all the basic questions regarding Python numeric data types. If you have anything to ask, write in a comment. I will reply you right back.