Example:
tuple = ("python", "includehelp", 43, 54.23)
Raise elements of tuple as a power to another tuple
In this article, we will be creating a python program where we will be given two tuples with integer values and the program will raise the elements of the tuple as the power to another tuple.
Input:
tup1 = (4, 1 ,7, 2)
tupl2 = (2, 5, 3, 8)
Output:
(16, 1, 343, 256)
Explanation:
The values are 42 , 15, 73, 28
For performing the task, we will simply traverse both the tuple with the same index and perform the power operation on the values. This can be done in python in a much easier way, let's explore it.
Method 1:
One method to perform the task is to use the generator expression which will create a new tuple consisting of values that are the result of exponent operation (done using ** operator). The zip() method is employed to create a new tuple using values of two tuples.
Output:
Method 2:
Another method to solve the problem is by mapping the exponential operation of tuple elements done using the pow() method. Then convert the map to a tuple using tuple() method.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer