ADVERTISEMENT
ADVERTISEMENT

Python program to find modulo of tuple elements

In this program, we are given two tuples with integer elements. We need to create a Python program to create a tuple containing elements that are modulo of tuple elements from both tuples at the given index.
Submitted by Shivang Yadav, on December 12, 2021

Records with numerical data showing important statistics and data are widely used in data science using the Python programming language. Python's ability to perform versatile tasks in an easy way lets it do this task also in an easy way. Here, in this article, we will see a Python program in which we will be performing modulus operations on elements of two tuples.

Before going further with the problem, lets 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)

Modulo of Tuple Elements

Modulus operator is used in returning the remainder value from the division of two values.

In this program, we are given two tuples with integer values as their elements. We will be creating a program to find the modulo of the elements of these two tuples.

Input: 
Tup1 = (8, 20, 6)
Tup2 = (3, 5, 7)

Output:
(2, 0, 6)

To find the modulo of tuple elements, we need to iterate over all elements of both tuples and perform modulus operation on each element pair and return the resultant value.

This can be done in python in multiple ways using its built-in methods,

Method 1:

One way is using the generator expression to create a new tuple consisting of values that is the result of modulus operation of elements of both tuples extracted using the zip() method.

# Python program to find modulo of tuple elements

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

# Finding modulo of tuple elements 
modTuple = tuple(val1 % val2 for val1, val2 in zip(tup1, tup2))

# Printing result
print("The modulo of tuple elements : " + str(modTuple))

Output:

The elements tuple 1 : (8, 20, 6)
The elements tuple 2 : (3, 5, 7)
The modulo of tuple elements : (2, 0, 6)

Method 2:

An alternate combination that will solve the problem is using the map() method to map all the modulus values of both tuple elements found using the mod operator imported from the operator library. Then converting this map to a tuple using the tuple() method.

# Python program to find modulo of tuple elements

# Importing operator library 
import operator

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

# Finding modulo of tuple elements 
modTuple = tuple(map(operator.mod, tup1, tup2))

# Printing result
print("The modulo of tuple elements : " + str(modTuple))

Output:

The elements tuple 1 : (8, 20, 6)
The elements tuple 2 : (3, 5, 7)
The modulo of tuple elements : (2, 0, 6)

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.