Python program to find the modulo of tuple elements

In this program, we are given two tuples consisting of integer elements. We need to create a Python program to find the modulo of tuple elements.
Submitted by Shivang Yadav, on December 04, 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 modulus 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)

Modulo of Tuple Elements

We are given two tuples consisting of integer elements. We need to create a Python program to find the modulo of tuple elements.

Input:
tuple1 = (4, 8, 7, 3, 9)
tuple2 = (3, 5, 2, 4, 3)

Output:
(1, 3, 1, 3, 0)

To find the solution, we will be simply iterating over both the tuples performing modulus operation i.e. tulp1 % tuple2 for each element. In python, we can perform the task in multiple ways using one of the multiple methods that are present in the function.

Method 1:

One method to solve the problem is by using the generator expression to perform the modulus operation on both the tuples by zipping the values using zip() method.

# Python program to find the 
# modulo of tuple elements
  
# Initializing and printing Tuples 
myTup1 = (4, 8, 7, 3, 9)
myTup2 = (3, 5, 2, 4, 3)

print("The elements of tuple 1 are " + str(myTup1))
print("The elements of tuple 2 are " + str(myTup2))
  
# Performing tuples modulus operation
retModulo = tuple(ele1 % ele2 for ele1, ele2 in zip(myTup1, myTup2))
print("The elements of resultant tuple of modulo operation are " + str(retModulo))

Output:

The elements of tuple 1 are (4, 8, 7, 3, 9)
The elements of tuple 2 are (3, 5, 2, 4, 3)
The elements of resultant tuple of modulo operation are (1, 3, 1, 3, 0)

Method 2:

Another method to solve the problem is directly using the map() method for mapping the result of the mod operation on each element of the tuples.

# Python program to find the 
# modulo of tuple elements

from operator import mod
  
# Initializing and printing Tuples 
myTup1 = (4, 8, 7, 3, 9)
myTup2 = (3, 5, 2, 4, 3)
print("The elements of tuple 1 are " + str(myTup1))
print("The elements of tuple 2 are " + str(myTup2))
  
# Performing tuples modulus operation
retModulo = tuple(map(mod, myTup1, myTup2))
print("The elements of resultant tuple of modulo operation are " + str(retModulo))

Output:

The elements of tuple 1 are (4, 8, 7, 3, 9)
The elements of tuple 2 are (3, 5, 2, 4, 3)
The elements of resultant tuple of modulo operation are (1, 3, 1, 3, 0)

Python Tuple Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.