Python Exercise: Lists, Sets, and Tuples (Beginner Friendly)

Arzath AreeffArzath Areeff
5 min read

List Exercises:

  1. Create and Access List

    • Create a list called fruits containing "apple", "banana", "cherry", and "date".

    • Print the second item in the list.

    • Change the third item to "blueberry" and print the updated list.

  2. List Operations

    • Given the list numbers = [1, 2, 3, 4, 5]:

      • Add the number 6 to the end of the list

      • Insert the number 0 at the beginning of the list

      • Remove the number 3 from the list

      • Print the final list

  3. List Slicing

    • Given letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']:

      • Create a new list containing elements from index 2 to 5

      • Create another list containing every alternate element starting from index 0

      • Print both new lists

  4. List Methods

    • Create a list colors = ["red", "green", "blue"]

    • Use list methods to:

      • Add "yellow" to the list

      • Remove "green" from the list

      • Reverse the order of the list

      • Print the final list

  5. List Length and Checking

    • Create a list pets = ["dog", "cat", "fish", "bird"]

    • Print the number of items in the list

    • Check if "hamster" is in the list and print the result

    • Check if "cat" is in the list and print the result


Set Exercises:

  1. Create and Access Set

    • Create a set called animals containing "dog", "cat", "rabbit", and "hamster".

    • Add "parrot" to the set.

    • Remove "rabbit" from the set.

    • Print the final set.

  2. Set Operations

    • Given two sets:

        set1 = {1, 2, 3, 4, 5}
        set2 = {4, 5, 6, 7, 8}
      
      • Find and print the union of both sets

      • Find and print the intersection of both sets

      • Find and print the difference between set1 and set2

  3. Set Methods

    • Create a set prime_numbers = {2, 3, 5, 7}

    • Use set methods to:

      • Add 11 to the set

      • Remove 3 from the set

      • Check if 5 is in the set

      • Print the final set and the result of the membership check

  4. Frozen Set

    • Create a regular set colors = {"red", "green", "blue"}

    • Convert it to a frozen set

    • Try to add "yellow" to the frozen set (catch the error if it occurs)

    • Print the frozen set

  5. Set Length and Clearing

    • Create a set vowels = {'a', 'e', 'i', 'o', 'u'}

    • Print the number of elements in the set

    • Clear all elements from the set

    • Print the empty set


Tuple Exercises:

  1. Create and Access Tuple

    • Create a tuple called days containing "Monday", "Tuesday", "Wednesday", "Thursday", "Friday".

    • Print the third item in the tuple.

    • Try to change the second item to "Sunnyday" (note what happens)

  2. Tuple Unpacking

    • Create a tuple student = ("John Doe", 25, "Computer Science")

    • Unpack the tuple into three variables: name, age, and major

    • Print each variable separately

  3. Tuple Concatenation

    • Given two tuples:

        tuple1 = (1, 2, 3)
        tuple2 = (4, 5, 6)
      
      • Create a new tuple that combines both tuples

      • Create another tuple that repeats tuple1 three times

      • Print both new tuples

  4. Tuple Methods

    • Create a tuple numbers = (5, 2, 8, 2, 7, 2, 9, 2)

    • Use tuple methods to:

      • Count how many times 2 appears in the tuple

      • Find the index of the first occurrence of 7

      • Print both results

  5. Tuple Conversion

    • Create a list cities = ["Paris", "London", "Tokyo"]

    • Convert the list to a tuple

    • Try to append "New York" to the tuple (note what happens)

    • Print the tuple


Answers:

List Answers:

  1.  fruits = ["apple", "banana", "cherry", "date"]
     print(fruits[1])  # banana
     fruits[2] = "blueberry"
     print(fruits)  # ['apple', 'banana', 'blueberry', 'date']
    
  2.  numbers = [1, 2, 3, 4, 5]
     numbers.append(6)
     numbers.insert(0, 0)
     numbers.remove(3)
     print(numbers)  # [0, 1, 2, 4, 5, 6]
    
  3.  letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
     slice1 = letters[2:6]  # ['c', 'd', 'e', 'f']
     slice2 = letters[::2]  # ['a', 'c', 'e', 'g']
     print(slice1, slice2)
    
  4.  colors = ["red", "green", "blue"]
     colors.append("yellow")
     colors.remove("green")
     colors.reverse()
     print(colors)  # ['blue', 'red', 'yellow']
    
  5.  pets = ["dog", "cat", "fish", "bird"]
     print(len(pets))  # 4
     print("hamster" in pets)  # False
     print("cat" in pets)  # True
    

Set Answers:

  1.  animals = {"dog", "cat", "rabbit", "hamster"}
     animals.add("parrot")
     animals.remove("rabbit")
     print(animals)  # {'dog', 'cat', 'hamster', 'parrot'}
    
  2.  set1 = {1, 2, 3, 4, 5}
     set2 = {4, 5, 6, 7, 8}
     print(set1.union(set2))  # {1, 2, 3, 4, 5, 6, 7, 8}
     print(set1.intersection(set2))  # {4, 5}
     print(set1.difference(set2))  # {1, 2, 3}
    
  3.  prime_numbers = {2, 3, 5, 7}
     prime_numbers.add(11)
     prime_numbers.remove(3)
     check = 5 in prime_numbers
     print(prime_numbers, check)  # {2, 5, 7, 11} True
    
  4.  colors = {"red", "green", "blue"}
     frozen_colors = frozenset(colors)
     try:
         frozen_colors.add("yellow")  # This will raise an AttributeError
     except AttributeError:
         print("Cannot modify a frozen set")
     print(frozen_colors)  # frozenset({'red', 'green', 'blue'})
    
  5.  vowels = {'a', 'e', 'i', 'o', 'u'}
     print(len(vowels))  # 5
     vowels.clear()
     print(vowels)  # set()
    

Tuple Answers:

  1.  days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
     print(days[2])  # Wednesday
     # days[1] = "Sunnyday"  # This will raise a TypeError
    
  2.  student = ("John Doe", 25, "Computer Science")
     name, age, major = student
     print(name)  # John Doe
     print(age)  # 25
     print(major)  # Computer Science
    
  3.  tuple1 = (1, 2, 3)
     tuple2 = (4, 5, 6)
     combined = tuple1 + tuple2  # (1, 2, 3, 4, 5, 6)
     repeated = tuple1 * 3  # (1, 2, 3, 1, 2, 3, 1, 2, 3)
     print(combined, repeated)
    
  4.  numbers = (5, 2, 8, 2, 7, 2, 9, 2)
     print(numbers.count(2))  # 4
     print(numbers.index(7))  # 4
    
  5.  cities = ["Paris", "London", "Tokyo"]
     cities_tuple = tuple(cities)
     # cities_tuple.append("New York")  # This will raise an AttributeError
     print(cities_tuple)  # ('Paris', 'London', 'Tokyo')
    
0
Subscribe to my newsletter

Read articles from Arzath Areeff directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arzath Areeff
Arzath Areeff

I co-founded digizen.lk to promote online safety and critical thinking. Currently, I’m developing an AI app to fight misinformation. As Founder and CEO of ideaGeek.net, I help turn startup dreams into reality, and I share tech insights and travel stories on my YouTube channels, TechNomad and Rz Omar.