Python Exam 3: Lists, Tuples, Sets, and Dictionaries (10 mixed exercises)

Python Exam 3: Data Structures

Test your skills organizing data with lists, sets, dictionaries, and tuples.

Exercise 1 Lists

Work with a list:

  1. Create a list `fruits = ["apple", "banana", "cherry"]`.
  2. Print the second item.
  3. Change "apple" to "kiwi".
  4. Add "orange" to the end of the list (`append`).
Solution
fruits = ["apple", "banana", "cherry"]
print(fruits[1])      # banana
fruits[0] = "kiwi"
fruits.append("orange")
print(fruits)
Exercise 2 List Slicing

Extract part of a list:

  1. You have `fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]`.
  2. Print items from the third to the fifth (index starts at 0).
Solution
fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(fruits[2:5]) # ['cherry', 'orange', 'kiwi']
Exercise 3 Tuples

Work with tuples (immutable):

  1. Create a tuple `fruits = ("apple", "banana", "cherry")`.
  2. Print the first item.
  3. Try to change the first item and explain the result in a comment.
Solution
fruits = ("apple", "banana", "cherry")
print(fruits[0])

# fruits[0] = "kiwi"
# This will raise a TypeError because tuples are immutable.
Exercise 4 Sets

Work with sets (unordered, no duplicates):

  1. Create a set `fruits = {"apple", "banana", "cherry"}`.
  2. Add "orange" to the set (`add`).
  3. Add "apple" again and print the set to see no duplicates.
Solution
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
fruits.add("apple") # will not be added because it already exists
print(fruits)
Exercise 5 Dictionaries

Store key-value data:

  1. Create a dictionary `car = {"brand": "Ford", "model": "Mustang", "year": 1964}`.
  2. Print the value of "model".
  3. Change "year" to 2020.
  4. Add a "color" key with value "red".
Solution
car = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(car.get("model"))
car["year"] = 2020
car["color"] = "red"
print(car)
Exercise 6 Looping Dictionaries

Print dictionary contents:

  1. Use a `for` loop to print all keys and values in the `car` dictionary.
  2. Use `car.items()`.
Solution
for key, value in car.items():
    print(key, value)
Exercise 7 Nested Dictionaries

Create a complex structure:

  1. Create a dictionary `myfamily`.
  2. It contains two keys: "child1" and "child2".
  3. Each child is a dictionary with "name" and "year".
  4. Print the name of the second child.
Solution
myfamily = {
  "child1" : {
    "name" : "Emil",
    "year" : 2004
  },
  "child2" : {
    "name" : "Tobias",
    "year" : 2007
  }
}
print(myfamily["child2"]["name"])
Exercise 8 Removing Items

Remove data from a list:

  1. You have `fruits = ["apple", "banana", "cherry"]`.
  2. Remove "banana" using `remove`.
  3. Remove the last item using `pop`.
Solution
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
fruits.pop()
print(fruits)
Exercise 9 List Comprehension

Create a list in a compact way:

  1. You have `fruits = ["apple", "banana", "cherry", "kiwi", "mango"]`.
  2. Create a new list `newlist` that includes only fruits containing "a".
  3. Use a list comprehension (one line).
Solution
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
Exercise 10 Copy Lists

Copy a list correctly:

  1. You have `thislist = ["apple", "banana"]`.
  2. Create a copy `mylist` using `copy()`.
  3. Make sure changes to the copy do not affect the original.
Solution
thislist = ["apple", "banana"]
mylist = thislist.copy()
print(mylist)
Smart Editor

Write code and see the result instantly

Try it free