A tuple is an immutable object in Python that can’t be changed. Tuples are also sequences, just like Python lists.
This Python Tuple exercise aims to help you to learn and practice tuple operations. All questions are tested on Python 3.
Also Read:
This Tuple exercise includes the following: –
- It contains 10 questions and solutions provided for each question.
- It has questions to practice Python tuple assignments, programs, and challenges.
- 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.
- Read the complete guide on Python Tuples to solve this exercise.
Table of contents
- Exercise 1: Reverse the tuple
- Exercise 2: Access value 20 from the tuple
- Exercise 3: Create a tuple with single item 50
- Exercise 4: Unpack the tuple into 4 variables
- Exercise 5: Swap two tuples in Python
- Exercise 6: Copy specific elements from one tuple to a new tuple
- Exercise 7: Modify the tuple
- Exercise 8: Sort a tuple of tuples by 2nd item
- Exercise 9: Counts the number of occurrences of item 50 from a tuple
- Exercise 10: Check if all items in the tuple are the same
Exercise 1: Reverse the tuple
Given:
tuple1 = (10, 20, 30, 40, 50)
Expected output:
(50, 40, 30, 20, 10)
Show Hint
Use tuple slicing to reverse the given tuple. Note: the last element starts at -1.
Show Solution
tuple1 = (10, 20, 30, 40, 50)
tuple1 = tuple1[::-1]
print(tuple1)
Exercise 2: Access value 20 from the tuple
The given tuple is a nested tuple. write a Python program to print the value 20.
Given:
tuple1 = ("Orange", [10, 20, 30], (5, 15, 25))
Expected output:
20
Show Hint
The given tuple is a nested tuple. Use indexing to locate the specified item.
Show Solution
tuple1 = ("Orange", [10, 20, 30], (5, 15, 25))
# understand indexing
# tuple1[0] = 'Orange'
# tuple1[1] = [10, 20, 30]
# list1[1][1] = 20
print(tuple1[1][1])
Exercise 3: Create a tuple with single item 50
Show Solution
tuple1= (50, )
print(tuple1)
Exercise 4: Unpack the tuple into 4 variables
Write a program to unpack the following tuple into four variables and display each variable.
Given:
tuple1 = (10, 20, 30, 40)
Expected output:
tuple1 = (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
Show Solution
tuple1 = (10, 20, 30, 40)
# unpack tuple into 4 variables
a, b, c, d = tuple1
print(a)
print(b)
print(c)
print(d)
Exercise 5: Swap two tuples in Python
Given:
tuple1 = (11, 22)
tuple2 = (99, 88)
Expected output:
tuple1: (99, 88) tuple2: (11, 22)
Show Solution
tuple1 = (11, 22)
tuple2 = (99, 88)
tuple1, tuple2 = tuple2, tuple1
print(tuple2)
print(tuple1)
Exercise 6: Copy specific elements from one tuple to a new tuple
Write a program to copy elements 44 and 55 from the following tuple into a new tuple.
Given:
tuple1 = (11, 22, 33, 44, 55, 66)
Expected output:
tuple2: (44, 55)
Show Solution
tuple1 = (11, 22, 33, 44, 55, 66)
tuple2 = tuple1[3:-1]
print(tuple2)
Exercise 7: Modify the tuple
Given is a nested tuple. Write a program to modify the first item (22) of a list inside a following tuple to 222
Given:
tuple1 = (11, [22, 33], 44, 55)
Expected output:
tuple1: (11, [222, 33], 44, 55)
Show Hint
The given tuple is a nested tuple. Use indexing to locate the specified item and modify it using the assignment operator.
Show Solution
tuple1 = (11, [22, 33], 44, 55)
tuple1[1][0] = 222
print(tuple1)
Exercise 8: Sort a tuple of tuples by 2nd item
Given:
tuple1 = (('a', 23),('b', 37),('c', 11), ('d',29))
Expected output:
(('c', 11), ('a', 23), ('d', 29), ('b', 37))
Show Solution
tuple1 = (('a', 23), ('b', 37), ('c', 11), ('d', 29))
tuple1 = tuple(sorted(list(tuple1), key=lambda x: x[1]))
print(tuple1)
Exercise 9: Counts the number of occurrences of item 50 from a tuple
Given:
tuple1 = (50, 10, 60, 70, 50)
Expected output:
2
Show Hint
Use the count()
method of a tuple.
Show Solution
tuple1 = (50, 10, 60, 70, 50)
print(tuple1.count(50))
Exercise 10: Check if all items in the tuple are the same
tuple1 = (45, 45, 45, 45)
Expected output:
True
Show Solution
def check(t):
return all(i == t[0] for i in t)
tuple1 = (45, 45, 45, 45)
print(check(tuple1))