Browse through various articles and courses for Free at DeveloperPublish.com

HomePythonPython Program to Find the Size of Primitive Data Types

# 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.

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.

### You May Also Like

#### Python Program to Remove Duplicates from a Linked List

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...

#### Python Program to Solve the Celebrity Problem

This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...

#### Python Program to Solve n-Queen Problem with Recursion

This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...