How to Get the Size of a Tuple in Python

In this tutorial, we will program 'How to Get the Size of a Tuple in Python.' We will learn how to get the full size of each tuple presented. The objective is to allow you to get the actual size of a tuple. I will provide a sample program to demonstrate the actual coding process in this tutorial.

This topic is very easy to understand. Just follow the instructions I provide, and you can do it yourself with ease. The program I will show you covers the basics of programming to get the full size of a tuple using some methods. I will do my best to provide you with a simple method for getting the size of a tuple. So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. mytuple1 = ("John", "Claire", "Naruto", "Steve", "Beast", "Sheer")
  2. mytuple2 = ((1, "Dog"), ( 2, "Cat"), (3, "Whale"), (4, "Bear"))
  3.  
  4. ret = False
  5.  
  6. while True:
  7.     print("\n================== Get the Size of a Tuple ==================\n\n")
  8.  
  9.     print("My Tuple 1: ", mytuple1)
  10.     print("My Tuple 2: ", mytuple2)
  11.     print("\n")
  12.     print("Tuple 1 size is: " + str(mytuple1.__sizeof__()) + "bytes")
  13.     print("Tuple 2 size is: " + str(mytuple2.__sizeof__()) + "bytes")
  14.  
  15.     opt = input("\nDo you want to try again?(yes/no): ")
  16.  
  17.     if opt.lower() == 'yes':
  18.             ret=False
  19.     elif opt.lower() == 'no':
  20.             ret=True
  21.             print("Exiting program....")
  22.  
  23.     else:
  24.             print("Please enter yes/no:")
  25.             break
  26.  
  27.     if ret == False:
  28.             continue

This script prints the size of the tuples mytuple1 and mytuple2 in bytes. It repeatedly prompts the user to rerun the size calculation process or exit the program. Each time, it displays the tuples and their sizes, allowing the user to choose whether to continue or exit based on their input.

Output:

There you have it we successfully created How to Get the Size of a Tuple in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment