Introduction
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 much 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.
Integer
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.
Division (/)
In Python, integer division gives the result in float always. Let’s look at the examples below under 4 different scenarios.
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.
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.
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 –
Max 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.
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:
Using getsizeof() method from the sys module
As you can see a
and b
requires 28 bytes and 32 bytes respectively to store 100 and 1111111111.
import sys
a = 100
print(sys.getsizeof(a))
>> 28
b = 1111111111
print(sys.getsizeof(b))
>> 32
Using __getsize__() on the object
As you can see from the below code, __getsize__ magic method also returns the same bytes.
a = 100
print(a.__sizeof__())
>> 28
b = 1111111111
print(a.__sizeof__())
>> 32
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
print(a.bit_length())
>> 7
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 a number
In this method, a number is passed to int class and it returns int object.
int(5)
>> 5
int(10.1)
>> 10
Passing number as a string
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')
and 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.
int('25')
>> 25
int('25', base=10)
>> 25
int('1110', base=2)
>> 14
int('765', base=8)
>> 501
int('F123', base=16)
>> 61731
Conclusion
In this article, you learned 6 interesting facts about Python int data type. Hope you enjoyed reading the article. Stay tuned for more existing articles on Python in the future articles.
Originally published at Medium on Jan 6, 2021.