ADVERTISEMENT
ADVERTISEMENT

Python program to raise elements of tuple as a power to another tuple

In this program, we are given two tuples with integer elements. We need to create a Python program to raise elements of the tuple as a power to another tuple.
Submitted by Shivang Yadav, on December 14, 2021

Python programming language has vast implications which require storage and operation on stored data to be performed in such a way that the extracted values can be used for some tasks. Here, we will see a program in which we will be raising the elements of a tuple as a power to the elements of another tuple.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

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.

# Python program to raise elements of tuple 
# as a power to another tuple

# Initializing and printing tuple values 
tup1 = (4, 1 ,7, 2)
tup2 = (2, 5, 3, 8)
print("The elements of tuple 1 : " + str(tup1))
print("The elements of tuple 2 : " + str(tup2))

# Raising elements of tuple as power to another tuple
powerTuple = tuple(val1 ** val2 for val1, val2 in zip(tup1, tup2))

print("The resultant power tuple is : " + str(powerTuple))

Output:

The elements of tuple 1 : (4, 1, 7, 2)
The elements of tuple 2 : (2, 5, 3, 8)
The resultant power tuple is : (16, 1, 343, 256)

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.

# Python program to raise elements of tuple 
# as a power to another tuple

# Initializing and printing tuple values 
tup1 = (4, 1 ,7, 2)
tup2 = (2, 5, 3, 8)
print("The elements of tuple 1 : " + str(tup1))
print("The elements of tuple 2 : " + str(tup2))

# Raising elements of tuple as power to another tuple
powerTuple = tuple(map(pow, tup1, tup2))

print("The resultant power tuple is : " + str(powerTuple))

Output:

The elements of tuple 1 : (4, 1, 7, 2)
The elements of tuple 2 : (2, 5, 3, 8)
The resultant power tuple is : (16, 1, 343, 256)

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.