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 »





Comments and Discussions!

Load comments ↻





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