Python Program to Find the Size of a Tuple

Given a tuple, the task is to find the size of the given tuple in Python.

Examples:

Example1:

Input:

Given tuple =(9, 11, 24, 19, 11, 23, 29, 23, 31)

Output:

The length of the given tuple (9, 11, 24, 19, 11, 23, 29, 23, 31) is [ 9 ]

Example2:

Input:

Given tuple =(11, 19, 45, 13, 23, 32, 46, 18, 49,58, 53, 75, 11, 19, 93, 99, 85, 68, 24)

Output:

The length of the given tuple (11, 19, 45, 13, 23, 32, 46, 18, 49, 58, 53, 75, 11, 19, 93, 99, 85, 68, 24) is [ 19 ]

Program to Find the Size of a Tuple in Python

Below are the ways to find the size of the given tuple in Python.

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Method #1: Using len() function

Approach:

  • Give the tuple as static input and store it in a variable.
  • Finding the size or length of a tuple is a rather simple task. The number of objects in a tuple is represented by its size. The syntax for determining the size is len (). The number of elements/objects in the tuple is returned by this function.
  • Calculate the len() using len() function and store it in a variable.
  • Print the length of the tuple.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gvntuple = (9, 11, 24, 19, 11, 23, 29, 23, 31)
# Finding the size or length of a tuple is a rather simple task.
# The number of objects in a tuple is represented by its size.
# The syntax for determining the size is len ().
# The number of elements/objects in the tuple is returned by this function.
# Calculate the len() using len() function and store it in a variable.
tuplelength = len(gvntuple)
# Print the length of the tuple.
print('The length of the given tuple', gvntuple, 'is [', tuplelength, ']')

Output:

The length of the given tuple (9, 11, 24, 19, 11, 23, 29, 23, 31) is [ 9 ]

Method #2: Using For loop

Approach:

  • Give the tuple as static input and store it in a variable.
  • Take a variable say tuplelength that calculates the given tuple length and initializes its value to 0.
  • Loop through the elements of the tuple using For loop.
  • Inside the For loop increment the value of tuplelength by 1.
  • Print the length of the tuple.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gvntuple = (9, 11, 24, 19, 11, 23, 29, 23, 31)
# Take a variable say tuplelength that calculates the given tuple
# length and initializes its value to 0.
tuplelength = 0
# Loop through the elements of #Inside the For loop increment the value of tuplelength by 1.the tuple using For loop.
for elemen in gvntuple:
    # Inside the For loop increment the value of tuplelength by 1.
    tuplelength = tuplelength+1

# Print the length of the tuple.
print('The length of the given tuple', gvntuple, 'is [', tuplelength, ']')

Output:

The length of the given tuple (9, 11, 24, 19, 11, 23, 29, 23, 31) is [ 9 ]

Related Programs: