Python Tuples: Excersises

Tuples in Python are immutable sequences, typically used to store collections of heterogeneous data. This article presents 20 exercises that will help you strengthen your understanding of tuples and practice various operations with them. At the end of the article, you’ll find the answers to all exercises.

Exercises

  1. Create a tuple with the values 5, 10, 15, 20, 25 and print the tuple.

    t = (5, 10, 15, 20, 25)
    print(t)  # Output: (5, 10, 15, 20, 25)
  2. Given the tuple t = ("1984", "The Hobbit", "Moby Dick"), access the second item.

    t = ("1984", "The Hobbit", "Moby Dick")
    print(t[1])  # Output: The Hobbit
  3. Use negative indexing to access the last item of the tuple t = ("fiction", "non-fiction", "fantasy").

    t = ("fiction", "non-fiction", "fantasy")
    print(t[-1])  # Output: fantasy
  4. Unpack the tuple t = (100, 200, 300) into three variables and print them.

    t = (100, 200, 300)
    a, b, c = t
    print(a, b, c)  # Output: 100 200 300
  5. Create a tuple with a single item, "Python", and print the tuple.

    t = ("Python",)
    print(t)  # Output: ('Python',)
  6. Use slicing to obtain the first three items from the tuple t = ("book1", "book2", "book3", "book4", "book5").

    t = ("book1", "book2", "book3", "book4", "book5")
    print(t[:3])  # Output: ('book1', 'book2', 'book3')
  7. Check if the item "Dune" is in the tuple t = ("1984", "Brave New World", "Fahrenheit 451").

    t = ("1984", "Brave New World", "Fahrenheit 451")
    print("Dune" in t)  # Output: False
  8. Combine two tuples, t1 = (2, 4, 6) and t2 = (8, 10, 12), into a single tuple.

    t1 = (2, 4, 6)
    t2 = (8, 10, 12)
    t3 = t1 + t2
    print(t3)  # Output: (2, 4, 6, 8, 10, 12)
  9. Find the length of the tuple t = ("a", "b", "c", "d").

    t = ("a", "b", "c", "d")
    print(len(t))  # Output: 4
  10. Multiply the tuple t = ("book",) by 3 and print the result.

    t = ("book",)
    print(t * 3)  # Output: ('book', 'book', 'book')
  11. Convert the tuple t = (40, 50, 60) into a list.

    t = (40, 50, 60)
    l = list(t)
    print(l)  # Output: [40, 50, 60]
  12. Use the count() method to find how many times "The Hobbit" appears in the tuple t = ("The Hobbit", "1984", "The Hobbit", "Dune").

    count() method documentation

    t = ("The Hobbit", "1984", "The Hobbit", "Dune")
    print(t.count("The Hobbit"))  # Output: 2
  13. Find the index of "non-fiction" in the tuple t = ("fiction", "non-fiction", "fantasy").

    index() method documentation

    t = ("fiction", "non-fiction", "fantasy")
    print(t.index("non-fiction"))  # Output: 1
  14. Convert the list l = [5, 10, 15, 20] into a tuple.

    l = [5, 10, 15, 20]
    t = tuple(l)
    print(t)  # Output: (5, 10, 15, 20)
  15. Create a tuple t = ("red", "green", "blue") and iterate over its elements using a for loop.

    t = ("red", "green", "blue")
    for color in t:
        print(color)  
    # Output: 
    # red
    # green
    # blue
  16. Python doesn’t support tuple comprehensions, but you can achieve this using a generator and converting it to a tuple:

    t = tuple(x**2 for x in range(2, 7))
    print(t)  # Output: (4, 9, 16, 25, 36)
  17. Create a nested tuple with the following structure: ((2, 4, 6), ("x", "y", "z")) and access the second item in the first tuple.

    t = ((2, 4, 6), ("x", "y", "z"))
    print(t[0][1])  # Output: 4
  18. Write a Python function that accepts a tuple and returns a new tuple with all the elements squared.

    def square_tuple(t):
        return tuple(x**2 for x in t)
    
    print(square_tuple((2, 3, 4)))  # Output: (4, 9, 16)
  19. Given a tuple t = (200, 300, 400, 500, 600), remove the first item and return the remaining tuple.

    t = (200, 300, 400, 500, 600)
    t = t[1:]
    print(t)  # Output: (300, 400, 500, 600)
  20. Write a Python script that concatenates all the elements of a tuple into a single string.

    t = ("Hello", "from", "Python", "with", "love")
    result = " ".join(t)
    print(result)  # Output: Hello from Python with love

Answers

  1. t = (5, 10, 15, 20, 25)
    print(t) # Output: (5, 10, 15, 20, 25)

  2. t = ("1984", "The Hobbit", "Moby Dick")
    print(t[1]) # Output: The Hobbit

  3. t = ("fiction", "non-fiction", "fantasy")
    print(t[-1]) # Output: fantasy

  4. t = (100, 200, 300)
    a, b, c = t
    print(a, b, c) # Output: 100 200 300

  5. t = ("Python",)
    print(t) # Output: ('Python',)

  6. t = ("book1", "book2", "book3", "book4", "book5")
    print(t[:3]) # Output: ('book1', 'book2', 'book3')

  7. t = ("1984", "Brave New World", "Fahrenheit 451")
    print("Dune" in t) # Output: False

  8. t1 = (2, 4, 6)
    t2 = (8, 10, 12)
    t3 = t1 + t2
    print(t3) # Output: (2, 4, 6, 8, 10, 12)

  9. t = ("a", "b", "c", "d")
    print(len(t)) # Output: 4

  10. t = ("book",)
    print(t * 3) # Output: ('book', 'book', 'book')

  11. t = (40, 50, 60)
    l = list(t)
    print(l) # Output: [40, 50, 60]

  12. t = ("The Hobbit", "1984", "The Hobbit", "Dune")
    print(t.count("The Hobbit")) # Output: 2

  13. t = ("fiction", "non-fiction", "fantasy")
    print(t.index("non-fiction")) # Output: 1

  14. l = [5, 10, 15, 20]
    t = tuple(l)
    print(t) # Output: (5, 10, 15, 20)

  15. t = ("red", "green", "blue")
    for color in t:
        print(color) # Output:
        red
        green
        blue

  16. t = tuple(x**2 for x in range(2, 7))
    print(t) # Output: (4, 9, 16, 25, 36)

  17. t = ((2, 4, 6), ("x", "y", "z"))
    print(t[0][1]) # Output: 4

  18. def square_tuple(t):
        return tuple(x**2 for x in t)

    print(square_tuple((2, 3, 4))) # Output: (4, 9, 16)

  19. t = (200, 300, 400, 500, 600)
    t = t[1:]
    print(t) # Output: (300, 400, 500, 600)

  20. t = ("Hello", "from", "Python", "with", "love")
    result = " ".join(t)
    print(result) # Output: Hello from Python with love

Conclusion

Practicing these exercises will improve your understanding of tuples in Python, which are an essential part of the language. You’ll become more comfortable working with tuples and applying tuple operations to solve problems effectively.

Leave a Comment