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 SNIPPETimport 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
- At the start, we use theÂ
import
function withsys
which allows to access certain system-specific parameters and functions. - In the next line of code we declare the function
sys.getsizeof()
with the variablea
, the integer12
as the input value, followed by aprint
function which displays the value stored in the variablea
. - Similarly, we declare the function
sys.getsizeof()
with the variableb
, the string('bugs')
as the input, followed by theÂprint
function which displays the value stored in the variableb
. - We declare the function
sys.getsizeof()
with the variablec
, the tuple(('b','u','g','s'))
enclosed in double parenthesis as the input, followed by theprint
function which displays the value stored in the variablec
. - We declare the function
sys.getsizeof()
with the variabled
, the list(['b','u','g','s'])
enclosed in square brackets and a parenthesis as the input, followed by theprint
function which displays the value stored in the variabled
. - We declare the function
sys.getsizeof()
with the variablee
, the set({1,2,3,4})
enclosed in curly brackets and parenthesis as the input, followed by theprint
function which displays the value stored in the variablee
. - We declare the function
sys.getsizeof()
with the variablef
, the dict({1:'a',2:'b',3:'c',4:'d'})
enclosed in curly brackets and parenthesis as the input, followed by theprint
function which displays the value stored in the variablef
.
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.