What is size of integer in python?

The short answer

You're getting the size of the class, not of an instance of the class. Call int to get the size of an instance:

>>> sys.getsizeof[int[]]
24

If that size still seems a little bit large, remember that a Python int is very different from an int in [for example] c. In Python, an int is a fully-fledged object. This means there's extra overhead.

Every Python object contains at least a refcount and a reference to the object's type in addition to other storage; on a 64-bit machine, that takes up 16 bytes! The int internals [as determined by the standard CPython implementation] have also changed over time, so that the amount of additional storage taken depends on your version.

Some details about int objects in Python 2 and 3

Here's the situation in Python 2. [Some of this is adapted from a blog post by Laurent Luce]. Integer objects are represented as blocks of memory with the following structure:

typedef struct {
    PyObject_HEAD
    long ob_ival;
} PyIntObject;

PyObject_HEAD is a macro defining the storage for the refcount and the object type. It's described in some detail by the documentation, and the code can be seen in this answer.

The memory is allocated in large blocks so that there's not an allocation bottleneck for every new integer. The structure for the block looks like this:

struct _intblock {
    struct _intblock *next;
    PyIntObject objects[N_INTOBJECTS];
};
typedef struct _intblock PyIntBlock;

These are all empty at first. Then, each time a new integer is created, Python uses the memory pointed at by next and increments next to point to the next free integer object in the block.

I'm not entirely sure how this changes once you exceed the storage capacity of an ordinary integer, but once you do so, the size of an int gets larger. On my machine, in Python 2:

>>> sys.getsizeof[0]
24
>>> sys.getsizeof[1]
24
>>> sys.getsizeof[2 ** 62]
24
>>> sys.getsizeof[2 ** 63]
36

In Python 3, I think the general picture is the same, but the size of integers increases in a more piecemeal way:

>>> sys.getsizeof[0]
24
>>> sys.getsizeof[1]
28
>>> sys.getsizeof[2 ** 30 - 1]
28
>>> sys.getsizeof[2 ** 30]
32
>>> sys.getsizeof[2 ** 60 - 1]
32
>>> sys.getsizeof[2 ** 60]
36

These results are, of course, all hardware-dependent! YMMV.

The variability in integer size in Python 3 is a hint that they may behave more like variable-length types [like lists]. And indeed, this turns out to be true. Here's the definition of the C struct for int objects in Python 3:

struct _longobject {
    PyObject_VAR_HEAD
    digit ob_digit[1];
};

The comments that accompany this definition summarize Python 3's representation of integers. Zero is represented not by a stored value, but by an object with size zero [which is why sys.getsizeof[0] is 24 bytes while sys.getsizeof[1] is 28]. Negative numbers are represented by objects with a negative size attribute! So weird.

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 22 + 0 x 21 + 1 x 20

As you can see, it takes 3 bits to store the number 5 in the memory:

[101]2 = [5]10

Suppose that you have 8 bits, you can store up to 255 integers from zero to 255:

255= 1x 27 + 1 x 26 + 1 x 25 + 1x 24 + 1 x 23 + 1 x 22 + 1x 21 + 1 x 20

By using 8 bits, you can store up to 28 – 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 27 = 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.

8-bits = [-27, 27 – 1]

Similarly, if you want to use 16 bits, 32 bits, and 64 bits to store integers, the ranges would be:

  • 16-bits ~ [-215, 215 – 1] = [-32,768, 32,767]
  • 32-bits ~ [-231, 231 – 1] = [-2,147,483,648, 2,147,483,647]
  • 64-bits ~ [-263, 263 – 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]

Output:

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]

Ouput:

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]

Output:

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 264 :

from sys import getsizeof counter = 2**64 size = getsizeof[counter] print[size] # 36 bytes

Code language: Python [python]

Output:

36

Code language: Python [python]

So to store the integer 264, 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]

Output:

30 -10 200 100000000000000000000

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]

Output:

2.0

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.

Did you find this tutorial helpful ?

Why is an integer 28 bytes in Python?

Getting the size of an integer 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. It returns 28 bytes.

What is the size of integer in Python 3?

Integers in Python 3 are of unlimited size. Python 2 has two integer types - int and long. There is no 'long integer' in Python 3 anymore.

Can int be of any size in Python?

Python currently distinguishes between two kinds of integers [ints]: regular or short ints, limited by the size of a C long [typically 32 or 64 bits], and long ints, which are limited only by available memory.

How large can an integer in Python be in 64 bit?

maxint is at least 2**31-1 , and on a 64-bit environment, it is 2**63-1 . long has no maximum and minimum limit.

Chủ Đề