Python Program to Find the Size of Primitive Data Types

In this python tutorial, you will learn how to find the size of Primitive Data Types using the sys.getsizeof() function and print function of the python programming language.

How to Find the size of Primitive Data Types in Python?

Let’s take a look at the source code , here the values are assigned in the code and the sys.getsizeof() carries out the function.

RUN CODE SNIPPET
import sys

a=sys.getsizeof(12)
print(a)

b=sys.getsizeof('bugs')
print(b)

c=sys.getsizeof(('b','u','g','s'))
print(c)

d=sys.getsizeof(['b','u','g','s'])
print(d)

e=sys.getsizeof({1,2,3,4})
print(e)

f=sys.getsizeof({1:'a',2:'b',3:'c',4:'d'})
print(f)

OUTPUT:

28
53
72
88
216
232
  1. At the start, we use the  import function with sys which allows to access certain system-specific parameters and functions.
  2. In the next line of code we declare the function sys.getsizeof()with the variable a , the integer 12 as the input value, followed by a print function which displays the value stored in the variable a.
  3. Similarly, we declare the function sys.getsizeof() with the variable b , the string ('bugs') as the input, followed by the print function which displays the value stored in the variable b.
  4. We declare the function sys.getsizeof() with the variable c , the tuple (('b','u','g','s')) enclosed in double parenthesis as the input, followed by the print function which displays the value stored in the variable c.
  5. We declare the function sys.getsizeof() with the variable d , the list (['b','u','g','s']) enclosed in square brackets and a parenthesis as the input, followed by the print function which displays the value stored in the variable d.
  6. We declare the function sys.getsizeof() with the variable e , the set ({1,2,3,4}) enclosed in curly brackets and parenthesis as the input, followed by the print function which displays the value stored in the variable e.
  7. We declare the function sys.getsizeof() with the variable f , the dict ({1:'a',2:'b',3:'c',4:'d'}) enclosed in curly brackets and parenthesis as the input, followed by the print function which displays the value stored in the variable f.

NOTE:

  • A byte, or eight bits, is used as the fundamental unit of measurement for data, computers use binary digits which is 0 and 1 to store data.
  • The import function searches for the module initially in the local scope or area and the value returned is seen in the output.
  • The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment.
  •  The integers are zero, positive or negative whole numbers without a fractional part.
  •  A string is a sequence of characters, where a character is simply a symbol. The string in enclosed in ‘ ‘ single quotes.
  •  Tuples are a data structure that store an ordered sequence of values, in simpler terms tuples are used to store multiple items in a single variable. Each value of the tuple is enclosed in single quotes ‘ ‘ and separated by a comma.
  • Lists are used to store multiple items in a single variable,  a list is created by placing all the items also called elements inside square brackets [] , separated by commas.
  • A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. In simpler terms, sets are used to store multiple items in a single variable. The elements are enclosed in curly brackets and separated by commas.
  • The dict() function is a constructor which creates a dictionary,  an unordered collection of items where each item of a dictionary has a key/value pair. The pair of keys/values are separated by a : colon with a comma between each pair and enclosed in single quotes.