Size of different data types in Python

Viewed 796

I have been using Pycharm for Python executions. I'm using version 3.8. I'm not able to get the exact size of the data types in Python. I refereed the following link: https://www.geeksforgeeks.org/how-to-find-size-of-an-object-in-python/ But when I executed the same examples as mentioned in the link I didn't get a similar output.

import sys 
a = sys.getsizeof(12) 
print(a)
b=sys.getsizeof('geeks') 
print(b)

I executed the above code. The output for a was 14 and for b was 30. But according to the link, it should have been 28 and 54 respectively.

Please Help if there's any other way of calculating the size for the datatypes.

1 Answers

The size of each datatype in Python 3.8 is as follows:

Size of int is: 12

Size of float is: 16

Size of str is: 25

Size of list is: 20

Size of tuple is: 12

Size of dict is: 120

Size of set is: 100

Size of frozenset is: 100

Size of None is: 8

Size of Ellipsis is: 8

Size of Object is: 8

Size of Lambda is: 60

Size of Function-ref is: 60

Size of Exception is: 32

Got this by using the Script:

def f(): return

types = [int(0), float(0), str(), list(), tuple(), dict(), set(), frozenset(), None, ..., object(), lambda: None, f, Exception()]

names = ["int", "float", "str", "list", "tuple", "dict", "set", "frozenset", "None", "Ellipsis", "Object", "Lambda", "Function-ref", "Exception"]

sizes = list(map(lambda x: x.sizeof(), types))

for t, s in zip(names, sizes): print("Size of {} is: {}".format(t, s))

Related