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.
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.
Output:
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.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer