Example:
tuple = ("python", "includehelp", 43, 54.23)
N element incremental Tuples
In this program, we are given a number N and a range (L, R). We need to create Python program for creating N element incremental tuple. We will create a tuple for each element of the range, with i [in the range (L, R)] repeatedly N times.
Input:
N = 4, [L, R] = [3, 6]
Output:
((3, 3, 3, 3), (4, 4, 4, 4), (5, 5, 5, 5), (6, 6, 6, 6), )
For this, we will loop for the range and for each number of range create repeat the given number N time and create a tuple with these elements.
Method 1:
One method to create N incremental tuples is by using a generator expression in which we will iterate through all elements in the range [L, R] and create a tuple with N element of that range. And finally, encapsulate it in a tuple using the tuple() method.
Output:
Method 2:
Another method to solve the problem is by using the repeat method to repeat the value of range N times. And create a tuple using the generator expression only.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer