This document contains structured problems to enhance your problem-solving skills using Python's core data structures. Each problem demonstrates common beginner-level mistakes related to Lists, Tuples, Dictionaries, Sets, and Mixed Types.
You're trying to access an index that doesn’t exist and modify a list item.
fruits = ["apple", "banana", "orange"]
fruits.append("grape")
fruits[3] = "melon"
print(fruits[4])- How
append()affects indexing - What happens when accessing out-of-range indexes
- Proper way to replace an item
Trying to modify a tuple, which is not allowed.
student_info = ("John", 16, "Grade 10")
student_info[1] = 17
print("Updated Age:", student_info[1])- Tuples are immutable
- How to work with tuples if you need changes (hint: convert to list)
Accessing a non-existent key and misusing append() on a dictionary.
grades = {"Alice": 85, "Bob": 92}
print("Charlie’s grade is", grades["Charlie"])
grades["Alice"] = 88
grades.append("Eve": 90)- How to handle missing keys safely (
get()orin) - Adding new entries properly (
grades["Eve"] = 90)
Trying to access sets using index and adding unhashable elements.
colors = {"red", "blue", "green"}
colors[1] = "yellow"
colors.add(["black", "white"])
print(colors)- Sets do not support indexing
- Only hashable (immutable) types can be added (e.g., strings, tuples)
Applying a list method to a set and accessing keys that don’t exist.
students = ["Alice", "Bob", "Charlie"]
ages = {"Alice": 15, "Bob": 16}
unique_names = set(students)
unique_names.add("Alice")
students.add("Daniel")
print(ages["Charlie"]).add()doesn't work on lists, use.append()KeyErrorwhen accessing missing dictionary keys
📚 Next Steps: Try fixing each bug and re-run the corrected code. Make sure you understand:
- Data type behavior
- Safe data access
- Proper method usage
fruits = ["apple", "banana", "orange"]
fruits.append("grape")
fruits[3] = "melon"
print(fruits[4])fruits[3] = "melon"is fine as it replaces "grape".- But
print(fruits[4])throwsIndexErrorsince there's no 5th element.
fruits = ["apple", "banana", "orange"]
fruits.append("grape")
fruits[3] = "melon"
print(fruits)student_info = ("John", 16, "Grade 10")
student_info[1] = 17
print("Updated Age:", student_info[1])- Tuples are immutable. You can't change
student_info[1].
student_info = ("John", 17, "Grade 10")
print("Updated Age:", student_info[1])grades = {"Alice": 85, "Bob": 92}
print("Charlie’s grade is", grades["Charlie"])
grades["Alice"] = 88
grades.append("Eve": 90)grades["Charlie"]raisesKeyError..append()doesn't work with dictionaries.
grades = {"Alice": 85, "Bob": 92}
print("Charlie’s grade is", grades.get("Charlie", "Not Found"))
grades["Alice"] = 88
grades["Eve"] = 90colors = {"red", "blue", "green"}
colors[1] = "yellow"
colors.add(["black", "white"])
print(colors)- Sets don't support indexing like
colors[1]. .add()can only take hashable (immutable) items, not a list.
colors = {"red", "blue", "green"}
colors.add("yellow")
colors.update(["black", "white"])
print(colors)students = ["Alice", "Bob", "Charlie"]
ages = {"Alice": 15, "Bob": 16}
unique_names = set(students)
unique_names.add("Alice")
students.add("Daniel")
print(ages["Charlie"])studentsis a list; you can't use.add(), use.append()instead.ages["Charlie"]causesKeyError.
students = ["Alice", "Bob", "Charlie"]
ages = {"Alice": 15, "Bob": 16}
unique_names = set(students)
unique_names.add("Alice")
students.append("Daniel")
print(ages.get("Charlie", "Not Found"))