- Python Integers
- How computers store integers
- How Python represents integers
- Python int type
- Getting the size of an integer
- Python integer operations
- Summary
- Python Integer: Non-Fractional Numbers (With Example Code)
- Max size of a Python integer
- Integer types
- Converting from and to an integer
- String to integer
- Integer to string
- Float to integer
- Python random integer
- Is it a Python integer?
- Get certified with our courses
- Learn more
- Leave a Comment Cancel reply
- Why is Python Integer Size Different from Integers in C?
- Let’s Check – Why is Python Integer Size Different from Integers in C?
- Final Thoughts – Python Integer Size
Python Integers
Summary: in this tutorial, you’ll learn about Python integers and how Python stores integers in the memory.
Integers are whole numbers that include negative numbers, zero, and positive numbers such as -3, -2, -1, 0, 1, 2, 3.
Python uses the class int to represent all integer numbers. All integers are objects.
How computers store integers
Computers can’t store integers directly. Instead, they only can store binary numbers such as 0 and 1.
To store integers, the computers need to use binary numbers to represent the integers.
For example, to store the number 5, the computers need to represent it using a base-2 number:
5 = 1 x 2 2 + 0 x 2 1 + 1 x 2 0
As you can see, it takes 3 bits to store the number 5 in the memory:
Suppose that you have 8 bits, you can store up to 255 integers from zero to 255:
255= 1x 2 7 + 1 x 2 6 + 1 x 2 5 + 1x 2 4 + 1 x 2 3 + 1 x 2 2 + 1x 2 1 + 1 x 2 0
By using 8 bits, you can store up to 2 8 – 1 = 255 integers.
To store both negative integers, zero, and positive integers, you need to reserve 1 bit for storing the sign, negative (-) and positive (+). Therefore, with 8 bits:
- The largest integer that the computers can represent is 2 7 = 127.
- And the computers can store all integers in the range (-127, 127)
Because the number zero doesn’t have a sign, the computers can squeeze out an extra number. Therefore, 8 bits can store all integers from -128 to 127.
Similarly, if you want to use 16 bits, 32 bits, and 64 bits to store integers, the ranges would be:
- 16-bits ~ [-2 15 , 2 15 – 1] = [ -32,768 , 32,767 ]
- 32-bits ~ [-2 31 , 2 31 – 1] = [- 2,147,483,648 , 2,147,483,647 ]
- 64-bits ~ [-2 63 , 2 63 – 1] = [ -9,223,372,036,854,775,808 , 9,223,372,036,854,775,807 ]
How Python represents integers
Other programming languages such as Java and C# use a fixed number of bits to store integers.
For example, C# has the int type that uses 32-bits and the long type that uses 64 bits to represent integers. Based on the integer types, you can determine the ranges of the integers those types can represent.
Python, however, doesn’t use a fixed number of bit to store integers. Instead, Python uses a variable number of bits to store integers. For example, 8 bits, 16 bits, 32 bits, 64 bits, 128 bits, and so on.
The maximum integer number that Python can represent depends on the memory available.
Also, integers are objects. Python needs an extra fixed number of bytes as an overhead for each integer.
It’s important to note that the bigger the integer numbers are, the slower the calculations such as + , — , … will be.
Python int type
The following defines a variable that references an integer and uses the type() function to get the class name of the integer:
counter = 10 print(type(counter))
Code language: Python (python)
class 'int'>
Code language: Python (python)
As you can see clearly from the ouput, an integer is an instance of the class int .
Getting the size of an integer
To get the size of an integer, you use the getsizeof() function of the sys module.
The getsizeof() function returns the number of bytes that Python uses to represent an integer. For example:
from sys import getsizeof counter = 0 size = getsizeof(counter) print(size) # 24 bytes
Code language: Python (python)
24
Code language: Python (python)
To store the number 0, Python uses 24 bytes. Since storing the number zero, Python needs to use only 1 bit. Note that 1 byte equals 8 bits.
Therefore, you can think that Python uses 24 bytes as an overhead for storing an integer object.
The following returns the size of the integer 100:
from sys import getsizeof counter = 100 size = getsizeof(counter) print(size) # 28 bytes
Code language: Python (python)
28
Code language: Python (python)
It returns 28 bytes. Since 24 bytes is an overhead, Python uses 4 bytes to represent the number 100.
The following shows the size of the integer 2 64 :
from sys import getsizeof counter = 2**64 size = getsizeof(counter) print(size) # 36 bytes
Code language: Python (python)
36
Code language: Python (python)
So to store the integer 2 64 , Python uses 36 bytes.
Python integer operations
Python integers support all standard operations including:
- Addition +
- Subtraction –
- Multiplication *
- Division /
- Exponents **
The result of addition, subtraction, multiplication, and exponents of integers is an integer. For example:
a = 10 b = 20 c = a + b print(c) print(type(c)) c = a - b print(c) print(type(c)) c = a * b print(c) print(type(c)) c = a ** b print(c) print(type(c))
Code language: Python (python)
30 class 'int'> -10 class
'int'> 200 class 'int'> 100000000000000000000 class 'int'>Code language: Python (python)
However, the division of two integers always returns a floating-point number. For example:
a = 10 b = 5 c = a / b print(c) print(type(c))
Code language: Python (python)
2.0 class 'float'>
Code language: Python (python)
Summary
- Integers are whole numbers that include negative whole numbers, zero, and positive whole numbers.
- Computers use binary numbers to represent integers.
- Python uses a variable number of bits to represent integers. Therefore, the largest integer number that Python can represent depends on the available memory of the computer.
- In Python, all integers are instances of the class int .
- Use the getsizeof() function of the sys module to get the number of bytes of an integer.
- Python integers support all standard operations including addition, subtraction, multiplication, division, and exponent.
Python Integer: Non-Fractional Numbers (With Example Code)
The Python integer is a non-fractional number, like 1, 2, 45, -1, -2, and -100. It’s one of the three types of numbers Python supports natively, the others being floating-point numbers and complex numbers.
Max size of a Python integer
Unlike many other programming languages, integers in Python 3 can have large values. They are unbounded, meaning there is no limit to their size. For example:
>>> num = 98762345098709872345000 >>> num + 1 98762345098709872345001
Of course, there is a limit since your computer does not have unlimited memory. However, for all practical purposes, you don’t have to worry about it.
Integer types
Unlike Python 2 and many other languages, Python 3 has only one integer type. This is part of Python’s aspiration to be a clean, easy-to-learn language. It’s one less thing we have to worry about. For more details, see PEP-0237.
Converting from and to an integer
String to integer
To convert a string to an integer in Python, use the int() function:
Integer to string
To convert an integer to a string in Python, use the str() function:
Float to integer
To convert a float to an integer, use the int() function:
Python random integer
Many use cases require a random integer. For this, you need to import the module random . Be warned that this offers pseudo-randomness, which is not suitable for cryptography.
>>> import random >>> random.randint(1,10)
The above instruction returns a pseudo-random number from 1 to 10 inclusive, which means including 1 and 10. For full details of the random module, visit the Python documentation.
Is it a Python integer?
We can use the type() function to check if a value is an integer. It will return int for integers. Here’s a simple example of how to use this in an if -statement:
>>> type(2) int >>> if isinstance(2, int): . print('An integer') . An integer
Don’t use if type(2) == int .
Using isinstance() is almost always the better, cleaner way and covers more use cases, like subclasses.
Get certified with our courses
Our premium courses offer a superior user experience with small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.
Learn more
This article is part of my Python tutorial. You can head over to the start of the tutorial here. You can navigate this tutorial using the buttons at the top and bottom of the articles. To get an overview of all articles in the tutorial, please use the fold-out menu at the top.
If you liked this article, you might also like to read the following articles:
Leave a Comment Cancel reply
You must be logged in to post a comment.
You are browsing the free Python tutorial. Make sure to check out my full Python courses as well.
Subscribe to my newsletter for Python news, tips, and tricks!
Why is Python Integer Size Different from Integers in C?
Python integers are not just basic arithmetic types like integers or real numbers in C, they are objects with an internal data structure for storage. This is why Python’s integer size is different from other languages.
In fact, Python integers are variable-length, meaning they can grow as large as the available memory allows. This allows for precision and accuracy when dealing with large numbers.
But why is the size of Python integers larger than those in C? Let’s dive deeper into this topic to understand the inner workings of Python integers.
Let’s Check – Why is Python Integer Size Different from Integers in C?
Whenever Python finds an assignment like , it tries to construct an integer object. In Python, an integer object is seeded using the following data structure.
typedef struct < PyObject_HEAD long ob_ival; >PyIntObject;
The is a macro that expands to a reference counter of type and a pointer of type.
Next, Python reserves a pre-allocated block of Integer objects. And use it to serve new integer requests instead of making allocations on every assignment. Here is the block structure used to hold integer objects.
struct _intblock < struct _intblock *next; PyIntObject objects[N_INTOBJECTS]; >; typedef struct _intblock PyIntBlock;
#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Once a block’s integer object pool gets depleted, a new block becomes available to fulfill the new requests for integer objects. All these blocks are chained together in the form of a single linked list.
We should also mention that Python handles small integers in a little different fashion than big integers. It uses a type array of 262 pointers to store the small integers falling in the range from -5 to 256.
It means the small integers won’t get served from the object pool. Instead, they will use the list of pointers given above. Hence, the small integers will end up using the same PyIntObject integer object but with a different reference count.
On the contrary, the big integers get their allocation from the integer object pool. And each of them would have its own type object.
Another interesting fact that you should know is the difference between the lifetime of small and big integers. Unlike a big integer, small integers stay in memory as long as the Python interpreter is running. Observe this fact from the example given below.
small_int1=-5 => None small_int2=-5 => None small_int1 is small_int2 => True big_int1=257 => None big_int2=257 => None big_int1 is big_int2 => False
Final Thoughts – Python Integer Size
Till now, you’ve learned that Python integers are entirely different from the integer type available in C. And more importantly, you would now know the answer to your question- “Why is Python Integer Size Different from Integers in C?”.
We hope you enjoyed this post and have a clear idea of the integer object pool, its structure, and its application in Python.
But that’s not all! Our website offers a range of additional resources to help you build your skillset.
TechBeamers.