GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Get the size of an object in bytes - w3resource

Python: Get the size of an object in bytes

Last update on May 17 2025 05:43:31 (UTC/GMT +8 hours)


Object Size Finder

Write a Python program to get the size of an object in bytes.

Pictorial Presentation:

Get the size of an object in bytes


Sample Solution:

Python Code:

import sys  # Import the sys module to use sys.getsizeof()

# Define three strings and assign values to them
str1 = "one"
str2 = "four"
str3 = "three"
x = 0
y = 112
z = 122.56

# Print the size in bytes of each variable
print("Size of ", str1, "=", str(sys.getsizeof(str1)) + " bytes")
print("Size of ", str2, "=", str(sys.getsizeof(str2)) + " bytes")
print("Size of ", str3, "=", str(sys.getsizeof(str3)) + " bytes")
print("Size of", x, "=", str(sys.getsizeof(x)) + " bytes")
print("Size of", y, "=", str(sys.getsizeof(y)) + " bytes")

# Define a list and assign values to it
L = [1, 2, 3, 'Red', 'Black']

# Print the size in bytes of the list
print("Size of", L, "=", sys.getsizeof(L), " bytes")

# Define a tuple and assign values to it
T = ("Red", [8, 4, 6], (1, 2, 3))

# Print the size in bytes of the tuple
print("Size of", T, "=", sys.getsizeof(T), " bytes")

# Define a set and assign values to it
S = {'apple', 'orange', 'apple', 'pear'}

# Print the size in bytes of the set
print("Size of", S, "=", sys.getsizeof(S), " bytes")

# Define a dictionary and assign values to it
D = {'Name': 'David', 'Age': 6, 'Class': 'First'}

# Print the size in bytes of the dictionary
print("Size of", D, "=", sys.getsizeof(S), " bytes")

Sample Output:

Size of  one = 52 bytes
Size of  four = 53 bytes
Size of  three = 54 bytes
Size of 0 = 24 bytes
Size of 112 =28 bytes
Size of [1, 2, 3, 'Red', 'Black'] = 104  bytes
Size of ('Red', [8, 4, 6], (1, 2, 3)) = 72  bytes
Size of {'orange', 'pear', 'apple'} = 224  bytes
Size of {'Name': 'David', 'Age': 6, 'Class': 'First'} = 224  bytes

For more Practice: Solve these Related Problems:

  • Write a Python program to compare the memory size of different data structures.
  • Write a Python program to get the size of a user-defined class object.
  • Write a Python program to measure memory usage before and after creating a large list.
  • Write a Python program to calculate the size of nested objects.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to find the available built-in modules.
Next: Write a Python program to get the current value of the recursion limit.

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.