PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
  • Quizzes
  • Code Editor
Home » Python Exercises » Python Tuple Exercise with Solutions

Python Tuple Exercise with Solutions

Updated on: June 3, 2025 | 101 Comments

A tuple is an immutable object in Python that cannot be changed. Tuples are also sequences, just like Python lists. This Python Tuple exercise aims to help you learn and practice tuple operations.

Also Read:

  • Python Tuples
  • Python Tuple Quiz

This Python tuple exercise contains 19 coding questions, each with a provided solution.

  • Practice and solve various tuple operations, manipulations, and tuple functions.
  • All questions are tested on Python 3.
  • Read the complete guide on Python Tuples to solve this exercise.

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.

Table of contents

  • Exercise 1: Perform Basic Tuple Operations
  • Exercise 2: Tuple Repetition
  • Exercise 3: Slicing Tuples
  • Exercise 4: Reverse the tuple
  • Exercise 5: Access Nested Tuples
  • Exercise 6: Create a tuple with single item 50
  • Exercise 7: Unpack the tuple into 4 variables
  • Exercise 8: Swap two tuples in Python
  • Exercise 9: Copy Specific Elements From Tuple
  • Exercise 10: List to Tuple
  • Exercise 11: Function Returning Tuple
  • Exercise 12: Comparing Tuples
  • Exercise 13: Removing Duplicates from Tuple
  • Exercise 14: Filter Tuples
  • Exercise 15: Map Tuples
  • Exercise 16: Modify Tuple
  • Exercise 17: Sort a tuple of tuples by 2nd item
  • Exercise 18: Count Elements
  • Exercise 19: Check if all items in the tuple are the same

Exercise 1: Perform Basic Tuple Operations

  1. Create a Tuple: Create a tuple named my_tuple containing the numbers 1, 2, 3, 4, and 5.
  2. Access Elements: Access and print the third element of my_tuple.
  3. Tuple Length: Find and print the length of my_tuple.

Expected Output:

My tuple: (1, 2, 3, 4, 5)
The third element of my_tuple: 3
The length of my_tuple: 5
+ Hint
  • Create Tuples using parentheses ().
  • Use indexing to access elements, meaning the first element is at index 0, the second at index 1, and so on.
  • Use the built-in function len() to find the length of tuple.
+ Show Solution
# 1. Create a Tuple
my_tuple = (1, 2, 3, 4, 5)
print(f"My tuple: {my_tuple}")

# 2. Access Elements
third_element = my_tuple[2] # Index 2 corresponds to the third element
print(f"The third element of my_tuple: {third_element}")

# 3. Tuple Length
tuple_length = len(my_tuple)
print(f"The length of my_tuple: {tuple_length}")Code language: Python (python)

Exercise 2: Tuple Repetition

Repeat a below tuple three times.

Given:

original_tuple = ('a', 'b')Code language: Python (python)

Expected Output:

('a', 'b', 'a', 'b', 'a', 'b')
+ Hint

Use the * operator to repeat a tuple.

+ Show Solution
original_tuple = ('a', 'b')
repeated_tuple = original_tuple * 3
print(f"Repeated tuple: {repeated_tuple}")Code language: Python (python)

Exercise 3: Slicing Tuples

Slice below tuple to get elements from the 4th to the 7th position.

Given:

numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)Code language: Python (python)

Expected Output:

(4, 5, 6, 7)
+ Hint

Tuple slicing allows you to extract a portion of a tuple. The syntax [start:end] creates a new tuple containing elements from the start index up to, but not including, the end index.

+ Show Solution
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

# Slice from the 4th position (index 3) up to (but not including) the 8th position (index 7)
sliced_numbers = numbers[3:7]
print(f"Sliced tuple: {sliced_numbers}")Code language: Python (python)

Since we want the 4th to 7th positions, we use index 3 as the start (0-based) and index 7 as the end (because it’s exclusive, it will stop before the 8th element, effectively giving us the 4th, 5th, 6th, and 7th elements).

Exercise 4: 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
tuple1 = (10, 20, 30, 40, 50)
tuple1 = tuple1[::-1]
print(tuple1)Code language: Python (python)

Exercise 5: Access Nested Tuples

Write a code to access and print value 20 from given nested tuple.

Given:

tuple1 = ("Orange", [10, 20, 30], (5, 15, 25))Code language: Python (python)

Expected output:

20

Show Hint

Use double indexing. First, find the index of the inner tuple, then use a second index to find the element within that inner tuple.

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])Code language: Python (python)

Exercise 6: Create a tuple with single item 50

Show Solution
tuple1= (50, )
print(tuple1)Code language: Python (python)

Exercise 7: Unpack the tuple into 4 variables

Write a code 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
+ Hint

You can assign multiple variables to a tuple directly, provided the number of variables matches the number of elements in the tuple.

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)Code language: Python (python)

Exercise 8: 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
tuple1 = (11, 22)
tuple2 = (99, 88)
tuple1, tuple2 = tuple2, tuple1
print(tuple2)
print(tuple1)Code language: Python (python)

Exercise 9: Copy Specific Elements From 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
tuple1 = (11, 22, 33, 44, 55, 66)
tuple2 = tuple1[3:-1]
print(tuple2)Code language: Python (python)

Exercise 10: List to Tuple

Convert a list my_list = [10, 20, 30] into a tuple.

Expected Output:

(10, 20, 30)
+ Show Solution
my_list = [10, 20, 30]

# Use the tuple() constructor to convert the list
converted_tuple = tuple(my_list)
print(f"Converted list to tuple: {converted_tuple}")Code language: Python (python)

Exercise 11: Function Returning Tuple

Write a function get_min_max(numbers) that takes a list of numbers and returns a tuple containing the minimum and maximum number.

Given:

def get_min_max(numbers):
    # Write your code here

# Test the function
my_numbers = [10, 5, 20, 2, 15]
min_max_values = get_min_max(my_numbers)
print(f"Original numbers: {my_numbers}")
print(f"Minimum and maximum values: {min_max_values}")Code language: Python (python)

Expected Output:

Original numbers: [10, 5, 20, 2, 15]
Minimum and maximum values: (2, 20)
+ Hint
  • Use the built-in min() and max() functions that can be directly applied to a list.
  • Functions can return multiple values by separating them with commas, which implicitly creates a tuple.
+ Show Solution
def get_min_max(numbers):
    if not numbers:
        return (None, None) # Handle empty list case
    
    min_val = min(numbers)
    max_val = max(numbers)
    return (min_val, max_val)

# Test the function
my_numbers = [10, 5, 20, 2, 15]
min_max_values = get_min_max(my_numbers)
print(f"Original numbers: {my_numbers}")
print(f"Minimum and maximum values: {min_max_values}")Code language: Python (python)

Exercise 12: Comparing Tuples

Compare two tuples and find out which one is “greater” and why?

Given:

t1 = (1, 2, 3)
t2 = (1, 2, 4)Code language: Python (python)

Expected Output:

Tuple 1: (1, 2, 3, 8)
Tuple 2: (1, 2, 4, 5)
(1, 2, 3, 8) is less than (1, 2, 4, 5)
+ Hint

Python compares tuples lexicographically (element by element) from left to right. The first pair of elements that differ determines the outcome.

+ Show Solution
t1 = (1, 2, 3)
t2 = (1, 2, 4)

print(f"Tuple 1: {t1}")
print(f"Tuple 2: {t2}")

if t1 > t2:
    print(f"{t1} is greater than {t2}")
elif t1 < t2:
    print(f"{t1} is less than {t2}")
else:
    print(f"{t1} is equal to {t2}")Code language: Python (python)

Explanation:

Python compares tuples element by element.

  1. It compares t1[0] (which is 1) with t2[0] (which is 1). They are equal.
  2. It then moves to t1[1] (which is 2) and t2[1] (which is 2). They are also equal.
  3. Finally, it compares t1[2] (which is 3) with t2[2] (which is 4). Since 3 < 4, Python determines that t1 is less than t2

Exercise 13: Removing Duplicates from Tuple

Write a code to create a new tuple with only unique elements.

Given:

my_tuple = (1, 2, 2, 3, 4, 4, 5)Code language: Python (python)

Expected Output:

Original tuple with duplicates: (1, 2, 2, 3, 4, 4, 5)
Tuple with unique elements: (1, 2, 3, 4, 5)
+ Hint

Sets in Python inherently store only unique elements. You can convert the tuple to a set to remove duplicates, and then convert it back to a tuple. Note that sets do not preserve order.

+ Show Solution
my_tuple = (1, 2, 2, 3, 4, 4, 5)
print(f"Original tuple with duplicates: {my_tuple}")

# Convert to a set to remove duplicates (order is not preserved)
unique_elements_set = set(my_tuple)

# Convert back to a tuple
unique_tuple = tuple(unique_elements_set)
print(f"Tuple with unique elements: {unique_tuple}")Code language: Python (python)

Solution 2: If preserving original order of first appearance is important:

from collections import OrderedDict
my_tuple = (1, 2, 2, 3, 4, 4, 5)
print(f"Original tuple with duplicates: {my_tuple}")

unique_ordered_tuple = tuple(OrderedDict.fromkeys(my_tuple))
print(f"Tuple with unique elements (order preserved): {unique_ordered_tuple}")Code language: Python (python)

Exercise 14: Filter Tuples

Write a code to filter out students with scores less than 90 from a given a list of tuples.

Given:

students = [('Alice', 85), ('Bob', 92), ('Charlie', 78)]Code language: Python (python)

Expected Output:

Students with scores 90 or above: [('Bob', 92), ('David', 95)]
+ Hint

Use a loop with a conditional statement, or a list comprehension with a condition. Access the score (the second element) of each student tuple.

+ Show Solution

Solution 1: Using loop

students = [('Alice', 85), ('Bob', 92), ('Charlie', 78), ('David', 95)]
print(f"Original student list: {students}")

high_achievers_loop = []
for student in students:
  if student[1] >= 90:
    high_achievers_loop.append(student)
print(f"Students with scores 90 or above (loop method): {high_achievers_loop}")Code language: Python (python)

Solution 2: Using list comprehension

students = [('Alice', 85), ('Bob', 92), ('Charlie', 78), ('David', 95)]
print(f"Original student list: {students}")

# Using a list comprehension for filtering
# For each student tuple, check if their score (student[1]) is >= 90
high_achievers = [student for student in students if student[1] >= 90]
print(f"Students with scores 90 or above: {high_achievers}")Code language: Python (python)

Exercise 15: Map Tuples

Given a tuple of numbers, create a new tuple where each number is squared.

Given:

t = (1, 2, 3, 4)Code language: Python (python)

Expected Output:

Original tuple: (1, 2, 3, 4)
Squared tuple: (1, 4, 9, 16)
+ Hint

Use a loop, a list comprehension (and then convert to tuple), or the map() function with a lambda expression or a regular function. The map() function is often used for applying a function to each item in an iterable.

+ Show Solution

Solution 1: Using map() and tuple()

t = (1, 2, 3, 4)
print(f"Original tuple: {t}")

# Method 1: Using map() and tuple()
squared_tuple_map = tuple(map(lambda x: x**2, t))
print(f"Squared tuple (map function): {squared_tuple_map}")Code language: Python (python)

Solution 2: Using a loop

# 7. Map Tuples
t = (1, 2, 3, 4)
print(f"Original tuple: {t}")

# Method 2: Using a loop
squared_list_loop = []
for num in t:
  squared_list_loop.append(num ** 2)
  squared_tuple_loop = tuple(squared_list_loop)
print(f"Squared tuple (loop): {squared_tuple_loop}")Code language: Python (python)

Exercise 16: Modify 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
tuple1 = (11, [22, 33], 44, 55)
tuple1[1][0] = 222
print(tuple1)Code language: Python (python)

Exercise 17: Sort a tuple of tuples by 2nd item

Given:

tuple1 = (('a', 23),('b', 37),('c', 11), ('d',29))Code language: Python (python)

Expected output:

(Sorted tuple by 2nd item: (('c', 11), ('a', 23), ('d', 29), ('b', 37))
+ Hint

Tuples are immutable, so you cannot sort them directly. You’ll need to convert the tuple to a list first, sort the list, and then optionally convert it back to a tuple.

Show Solution
# 1. Sort a tuple by 2nd item
tuple1 = (('a', 23), ('b', 37), ('c', 11), ('d', 29))
print(f"Original tuple: {tuple1}")

# Convert the tuple to a list because tuples are immutable and cannot be sorted in-place
list1 = list(tuple1)

# Sort the list using the 'sorted()' function with a lambda key
# The lambda function `lambda item: item[1]` tells sorted() to use the second element
# (index 1) of each inner tuple for comparison.
sorted_list = sorted(list1, key=lambda item: item[1])

# If you need the result back as a tuple, convert the sorted list back to a tuple
sorted_tuple = tuple(sorted_list)

print(f"Sorted tuple by 2nd item: {sorted_tuple}")Code language: Python (python)

Exercise 18: Count Elements

Write a code to 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
tuple1 = (50, 10, 60, 70, 50)
print(tuple1.count(50))Code language: Python (python)

Exercise 19: Check if all items in the tuple are the same

tuple1 = (45, 45, 45, 45)Code language: Python (python)

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))
Code language: Python (python)

Filed Under: Python, Python Basics, Python Exercises

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Image

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python Basics Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Loading comments... Please wait.

In: Python Python Basics Python Exercises
TweetF  sharein  shareP  Pin

  Python Exercises

  • All Python Exercises
  • Basic Exercise for Beginners
  • Input and Output Exercise
  • Loop Exercise
  • Functions Exercise
  • String Exercise
  • Data Structure Exercise
  • List Exercise
  • Dictionary Exercise
  • Set Exercise
  • Tuple Exercise
  • Date and Time Exercise
  • OOP Exercise
  • File Handling Exercise
  • Python JSON Exercise
  • Random Data Generation Exercise
  • NumPy Exercise
  • Pandas Exercise
  • Matplotlib Exercise
  • Python Database Exercise

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

Python Basics Python Exercises Python Quizzes Python Interview Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2025 pynative.com

Advertisement