 # Python Program for Modulo of Tuple Elements

In the previous article, we have discussed Python Program to find Sum of Tuple Items

Given a tuple and the task is to find the modulo of the given tuple elements.

zip() function in python:

The zip() function returns a zip object, which is an iterator of tuples in which the first item in each provided iterator is coupled together, and so on.

If the lengths of the provided iterators differ, the length of the new iterator is determined by the iterator with the fewest items.

Examples:

Example1:

Input:

Given First Tuple= (1, 10, 12, 3, 9)
Given Second Tuple=  (10, 9, 8, 7, 6)

Output:

The modulus of the given tuple elements (1, 10, 12, 3, 9) and (10, 9, 8, 7, 6) is:
(1, 1, 4, 3, 3)

Example2:

Input:

Given First Tuple= (12, 15, 18, 20, 64)
Given Second Tuple= (5, 6, 1, 7, 8)

Output:

The modulus of the given tuple elements (12, 15, 18, 20, 64) and (5, 6, 1, 7, 8) is:
(2, 3, 0, 6, 0)

## Program for Modulo of Tuple Elements in Python

Below are the ways to find the modulo of the given tuple elements:

### Method #1: Using zip() Function (Static Input)

Approach:

• Give the first tuple as static input and store it in a variable.
• Give the second tuple as static input and store it in another variable.
• Loop in the first and second tuples using the zip() function, apply modulus function to the first tuple iterator value with the second tuple iterator value and convert this statement to a tuple using the tuple() function.
• Print the modulo of the given tuple elements.
• The Exit of the program.

Below is the implementation:

# Give the first tuple as static input and store it in a variable.
fst_tupl = (1, 10, 12, 3, 9)
# Give the second tuple as static input and store it in another variable.
secnd_tupl = (10, 9, 8, 7, 6)
# Loop in the first and second tuples using the zip() function, apply modulus function to
# the first tuple iterator value with the second tuple iterator value and convert this
# statement to a tuple using the tuple() function.
rslt_tupl = tuple(ele1 % ele2 for ele1, ele2 in zip(fst_tupl, secnd_tupl))
# Print the modulo of the given tuple elements.
print("The modulus of the given tuple elements",
fst_tupl, "and", secnd_tupl, "is:")
print(rslt_tupl)


Output:

The modulus of the given tuple elements (1, 10, 12, 3, 9) and (10, 9, 8, 7, 6) is:
(1, 1, 4, 3, 3)

### Method #2: Using zip() Function (User Input)

Approach:

• Give the first tuple as user input using tuple(),map(),input(),and split() functions and Store it in a variable.
• Give the second tuple as user input using tuple(),map(),input(),and split() functions and Store it in another variable.
• Loop in the first and second tuples using the zip() function, apply modulus function to the first tuple iterator value with the second tuple iterator value and convert this statement to a tuple using the tuple() function.
• Print the modulo of the given tuple elements.
• The Exit of the program.

Below is the implementation:

# Give the first tuple as user input using tuple(),map(),input(),and split() functions and
# Store it in a variable.
fst_tupl = tuple(map(int, input( 'Enter some random tuple Elements separated by spaces = ').split()))
# Give the second tuple as user input using tuple(),map(),input(),and split() functions and
# Store it in another variable.
secnd_tupl = tuple(map(int, input( 'Enter some random tuple Elements separated by spaces = ').split()))
# Loop in the first and second tuples using the zip() function, apply modulus function to
# the first tuple iterator value with the second tuple iterator value and convert this
# statement to a tuple using the tuple() function.
rslt_tupl = tuple(ele1 % ele2 for ele1, ele2 in zip(fst_tupl, secnd_tupl))
# Print the modulo of the given tuple elements.
print("The modulus of the given tuple elements",
fst_tupl, "and", secnd_tupl, "is:")
print(rslt_tupl)


Output:

Enter some random tuple Elements separated by spaces = 12 15 18 20 64
Enter some random tuple Elements separated by spaces = 5 6 1 7 8
The modulus of the given tuple elements (12, 15, 18, 20, 64) and (5, 6, 1, 7, 8) is:
(2, 3, 0, 6, 0)

Remediate your knowledge gap by attempting the Python Code Examples regularly and understand the areas of need and work on them.