Tuple Exercises

These exercises are designed to build strong understanding of tuples in Python. They cover creation, access, unpacking, looping, logic, and edge cases.

Exercise 1: Create a Tuple

Create a tuple of five numbers and print it.
python
numbers = (10, 20, 30, 40, 50)
print(numbers)

Exercise 2: Access Tuple Items

Print:
  • First item
  • Last item
  • Third item
python
numbers = (10, 20, 30, 40, 50)

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

Exercise 3: Tuple Length

python
data = ("Python", "Java", "C++")
print(len(data))

Exercise 4: Check If Item Exists

Check if "apple" exists in the tuple.
python
fruits = ("apple", "banana", "cherry")

if "apple" in fruits:
    print("Apple found")

Exercise 5: Loop Through a Tuple

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

for color in colors:
    print(color)

Exercise 6: Loop Using Index

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

for i in range(len(colors)):
    print(i, colors[i])

Exercise 7: Tuple Unpacking

python
data = (100, 200, 300)
a, b, c = data

print(a)
print(b)
print(c)

Exercise 8: Extended Unpacking

python
numbers = (1, 2, 3, 4, 5)
a, *b, c = numbers

print(a)
print(b)
print(c)

Exercise 9: Swap Values Using Tuple

python
x = 10
y = 20

x, y = y, x

print(x)
print(y)

Exercise 10: Count Occurrences

python
values = (1, 2, 2, 3, 2, 4)
print(values.count(2))

Exercise 11: Find Index of Value

python
numbers = (10, 20, 30, 40)
print(numbers.index(30))

Exercise 12: Join Two Tuples

python
a = (1, 2)
b = (3, 4)

result = a + b
print(result)

Exercise 13: Repeat Tuple

python
pattern = ("A", "B")
print(pattern * 3)

Exercise 14: Convert Tuple to List and Back

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

temp = list(colors)
temp.append("yellow")
colors = tuple(temp)

print(colors)

Exercise 15: Nested Tuple Access

python
matrix = (
    (1, 2, 3),
    (4, 5, 6),
    (7, 8, 9)
)

print(matrix[1][1])

Exercise 16: Modify Nested Tuple Item

python
data = (1, [2, 3], 4)
data[1].append(99)

print(data)

Exercise 17: Tuple Slicing

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

print(numbers[1:4])
print(numbers[:3])
print(numbers[::-1])

Exercise 18: Single-Item Tuple Check

python
single = (10,)
print(type(single))

Exercise 19: Tuple from String

python
text = "Python"
t = tuple(text)
print(t)

Exercise 20: Predict the Output (Tricky)

python
a = (1, 2)
b = a
b = b + (3,)

print(a)
print(b)

Key Learning Outcomes

  • Tuple creation and access
  • Immutability behavior
  • Tuple unpacking techniques
  • Looping through tuples
  • Joining and repeating tuples
  • Nested tuple handling
  • Common tuple pitfalls
These exercises give a solid and interview-ready understanding of Python tuples.