Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python Tuple Exercises – Practice Problems with Solutions

Tuples are one of Python's built-in data structures used to store multiple items in a single variable. Unlike lists, tuples are ordered, allow duplicate values, and are immutable, meaning their contents cannot be changed after creation.

Practicing tuple exercises is one of the best ways to improve your understanding of Python data structures and prepare for coding interviews, assignments, and real-world projects.

In this tutorial, you'll find beginner-friendly to intermediate-level tuple exercises along with detailed solutions and explanations.


What is a Tuple?

A tuple is created using parentheses ().

Example:

colors = ("red", "green", "blue")

print(colors)

Output:

('red', 'green', 'blue')

Exercise 1: Create a Tuple

Problem

Create a tuple containing five fruits and print it.

Solution

fruits = (
    "Apple",
    "Banana",
    "Orange",
    "Mango",
    "Grapes"
)

print(fruits)

Output

('Apple', 'Banana', 'Orange', 'Mango', 'Grapes')

Explanation

A tuple can store multiple values separated by commas inside parentheses.


Exercise 2: Access Tuple Elements

Problem

Print the first and last element of a tuple.

Solution

numbers = (10, 20, 30, 40, 50)

print(numbers[0])
print(numbers[-1])

Output

10
50

Explanation

  • Index 0 returns the first item.
  • Index -1 returns the last item.

Exercise 3: Find Tuple Length

Problem

Find the total number of items in a tuple.

Solution

animals = (
    "Cat",
    "Dog",
    "Bird",
    "Fish"
)

print(len(animals))

Output

4

Exercise 4: Count Occurrences

Problem

Count how many times the number 5 appears in a tuple.

Solution

numbers = (
    1, 5, 2, 5, 3, 5, 4
)

print(numbers.count(5))

Output

3

Explanation

The count() method returns the number of occurrences of a value.


Exercise 5: Find an Item Position

Problem

Find the index of "Python".

Solution

languages = (
    "Java",
    "C++",
    "Python",
    "JavaScript"
)

print(
    languages.index("Python")
)

Output

2

Exercise 6: Slice a Tuple

Problem

Extract the middle three elements.

Solution

numbers = (
    10, 20, 30,
    40, 50, 60,
    70
)

print(numbers[2:5])

Output

(30, 40, 50)

Exercise 7: Join Two Tuples

Problem

Combine two tuples into one.

Solution

tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)

result = tuple1 + tuple2

print(result)

Output

(1, 2, 3, 4, 5, 6)

Exercise 8: Repeat a Tuple

Problem

Repeat a tuple three times.

Solution

letters = ("A", "B")

print(letters * 3)

Output

('A', 'B', 'A', 'B', 'A', 'B')

Exercise 9: Check if Item Exists

Problem

Check whether "Mango" exists in a tuple.

Solution

fruits = (
    "Apple",
    "Banana",
    "Mango"
)

if "Mango" in fruits:
    print("Found")
else:
    print("Not Found")

Output

Found

Exercise 10: Loop Through a Tuple

Problem

Print every item in a tuple.

Solution

colors = (
    "Red",
    "Green",
    "Blue"
)

for color in colors:
    print(color)

Output

Red
Green
Blue

Exercise 11: Convert Tuple to List

Problem

Convert a tuple into a list.

Solution

numbers = (
    10, 20, 30
)

result = list(numbers)

print(result)

Output

[10, 20, 30]

Exercise 12: Convert List to Tuple

Problem

Convert a list into a tuple.

Solution

data = [
    "Python",
    "Java",
    "C++"
]

result = tuple(data)

print(result)

Output

('Python', 'Java', 'C++')

Exercise 13: Unpack a Tuple

Problem

Assign tuple values to separate variables.

Solution

student = (
    "John",
    20,
    "A"
)

name, age, grade = student

print(name)
print(age)
print(grade)

Output

John
20
A

Exercise 14: Find Maximum Value

Problem

Find the largest number in a tuple.

Solution

numbers = (
    25, 60, 15,
    90, 40
)

print(max(numbers))

Output

90

Exercise 15: Find Minimum Value

Problem

Find the smallest number in a tuple.

Solution

numbers = (
    25, 60, 15,
    90, 40
)

print(min(numbers))

Output

15

Exercise 16: Calculate Sum

Problem

Calculate the sum of all values.

Solution

numbers = (
    10, 20, 30,
    40, 50
)

print(sum(numbers))

Output

150

Exercise 17: Reverse a Tuple

Problem

Reverse a tuple using slicing.

Solution

numbers = (
    1, 2, 3, 4, 5
)

reversed_tuple = numbers[::-1]

print(reversed_tuple)

Output

(5, 4, 3, 2, 1)

Exercise 18: Sort Tuple Values

Problem

Sort tuple values in ascending order.

Solution

numbers = (
    50, 10, 30,
    20, 40
)

sorted_tuple = tuple(
    sorted(numbers)
)

print(sorted_tuple)

Output

(10, 20, 30, 40, 50)

Exercise 19: Nested Tuple Access

Problem

Access the value 100 from a nested tuple.

Solution

data = (
    1,
    2,
    (50, 100, 150)
)

print(data[2][1])

Output

100

Exercise 20: Tuple Challenge

Problem

Store student information and display a formatted message.

Solution

student = (
    "Alice",
    21,
    "Computer Science"
)

print(
    f"Name: {student[0]}"
)
print(
    f"Age: {student[1]}"
)
print(
    f"Course: {student[2]}"
)

Output

Name: Alice
Age: 21
Course: Computer Science

Bonus Challenge

Create a tuple containing 10 numbers and:

  • Find the largest value
  • Find the smallest value
  • Calculate the sum
  • Calculate the average

Solution

numbers = (
    5, 10, 15, 20, 25,
    30, 35, 40, 45, 50
)

largest = max(numbers)
smallest = min(numbers)
total = sum(numbers)
average = total / len(numbers)

print("Largest:", largest)
print("Smallest:", smallest)
print("Sum:", total)
print("Average:", average)

Summary

In this tutorial, you learned how to:

  • Create tuples
  • Access tuple items
  • Slice tuples
  • Join tuples
  • Repeat tuples
  • Loop through tuples
  • Convert between lists and tuples
  • Unpack tuples
  • Find maximum and minimum values
  • Calculate sums and averages
  • Work with nested tuples

These exercises provide a solid foundation for mastering Python tuples and preparing for more advanced Python programming concepts. 




Post a Comment

0 Comments