You might be thinking why I am writing about numeric types as there is not much to be explained in detail. Most of us are familiar with basic arithmetic operations such as addition, subtraction, multiplication, and division. But actually, there is more Python offers that we need to understand. So, the goal of this article is to present you with interesting features of Python Int data type.
Any number that can be written without any fractional part is considered as an integer number. For example, 0, 1 , 100, -1001, 4578 etc. are integers.
We all know about addition (+), subtraction (-), multiplication (*) of integers gives the result in an integer.
But let us look into other features of the int data type.
1) Division (/)
In Python, integer division gives the result in float always. Let’s look at the examples below under 4 different scenarios.
2) Floor division (//)
The floor division operation of integers gives a floored quotient (i.e. the quotient is calculated first and then floor operation is applied). Floor operation in Python returns the closest integer value equal to or less than the given number.
Example 1: What is the value 5 // 2?
The answer is 2. Here is the explanation: The quotient is calculated first which is 2.5 and then floor operation is applied on 2.5. The floor value of 2.5 is 2 because 2 is the closest integer value that is equal to or less than 2.5.
Example 2: What is the value of -5 // 2?
The answer is -3. Here is the explanation: The quotient is calculated first which is -2.5 and then floor operation is applied on -2.5. The floor value of -2.5 is -3 because -3 is the closest integer value that is equal to or less than -2.5. If you are confused about how we are getting -3 as the result, look at the number line below and it will be clear to you. As you can see from the number line -2 can’t be the result because it will be greater than -2.5. Hence, an integer equal to or less than -2.5 is -3.
3) Modulo (%)
The floor division and modulo operators are defined in such a way that they always satisfy the below condition for any given two integers a & b.
a = b * (a // b) + (a % b)
Note that when we are dealing with only positive integers then modulo operation gives a reminder. But it is not the case when negative integers are involved. So be careful if you are using the modulo operator on negative integers as the results are not intuitive. Look at the four cases in the below code and you will understand. If you are in doubt make sure to calculate a % b using the modified version of the above equation -
a % b = a - (b * (a // b))
4) Maximum value of int
In Python 3, int is an arbitrary-precision data type. It means that integers in Python can be a very large number as you need (unbounded). That’s why the name arbitrary precision. But it limited by memory. As you can see you can create a very large number.
5) Memory usage
Int data type in Python works very differently compared to other languages such as C, C++, Java, etc. If you take an example of C language, there are multiple ways integer is declared such as int, short, long, unsigned int, unsigned short, etc.
But in Python, there is only one int type and it takes a variable number of bits. As the integer object gets bigger and bigger, the bit required to store the number will also increase and also the size of the integer object.
So, how to check the memory allocated to int or any other objects? There are two ways you can get the size:
getsizeof()method from the sys module
As you can see
b requires 28 bytes and 32 bytes respectively to store 100 and 1111111111.
a = 100
>> 28b = 1111111111
__getsize__()on the object
As you can see from the below code, __getsize__ magic method also returns the same bytes.
a = 100
>> 28b = 1111111111
You can also use
bit_length() method on the integer object which returns the number of bits required to represent an integer in binary.
a = 101
Note: There is a difference between
__sizeof__()internally and adds additional garbage collector overhead. So,
getsizeof()may return higher value than
__sizeof__(). So, if you check the results on different types such as list, tuple you may get different results.
6) Int constructor
‘Everything is an object in Python’. It means that all integers in Python are objects of the class ‘int’. The same is applicable for floats, strings, lists, tuple, etc. Classes can have constructors and
int class also has constructors. So, when we call
int('5') returns an instance of an
int class (returns int object). There are two ways we can use int constructors as explained below
- passing number as argument
In this method, a number is passed to int class and it returns int object.
- passing number (as string) & base as an argument
In this method, a number is passed as a string as the first parameter. There is an optional second parameter called
base which defaults to base 10. So for
int('25', base=10) returns 25. Similarly
int('1110', base=2) is the binary number and it returns 14 in base 10. Also,
int(765, base=8) is 501 in base 10 and
int('F123', base=16) is 61731 in base 10.
>> 25int('25', base=10)
>> 25int('1110', base=2)
>> 14int('765', base=8)
>> 501int('F123', base=16)
In this article, you learned 6 interesting facts about Python int data type. Hope you enjoyed reading the article. Stay tuned for more exciting articles on Python in future articles.
Originally published at pythonsimplified on 1-Jan-2021.