Practice Exercises: Data Structures
Are you ready to turn your Python knowledge into real skills? Practicing with Python data structures is one of the best ways to get comfortable with…
Are you ready to turn your Python knowledge into real skills? Practicing with Python data structures is one of the best ways to get comfortable with programming—and to prepare for future jobs in tech. This article is packed with beginner-friendly exercises to help you master lists, dictionaries, tuples, and sets. Whether you’re brand new or brushing up, these hands-on challenges will help you learn Python by doing.
Why Practice Python Data Structures?
Data structures are the building blocks of Python programming. Lists, dictionaries, tuples, and sets help you organize, store, and work with data in your programs.
Practicing these concepts is key to remembering and applying what you’ve learned. The more you use them, the more confident you’ll feel—whether you’re working on a personal project or interviewing for a programming job.
Think of each exercise as a step toward becoming a stronger, more capable Python programmer.
How to Use These Exercises
Here’s how to get the most out of these Python beginner exercises:
- Try solving each exercise on your own first. Don’t peek at hints or solutions right away!
- Use any Python editor or an online tool (like Replit or Python Tutor) to write and test your code.
- Don’t worry about mistakes. Every error is a learning opportunity. Keep experimenting and tweaking your code until it works.
Ready? Let’s dive into some practical python data structures exercises!
List Practice Exercises
Lists are one of the most flexible and commonly used data structures in Python. They let you store collections of items, like numbers or words.
1. Create and Print a List
- Create a list called
colorswith the items:"red","green", and"blue". - Print the list.
2. Add and Remove Items
- Add
"yellow"to the end of thecolorslist. - Remove
"green"from the list. - Print the updated list.
3. Access and Change Items
- Print the first item in the
colorslist. - Change the last item to
"purple". - Print the list again.
4. Sort and Find Items
- Create a list of numbers:
[5, 2, 9, 1, 7]. - Sort the list in ascending order and print it.
- Check if the number
3is in the list, and print the result (TrueorFalse).
5. Experiment with List Methods
- Try using at least two different list methods (like
.append(),.pop(),.insert(), or.reverse()) on any list you create. - Print the results to see how each method works.
Quiz Question 1
Question: What does the .append() method do for lists in Python?
- A) It removes the last item from the list
- B) It adds a new item to the end of the list
- C) It sorts the list in place
- D) It checks if an item is in the list
Dictionary Practice Exercises
Dictionaries store data in key-value pairs. They’re perfect for looking up information quickly.
1. Create and Print a Dictionary
- Create a dictionary called
personwith keys:"name"(your name),"age"(your age), and"city"(your city). - Print the dictionary.
2. Update and Add Items
- Change the
"city"value to a different city. - Add a new key-value pair:
"job"with any job title you like. - Print the updated dictionary.
3. Look Up and Remove Items
- Print the value for the
"name"key. - Remove the
"age"key from the dictionary. - Print the dictionary again.
4. Use Dictionary Methods
- Use the
.keys()method to print all the keys in the dictionary. - Use the
.values()method to print all the values.
Quiz Question 2
Question: How do you add a new key-value pair to a dictionary called my_dict?
- A)
my_dict.add('key', 'value') - B)
my_dict['key'] = 'value' - C)
my_dict.append('key', 'value') - D)
my_dict.insert('key', 'value')
Tuples and Sets Practice Exercises
Tuples and sets are two more useful Python data structures. Tuples are like lists, but you can’t change them after they’re created. Sets are collections of unique items.
1. Create and Use a Tuple
- Create a tuple called
dimensionswith the values(10, 20, 30). - Print the second item in the tuple.
2. When to Use Tuples
- Write a short comment in your code explaining why you might use a tuple instead of a list.
3. Create a Set and Remove Duplicates
- Create a list with some repeated numbers:
[1, 2, 2, 3, 4, 4, 5]. - Convert the list to a set to remove duplicates.
- Print the set.
4. Set Operations
- Create two sets:
a = {1, 2, 3}andb = {3, 4, 5}. - Print the union of the sets (all unique items from both).
- Print the intersection (items both sets share).
Quiz Question 3
Question: Which Python data structure would you use if you want to store a collection of items that must all be unique?
- A) List
- B) Dictionary
- C) Set
- D) Tuple
Quiz Question 4
Question: What happens if you try to change an item in a tuple after it has been created?
- A) You get an error because tuples cannot be changed
- B) The item is updated successfully
- C) A new tuple is automatically created
- D) The item is removed from the tuple
Quick Review Quiz
Test your understanding with these short questions. Write your answers on paper or in your code editor.
Quiz Question 5
Question: How can you check if an item (for example, 5) is in a set called my_set?
- A)
my_set.has(5) - B)
5 in my_set - C)
my_set.contains(5) - D)
my_set.find(5)
Quiz Question 6
Question: Which data structure would you use to store the days of the week in order, where the order matters and you might want to change the values later?
- A) Tuple
- B) Set
- C) List
- D) Dictionary
Next Steps After Practicing
Great job working through these python data structures exercises! Here’s how you can keep building your skills:
- Try small projects. For example, make a contact book using dictionaries or a shopping list with lists.
- Explore more advanced Python topics. Learn about loops, functions, and file handling.
- Join coding communities. Sites like Stack Overflow, Reddit, or local groups can offer support, feedback, and new challenges.
Remember, the best way to learn Python is by doing. Keep practicing, stay curious, and celebrate your progress!
Ready for more? Check out our next article or explore more beginner exercises at learnpyfast.com. Your programming journey is just getting started!
Quiz Answer Key
Question 1
Correct answer: B) It adds a new item to the end of the list
Explanation: The .append() method adds a new item to the end of a list.
Question 2
Correct answer: B) my_dict['key'] = 'value'
Explanation: You add a new key-value pair to a dictionary by assigning a value to a new key using square brackets.
Question 3
Correct answer: C) Set
Explanation: Sets automatically remove duplicate items and only store unique values.
Question 4
Correct answer: A) You get an error because tuples cannot be changed
Explanation: Tuples are immutable, meaning their items cannot be changed after creation.
Question 5
Correct answer: B) 5 in my_set
Explanation: The in keyword checks if an item exists in a set.
Question 6
Correct answer: C) List
Explanation: Lists are ordered and mutable, making them perfect for storing items where order matters and changes may be needed.