ADVERTISEMENT
ADVERTISEMENT

Python | Tuple elements inversions

In this program, we are given a tuple consisting of integer elements. And our task is to create a Python program to perform the Tuple elements inversions.
Submitted by Shivang Yadav, on November 06, 2021

While working with tuples in python, we can perform operations and integer elements of the collection are manipulated more often. Here, we will create a python program that will perform bitwise inversion of each element of the tuple.

Bitwise inversion operation (~) is the operation that inverts all bits of the operand.

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)

Performing the Tuple element inversion

We are given a tuple consisting of integer values only. And we will create a program to perform tuple element inversion.

Input:
(3, 1, 5, 9)

Output:
(-4, -2, -6, -10)

To solve the problem, we will be traversing each element of the tuple. And perform bitwise inversion operation on them and return the inverted element tuple.

In the Python programming language, we have multiple methods to perform tuple inversion.

Method 1:

One method to invert elements of the tuple is by using a lambda function inside the map() with the function as an inversion operator. This will create a collection will all elements of the tuple inverted. Then we will convert it into a tuple.

# Python program to perform 
# tuple elements inversion

# Creating and print the 
# initial tuple
myTuple = (3, 1, 5, 9)
print("Initially elements of the tuple are " + str(myTuple))

# Bitwise inverting elements 
# of the tuple
invTuple = tuple(list(map(lambda x: ~x, list(myTuple))))

print("Bitwise inversion of all tuple elements are " + str(invTuple))

Output:

Initially elements of the tuple are (3, 1, 5, 9)
Bitwise inversion of all tuple elements are (-4, -2, -6, -10)

Method 2:

Another method to bitwise inverts the elements of the tuple is by using the operator.invert instead of inversion operator as the map's lambda function.

# Python program to perform 
# tuple elements inversion

import operator

# Creating and print the initial tuple
myTuple = (3, 1, 5, 9)
print("Initially elements of the tuple are " + str(myTuple))

# Bitwise inverting elements of the tuple
invTuple = tuple(list(map(operator.invert, list(myTuple))))

print("Bitwise inversion of all tuple elements are " + str(invTuple))

Output:

Initially elements of the tuple are (3, 1, 5, 9)
Bitwise inversion of all tuple elements are (-4, -2, -6, -10)

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.