CS170: Programming for the World Around Us - Lists with Python

Activity Goals

The goals of this activity are:
  1. To design and implement algorithms using lists
  2. To be able to explain that a list is an ordered collection of data
  3. To be able to create a list and assign its elements
  4. To be able to access an element from a list by its index
  5. To explain that strings are lists of characters, and to manipulate them accordingly
  6. To be able to iterate over a list
  7. To create an manipulate multidimensional lists

Supplemental Reading

Feel free to visit these resources for supplemental background reading material.

The Activity

Directions

Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.

Model 1: Lists in Python

Questions

  1. Comment each line of this program. What do you think it does? Try running it.
  2. What is the size of the grades list?
  3. What are the indices of the grades list?
  4. How would you modify the program above to play a game of "Duck-Duck-Goose" -- that is, iterating through the array until a certain value is reached (say, 92), and printing that index where it is found?

Model 2: Comparing Lists in Python

Questions

  1. Write a loop to check if these two lists are the same (that is, have the same size and contain the same values).
  2. What do you think del list1[5] does? Try it and then see if your lists are the same!
  3. Comment out the del line, and try list2.insert(4, 6) instead.
  4. What does list1.extend(list2) do? Print it out and see.

Model 3: 2D Lists in Python

Questions

  1. Write a program to print your daily schedule.
  2. Modify this program to add a third dimension to the list representing each day of the week.

Model 4: Strings in Python

Questions

  1. What does this program do when you run it?
  2. What are strings in Python?

Submission

I encourage you to submit your answers to the questions (and ask your own questions!) using the Class Activity Questions discussion board. You may also respond to questions or comments made by others, or ask follow-up questions there. Answer any reflective prompt questions in the Reflective Journal section of your OneNote Classroom personal section. You can find the link to the class notebook on the syllabus.