
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Raise elements of tuple as power to another tuple in Python
When it is required to raise the elements of one tuple, as a power of another tuple, the 'zip' method and the generator expression can be used.
The zip method takes iterables, aggregates them into a tuple, and returns it as the result.
Generator is a simple way of creating iterators. It automatically implements a class with '__iter__()' and '__next__()' methods and keeps track of the internal states, as well as raises 'StopIteration' exception when no values are present that could be returned.
Below is a demonstration of the same −
Example
my_tuple_1 = ( 7, 8, 3, 4, 3, 2) my_tuple_2 = (9, 6, 8, 2, 1, 0) print ("The first tuple is : " ) print(my_tuple_1) print ("The second tuple is : " ) print(my_tuple_2) my_result = tuple(elem_1 ** elem_2 for elem_1, elem_2 in zip(my_tuple_1, my_tuple_2)) print("The tuple raised to power of another tuple is : ") print(my_result)
Output
The first tuple is : (7, 8, 3, 4, 3, 2) The second tuple is : (9, 6, 8, 2, 1, 0) The tuple raised to power of another tuple is : (40353607, 262144, 6561, 16, 3, 1) > The first tuple is : (7, 8, 3, 4, 3, 2) The second tuple is : (9, 6, 8, 2, 1, 0) The tuple raised to power of another tuple is : (40353607, 262144, 6561, 16, 3, 1)
Explanation
- Two tuples are defined, and displayed on the console.
- The lists are iterated over, and they are zipped using the 'zip' method.
- The first element is taken as the power of the second element from both the tuples using the '**' operator.
- This is then converted to a tuple.
- This operation is assigned to a variable.
- This variable is the output that is displayed on the console.
- Related Questions & Answers
- Modulo of tuple elements in Python
- Delete Tuple Elements in Python
- How can I append a tuple into another tuple in Python?
- Flatten tuple of List to tuple in Python
- Summation of list as tuple attribute in Python
- How to append elements in Python tuple?
- Python - Join tuple elements in a list
- In MySQL, how to raise a number to the power of another number?
- Count the elements till first tuple in Python
- Python – Concatenate Rear elements in Tuple List
- C# Program to access tuple elements
- Raise a polynomial to a power in Python
- How to get unique elements in nested tuple in Python
- Python – Filter tuple with all same elements
- Tuple Division in Python
Advertisements