the way I do this (and its usually to ensure a fixed width integer before sending to some hardware) is via ctypes

from ctypes import c_ushort 

def hex16(self, data):
    '''16bit int->hex converter'''
    return  '0x%004x' % (c_ushort(data).value)
#------------------------------------------------------------------------------      
def int16(self, data):
    '''16bit hex->int converter'''
    return c_ushort(int(data,16)).value

otherwise struct can do it

from struct import pack, unpack
pack_type = {'signed':'>h','unsigned':'>H',}
pack(self.pack_type[sign_type], data)
Answer from Naib on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-data-types
Python Data Types - GeeksforGeeks
Python numbers represent data that has a numeric value. A numeric value can be an integer, a floating number or even a complex number.
Published   1 week ago
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python integers
An Essential Guide to Python Integers
March 27, 2025 - 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:
🌐
Reddit
reddit.com › r/learnpython › how much memory do int's take up in python?
r/learnpython on Reddit: How much memory do int's take up in python?
December 6, 2014 -

Going through the docs I didn't find a definite answer

If I do

x = 234234
sys.getsizeof(x)

It returns 28, so that's 224 bits ?

Apparently getsizeof() returns the size of an object in bytes, is this size consistent across all int's in python though?

I know that there are long ints, and that they are converted automatically to long ints at a certain value in python, but I'm just wondering about ints for now.

In Java the size that I got was 32 bit, so 4 bytes.

Is this correct, that :

python int = 224 bits

java int = 32 bits

And is this because the python int has more methods etc?

Basically what I was trying to state was that across programming languages the data type sizes were fairly conventional and that an int in C# would be the same size as an int in Python / Java. I think this might be wrong though.

Thanks!

Top answer
1 of 4
4

In Python, integers have arbitrary magnitude. You need not worry about how many bits are used in memory to store the integer; integers can be arbitrarily large (or large negative).

In this case, the 224 bits you are seeing are not all used to represent the integer; a lot of that is overhead of representing a Python object (as opposed to a native value in C-like languages).

EDIT: See also: https://docs.python.org/3/library/stdtypes.html#typesnumeric

2 of 4
3

is this because the python int has more methods etc?

Almost but not quite.

Internally, all python objects contain at least a reference count used by the garbage collector and a pointer to a [statically-allocated] type object (which is essentially an array of function pointers to the methods available on objects of that type). Objects with variable size (like lists, dicts, and python 3's variable-precision ints) also contain a size value.

So that's 3 values stored in the "header" of each python object. On a 32-bit platform these 3 values will most likely take 3*32 bits, and on a 64-bit platform they will take 3*64 bits. So any variable-size python object with zero size is going to take at least 24 bytes on a 64-bit machine.

Beyond that, the value of a python long integer is actually stored in an array of 15- or 30-bit chunks (depending on your build). On my 64-bit build, you can see the size of integers increase steadily as I increase their size by increments of 30 bits.

>>> [sys.getsizeof(2**(n*30) - 1) for n in range(100)]
[24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 
 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152, 156, 
 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204, 208, 212, 216, 
 220, 224, 228, 232, 236, 240, 244, 248, 252, 256, 260, 264, 268, 272, 276, 
 280, 284, 288, 292, 296, 300, 304, 308, 312, 316, 320, 324, 328, 332, 336, 
 340, 344, 348, 352, 356, 360, 364, 368, 372, 376, 380, 384, 388, 392, 396, 
 400, 404, 408, 412, 416, 420]

And there you have it, demystified I hope.

🌐
Codingem
codingem.com › home › python maximum/minimum integer value (with examples)
Python Maximum/Minimum Integer Value (with Examples)
December 8, 2022 - To get the maximum/minimum value for integer in Python, use the sys.maxsize. Notice that 'int' is not bounded in Python 3.0+.
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.3 documentation
February 25, 2026 - Also referred to as integer division. For operands of type int, the result has type int. For operands of type float, the result has type float. In general, the result is a whole integer, though the result’s type is not necessarily int.
🌐
Code-maven
python.code-maven.com › size-of-integer-in-python
Size of integer in Python
Accordingly they can hold integer numbers to the appropriate exponent of 2. So an 8-bit unsigned integer uses 8 bits (1 byte) and can hold values between 0-255 (that is 2**8-1). Python hides the need to make a decision and a variable in Python ...
🌐
Quora
quora.com › What-is-the-largest-number-that-Python-will-store-as-an-integer-without-converting-it-to-a-long-or-float-value
What is the largest number that Python will store as an integer without converting it to a long or float value? - Quora
Answer: Python can store integers in arbitrary precision which means that the largest possible integer that (still) fits in memory will be way too large to store in a mere float (Python does not support a long data type). You can test this by computing a very large integer like 2
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › learning python › int max or sys.maxint in python
Int Max or sys.maxint in Python
July 29, 2024 - Python's integers are of arbitrary precision, allowing for a vast range of values. The sys.maxsize attribute provides the maximum value a variable of type Py_ssize_t can take.
🌐
LinkedIn
linkedin.com › posts › reuven_how-big-is-a-python-integer-many-of-my-activity-7044578562690981888-FdWF
Reuven Lerner on LinkedIn: How big is a Python integer? Many of my students, especially those coming… | 15 comments
March 23, 2023 - As your integers grow, Python will use as much memory as needed, until it runs out of RAM. By the way, that's why we use NumPy/Pandas for data analysis: They use C-based integers, which are far smaller. But they also have a floor and ceiling on their sizes, whereas Python ints don't.
🌐
Spoken-tutorial
forums.spoken-tutorial.org › question › 1517 › int-data-type
Python-3.4.3 - Int data type - Spoken Tutorial Forums
In the spoken tutorial, I noticed ... Basic-datatypes-and-operators 04-05 min 10-20 sec ... YesCancel24-06-19, 1:56 p.m. LaxmiT · In c language size of int is 2 bytes, in java is 4 bytes....
🌐
freeCodeCamp
freecodecamp.org › news › maximum-integer-size-in-python
Int Max in Python – Maximum Integer Size
April 3, 2023 - You can perform operation with large integers values in Python without worrying about reaching the max value. The only limitation to using these large values is the available memory in the systems where they're being used. In this article, you have learned about the max integer size in Python.
🌐
Execute Program
executeprogram.com › courses › python-for-programmers › lessons › size-and-precision-of-numbers
Python for Programmers: Size and Precision of Numbers
Learn programming languages like TypeScript, Python, JavaScript, SQL, and regular expressions. Interactive with real code examples.
🌐
AskPython
askpython.com › home › variable’s memory size in python
Variable's memory size in Python - AskPython
April 29, 2023 - OUTPUT The size of the integer variable is: 24 bytes. So floating values in Python take up a total memory space of 24 bytes.
🌐
Protocol Buffers
protobuf.dev › programming-guides › proto3
Language Guide (proto 3) | Protocol Buffers Documentation
[4] 64-bit or unsigned 32-bit integers are always represented as long when decoded, but can be an int if an int is given when setting the field. In all cases, the value must fit in the type represented when set. See [2]. [5] Python strings are represented as unicode on decode but can be str ...
🌐
W3Schools
w3schools.com › python › python_datatypes.asp
Python Data Types
Python Variables Variable Names Assign Multiple Values Output Variables Global Variables Variable Exercises Code Challenge Python Data Types
🌐
DataCamp
datacamp.com › tutorial › everything-you-need-to-know-about-pythons-maximum-integer-value
What You Need to Know About Python's Maximum Integer Value | DataCamp
May 6, 2024 - Similar to the long data type in Python 2, the int data type in Python 3 is constrained only by the amount of memory available. In fact, because there is effectively no upper limit to the size of integers in Python 3, the long data type doesn't exist in Python 3.
🌐
Python
docs.python.org › 3 › c-api › long.html
Integer Objects — Python 3.14.3 documentation
February 24, 2026 - All integers are implemented as “long” integer objects of arbitrary size. On error, most PyLong_As* APIs return (return type)-1 which cannot be distinguished from a number. Use PyErr_Occurred() to disambiguate. ... Part of the Limited API (as an opaque struct). This subtype of PyObject represents a Python ...
🌐
Python
peps.python.org › pep-0237
PEP 237 – Unifying Long Integers and Integers | peps.python.org
February 1, 2025 - 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.