
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Tuple multiplication in Python
When it is required to perform tuple multiplication, 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 = (23, 45, 12, 56, 78) my_tuple_2 = (89, 41, 76, 0, 11) 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 multiplied tuple is : ") print(my_result)
Output
The first tuple is : (23, 45, 12, 56, 78) The second tuple is : (89, 41, 76, 0, 11) The multiplied tuple is : (2047, 1845, 912, 0, 858)
Explanation
- Two tuples are defined, and are displayed on the console.
- They are zipped, and iterated through
- Every element from first tuple is multiple with the corresponding element in the second tuple.
- It is converted to a tuple.
- This operation is assigned to a value.
- It is displayed as output on the console.
- Related Articles
- Flatten tuple of List to tuple in Python
- Tuple Division in Python
- Unpacking a Tuple in Python
- Tuple Data Type in Python
- Delete Tuple Elements in Python
- Tuple XOR operation in Python
- Custom Multiplication in list of lists in Python
- Built-in Tuple Functions in Python
- Replace duplicates in tuple in Python
- Sort lists in tuple in Python
- How can I append a tuple into another tuple in Python?
- Raise elements of tuple as power to another tuple in Python
- Multiplication of two Matrices using Numpy in Python
- Program to apply Russian Peasant Multiplication in Python
- Scalar multiplication with Einstein summation convention in Python

Advertisements