ADVERTISEMENT
ADVERTISEMENT

Python program to perform multiplication operation on tuples

In this program, we are given two tuples. We need to create a Python program to perform multiplication operation on tuples.
Submitted by Shivang Yadav, on December 19, 2021

Tuples in Python are common types of collections that are used commonly for storing data records in python for processing. In fields like data science and AI, we need to process data tuples to extract fruitful information. In this problem, we will create a Python program to perform multiplication operation on tuples.

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)

Performing multiplication operation on Tuples

In this program, we are given two tuples. We need to create a Python program to return a tuple that contains the multiplication elements.

Input:
tuple1 = (4, 1, 7, 9) , tuple2 = (2, 4, 6, 7)

Output:
(8, 4, 1, 42, 63)

Python provides multiple ways to solve the problem and here we will see some of them working.

Method 1:

One method to solve the problem is by simply using generator expression where we will map the multiplication of the same index elements of both tuples using the zip() method can create a tuple for the resultant value.

# Python program to perform multiplication 
# operation on Tuples

# Initializing and printing both tuples 
tuple1 = (6, 2, 9, 1)
tuple2 = (8, 6, 4, 2)
print("Tuple 1 : " + str(tuple1))
print("Tuple 2 : " + str(tuple2))

# Performing multiplication operation on tuples
mulTuple = tuple(tup1Ele * tup2Ele for tup1Ele, tup2Ele in zip(tuple1, tuple2))

# Printing result
print("Multiplication tuple : " + str(mulTuple))

Output:

Tuple 1 : (6, 2, 9, 1)
Tuple 2 : (8, 6, 4, 2)
Multiplication tuple : (48, 12, 36, 2)

Method 2:

Another method to solve the problem is by using the map function with mul operator as a working function. And then converting the created map to a tuple using the tuple() method.

# Python program to perform multiplication 
# operation on Tuples

import operator

# Initializing and printing both tuples 
tuple1 = (6, 2, 9, 1)
tuple2 = (8, 6, 4, 2)
print("Tuple 1 : " + str(tuple1))
print("Tuple 2 : " + str(tuple2))

# Performing multiplication operation on tuples
mulTuple = tuple(map(operator.mul, tuple1, tuple2))

# Printing result
print("Multiplication tuple : " + str(mulTuple))

Output:

Tuple 1 : (6, 2, 9, 1)
Tuple 2 : (8, 6, 4, 2)
Multiplication tuple : (48, 12, 36, 2)

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.