Code
# We won't use the random library until the end of this exercise,
# but it's always good to put imported libraries at the top of your notebook.
import random
Python Data Structures Practice
A cartoon panda is buried under a pile of dictionaries (Midjourney5)[https://www.midjourney.com/jobs/ea8d00de-44ce-446b-b74b-1b2dda56b0f3?index=0]
Welcome to the end-of-day exercise for Day 2! Today, weโll be putting into practice what youโve learned about Python data structures, particularly lists and dictionaries. This exercise allows you to work with real data from your classmates while reinforcing key concepts.
By the end of this exercise, you should be able to:
First, letโs import the necessary libraries:
In this first part, weโll create data structures based on information from your classmates.
Create a list containing the names of at least 4 of your classmates in this course.
Now, letโs create a dictionary where the keys are your classmatesโ names, and the values are another dictionary containing information about them. For each classmate, include the following information:
favorite_color
)number_of_pets
)preferred_study_snack
)Now that we have our data structures, letโs practice manipulating them.
Using the list of classmate names you created in Task 1, perform the following operations:
Using the dictionary of classmate information from Task 2, perform the following operations:
Letโs add a fun element to our exercise using the random
module. Before we dive into the main task, letโs look at how we can use the random library to select random items from a dictionary.
Hereโs a simple example of how to select random items from a dictionary:
import random
# Sample dictionary
fruit_colors = {
"apple": "red",
"banana": "yellow",
"grape": "purple",
"kiwi": "brown",
"orange": "orange"
}
# Select a single random key-value pair
random_fruit, random_color = random.choice(list(fruit_colors.items()))
print(f"Randomly selected fruit: {random_fruit}")
print(f"Its color: {random_color}")
# To get just a random key:
random_fruit = random.choice(list(fruit_colors.keys()))
print(f"Another randomly selected fruit: {random_fruit}")
# To select multiple random items:
num_selections = 3
random_fruits = random.sample(list(fruit_colors.keys()), num_selections)
print(f"Randomly selected {num_selections} fruits: {random_fruits}")
Randomly selected fruit: kiwi
Its color: brown
Another randomly selected fruit: banana
Randomly selected 3 fruits: ['kiwi', 'apple', 'banana']
This example demonstrates how to:
random.choice()
to select a single random item from a listrandom.sample()
to select multiple unique random items from a listNote: random.choice()
selects a single item, while random.sample()
can select multiple unique items. For our snack-sharing task below, random.sample()
might be more useful!
Create a function that randomly selects a classmate to share their snack with another random classmate. Print out the results as โName will share [snack] with Nameโ.
Great job completing this exercise! Youโve practiced creating and manipulating lists and dictionaries, performed basic data analysis, and even created a fun random snack-sharing function. These skills will be invaluable as you continue your journey in Python programming and data science.
Remember, the key to mastering these concepts is practice. Feel free to modify this exercise with your own data or ideas to further reinforce your learning.
Donโt forget to check out our course cheatsheets for quick reference on lists and dictionaries!