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:
- python program to find the factorial of a number
- python program to find the factors of a number
- python program to find the cumulative sum of a list
- python program to sort a list of tuples in increasing order by the last element in each tuple
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to find the power of a number using recursion
- python program to find the sum of elements in a list recursively