Micro Lab: Lists #
In this Micro Lab, we’ll explore lists, a useful data structure.
[0] Set up #
π» Start by making a new folder in your
cs9 folder.
cd Desktop/cs9
mkdir unit_01
cd unit_01
π» Then, clone your starter code.
Be sure to change YOUR-GITHUB-USERNAME to your actual Github username.
git clone https://github.com/the-isf-academy/lab-micro-lists-YOUR-GITHUB-USERNAME.git
[1] Lists #
What is a list? We call a list a data structure because they give structure to data. They put things in an ordered grouping, so you can access each element at an index.
π» Begin by openning the file
lists_intro.py. Change the value of the variables my_name and friend_name to anything you want. Something like:
#VARIABLES
my_name = "Wall-E"
friend_name = "Eva"
The program currently prints out “help! I am trapped inside the computer!”.
π» Replace that string with the following objects one at a time. For each one, save your program and run it to see what the output.
cs1_dessert_listcs1_dessert_list[0]cs1_dessert_list[3]cs1_dessert_list[-1]len(cs1_dessert_list)cs1_dessert_list[100]my_name + " likes to eat " + cs1_dessert_list[0] + " but " + friend_name + " likes to eat " + cs1_dessert_list[1]
[Looping through a list] #
Paste the code block below into the ‘Part B’ section of your list_intro.py file.
print("Ms. Brown and Ms. Genzlinger go to the store for dessert. They decide to buy...")
for thing in cs1_dessert_list:
print(thing)
In the loop above, thing is a variable that we use to store each element within cs1_desserts as we loop through one by one. thing is not a very descriptive variable name - there are lots of things in the world. How will we know what thing the variable is storing?
π»
Try changing thing to a more descriptive word in both lines, like sweet or dessert_item and see if the output changes.
[Looping Through Part of a List] #
To give you more options, you can specify a range for your for-loop.
Paste the code block below into the ‘Part C’ section of your list_intro.py file.
print("But then they realized they ran out of money, so they can only buy 4 desserts. They decide to buy...")
for i in range(1, 5):
print(cs1_dessert_list[i])
Oops! This code has an error!
The first four desserts in the cs1_dessert_list list are ice cream, brownies, mochi, and timtams. But our code prints out brownies, mochi, timtams, and creme brulee.
π» Change the code to print out the first 4 desserts only.
[2] Lists: To-do Program #
Whenever we use lists everytime we make a written to-do list. So let’s digitize it and create a terminal version!
Let’s start with the most basic version of a to-do list. Here is the pseudocode:
create an empty list
infinite loop
print the current list
ask what the user would like to add to the list
add their item to the list
π» It's up to you to translate the pseudocode into Python code and create a working to-do program.
Your code will go in lists_todo.py.
πΎ π¬ Adding to a list
Let’s create a list of pizza toppings:
pizza_toppings = ['cheese','pineapple']To add something to this list:
pizza_toppings.append('ham')The list now looks like:
pizza_toppings = ['cheese','pineapple','ham']
[Advanced To-do] #
Let’s expand our app to allow us to remove items from the to-do list.
Here is the more pseudocode for the most advanced to-do list:
create an empty list
infinite loop
print the current list
if the list has zero items in it
ask what the user would like to add to the list
add their item to the list
else
ask the user if they would like to add to the list or remove from the list
if the user wants to add to the list
ask what the user would like to add to the list
add their item to the list
if the user wants to remove from the list
ask what the user would like to remove from the list
remove the item from the list
π» Expand the functionality of your to-do program.
πΎ π¬ Advanced lists
Consider our
pizza_toppingslist:pizza_toppings = ['cheese','pineapple','ham']What if we wanted to remove
'pineapple'from our list?pizza_toppings.remove('pineapple')Now our
pizza_toppingslist looks like:pizza_toppings = ['cheese','ham']What if we wanted to check how many pizza toppings we have?
number_of_toppings = len(pizza_toppings)
number_of_toppingswill now hold the value2.