ADVERTISEMENT
ADVERTISEMENT

Python program to perform XOR operation on tuples

In this program, we are given two tuples. We need to create a Python program to perform XOR operation on tuples.
Submitted by Shivang Yadav, on December 14, 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 XOR 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)

XOR 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 XOR elements.

Sample Input:
tuple1 = (4, 1, 7, 9, 3) , tuple2 = (2, 4, 6, 7, 8)

Output:
(6, 5, 1, 14, 11)

Python provides multiple ways to solve the problem and here we will see some of them working.

Method 1:

One method to perform the XOR operation on elements of tuples is by using generator expression with the zip() to map the elements of both tuples using the XOR operation and create a tuple with resultant values.

# Python program to perform XOR 
# operation on tuples

# Initializing and printing tuple
myTup1 = (4, 1, 7, 9, 3)
myTup2 = (2, 4, 6, 7, 8)
print("The elements of tuple 1 : " + str(myTup1))
print("The elements of tuple 2 : " + str(myTup2))

# Performing XOR operation on tuples 
res = tuple(ele1 ^ ele2 for ele1, ele2 in zip(myTup1, myTup2))
print("The elements of XOR tuple : " + str(res))

Output:

The elements of tuple 1 : (4, 1, 7, 9, 3)
The elements of tuple 2 : (2, 4, 6, 7, 8)
The elements of XOR tuple : (6, 5, 1, 14, 11)

Method 2:

Another method to solve the problem is by mapping the XOR operation of the tuple elements done using the XOR operator. Then convert the map to a tuple using the tuple() method.

# Python program to perform XOR 
# operation on tuples

import operator

# Initializing and printing tuple
myTup1 = (4, 1, 7, 9, 3)
myTup2 = (2, 4, 6, 7, 8)
print("The elements of tuple 1 : " + str(myTup1))
print("The elements of tuple 2 : " + str(myTup2))

# Performing XOR operation on tuples 
res = tuple(map(operator.xor, myTup1, myTup2))
print("The elements of XOR tuple : " + str(res))

Output:

The elements of tuple 1 : (4, 1, 7, 9, 3)
The elements of tuple 2 : (2, 4, 6, 7, 8)
The elements of XOR tuple : (6, 5, 1, 14, 11)

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.