A tuple object is immutable in Python. Tuples are also sequences, just like Python lists. The tuples cannot be changed. This Python Tuple exercise aims to help Python developers to learn and practice tuple operations. Here I am providing 10 tuple programs to help you in brushing up your coding skills. All questions are tested on Python 3.
Further Reading:
- Explore All Python Exercises and Python Quizzes to practice Python
- Also, try to solve a Python Tuple Quiz
What included in this Tuple exercise?
The exercise contains 10 questions and solutions provided for each question. This Python tuple coding exercise is nothing but Python tuple assignments to solve, where you can solve and practice different tuple programs, questions, problems, and challenges.
Each question includes a specific tuple related topic you need to learn. This tuple exercise covers tuple creation, operations, unpacking of a tuple. When you complete each question, you get more familiar with Python tuple. Let us know if you have any alternative solutions. It will help other developers.
Use Online Code Editor to solve exercise questions.
Exercise Question 1: Reverse the following tuple
aTuple = (10, 20, 30, 40, 50)
Expected output:
(50, 40, 30, 20, 10)
Exercise Question 2: Access value 20 from the following tuple
aTuple = ("Orange", [10, 20, 30], (5, 15, 25))
Expected output:
20
Exercise Question 3: Create a tuple with single item 50
Exercise Question 4: Unpack the following tuple into 4 variables
aTuple = (10, 20, 30, 40)
Expected output:
aTuple = (10, 20, 30, 40) # Your code print(a) # should print 10 print(b) # should print 20 print(c) # should print 30 print(d) # should print 40
Exercise Question 5: Swap the following two tuples
tuple1 = (11, 22) tuple2 = (99, 88)
Expected output:
tuple1 = (99, 88) tuple2 = (11, 22)
Exercise Question 6: Copy element 44 and 55 from the following tuple into a new tuple
tuple1 = (11, 22, 33, 44, 55, 66)
Expected output:
tuple2 = (44, 55)
Exercise Question 7: Modify the first item (22) of a list inside a following tuple to 222
tuple1 = (11, [22, 33], 44, 55)
Expected output:
tuple1 = (11, [222, 33], 44, 55)
Exercise Question 8: Sort a tuple of tuples by 2nd item
tuple1 = (('a', 23),('b', 37),('c', 11), ('d',29))
Expected output:
(('c', 11), ('a', 23), ('d', 29), ('b', 37))
Exercise Question 9: Counts the number of occurrences of item 50 from a tuple
tuple1 = (50, 10, 60, 70, 50)
Expected output:
2
Exercise Question 10: Check if all items in the following tuple are the same
tuple1 = (45, 45, 45, 45)
Expected output:
True