Home »
Python »
Python programs
Python program to perform AND operation on tuples
In this program, we are given two tuples. We need to create a Python program to perform AND operation on tuples.
Submitted by Shivang Yadav, on November 22, 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 AND 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)
AND Operation on Tuples
In this program, we are given two tuples. We need to create a python program to return a tuple that contains the AND elements.
Input:
tuple1 = (4, 1, 7, 9, 3),
tuple2 = (2, 4, 6, 7, 8)
Output:
(0, 0, 6, 1, 0)
Python provides multiple ways to solve the problem and here we will see some of them working.
Method 1:
One method to solve the problem is by simply computing the AND operation of all elements of tuples using the AND operator in the lambda function and mapping them. This collection is then converted to a tuple using the tuple() method.
# Python program to perform AND operation on tuples
# initializing and printing tuples
tuple1 = (4, 1, 7, 9, 3)
tuple2 = (2, 4, 6, 7, 8)
print("The elements of first tuple are "+str(tuple1))
print("The elements of second tuple are "+str(tuple2))
# Performing AND operation on tuples
andOperationTuple = tuple(map(lambda val1, val2: val1 & val2, tuple1, tuple2))
# Printing result
print("The result of AND operation on tuples are "+str(andOperationTuple))
Output:
The elements of first tuple are (4, 1, 7, 9, 3)
The elements of second tuple are (2, 4, 6, 7, 8)
The result of AND operation on tuples are (0, 0, 6, 1, 0)
Method 2:
Another method to solve the problem is by using the map() method to map each element of both tuples. And the operation on mapping is done using the iand() method to perform AND operation.
# Python program to perform AND operation on tuples
import operator
# Initializing and printing tuples
tuple1 = (4, 1, 7, 9, 3)
tuple2 = (2, 4, 6, 7, 8)
print("The elements of first tuple are "+str(tuple1))
print("The elements of second tuple are "+str(tuple2))
# Performing AND operation on tuples
andOperationTuple = tuple(map(operator.iand, tuple1, tuple2))
# Printing result
print("The result of AND operation on tuples are "+str(andOperationTuple))
Output:
The elements of first tuple are (4, 1, 7, 9, 3)
The elements of second tuple are (2, 4, 6, 7, 8)
The result of AND operation on tuples are (0, 0, 6, 1, 0)
Python Tuple Programs »