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)
Code language: Python (python)
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
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))
Code language: Python (python)
Expected output:
20
Show Hint
The given tuple is a nested tuple. Use indexing to locate the specified item.
Show Solution
Exercise 3: Create a tuple with single item 50
Show Solution
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)
Code language: Python (python)
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
Exercise 5: Swap two tuples in Python
Given:
tuple1 = (11, 22)
tuple2 = (99, 88)
Code language: Python (python)
Expected output:
tuple1: (99, 88) tuple2: (11, 22)
Show Solution
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)
Code language: Python (python)
Expected output:
tuple2: (44, 55)
Show Solution
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)
Code language: Python (python)
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
Exercise 8: Sort a tuple of tuples by 2nd item
Given:
tuple1 = (('a', 23),('b', 37),('c', 11), ('d',29))
Code language: Python (python)
Expected output:
(('c', 11), ('a', 23), ('d', 29), ('b', 37))
Show Solution
Exercise 9: Counts the number of occurrences of item 50 from a tuple
Given:
tuple1 = (50, 10, 60, 70, 50)
Code language: Python (python)
Expected output:
2
Show Hint
Use the count()
method of a tuple.
Show Solution
Exercise 10: Check if all items in the tuple are the same
tuple1 = (45, 45, 45, 45)
Code language: Python (python)
Expected output:
True