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:
- Create a list `fruits = ["apple", "banana", "cherry"]`.
- Print the second item.
- Change "apple" to "kiwi".
- 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:
- You have `fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]`.
- 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):
- Create a tuple `fruits = ("apple", "banana", "cherry")`.
- Print the first item.
- 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):
- Create a set `fruits = {"apple", "banana", "cherry"}`.
- Add "orange" to the set (`add`).
- 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:
- Create a dictionary `car = {"brand": "Ford", "model": "Mustang", "year": 1964}`.
- Print the value of "model".
- Change "year" to 2020.
- 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:
- Use a `for` loop to print all keys and values in the `car` dictionary.
- Use `car.items()`.
Solution
for key, value in car.items():
print(key, value)
Exercise 7
Nested Dictionaries
Create a complex structure:
- Create a dictionary `myfamily`.
- It contains two keys: "child1" and "child2".
- Each child is a dictionary with "name" and "year".
- 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:
- You have `fruits = ["apple", "banana", "cherry"]`.
- Remove "banana" using `remove`.
- 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:
- You have `fruits = ["apple", "banana", "cherry", "kiwi", "mango"]`.
- Create a new list `newlist` that includes only fruits containing "a".
- 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:
- You have `thislist = ["apple", "banana"]`.
- Create a copy `mylist` using `copy()`.
- Make sure changes to the copy do not affect the original.
Solution
thislist = ["apple", "banana"]
mylist = thislist.copy()
print(mylist)