September 26: Containers

Learning Objectives

After today's class, you should be able to:

  • Specify which containers are mutable and immutable
  • Write python code to implement and access lists, tuples, sets and dictionaries
  • Use in and not in in boolean expressions
  • Understand the difference between == and is

Lesson Outline

Class Activity: 20 min

Mini Lecture: 25 min

Class Activity 2: 30 min

Activity to practice

Little Free Library

photo of little free library

A Little Free Library is a community library that lets people borrow and contribute books. The library survives on the "take a book, leave a book" principle. If someone takes a book (like "Project Hail Mary"), they have to add another book to the library (like "The Martian"). It’s a great way for people to share books!

In this lab, you will create a library.py program to simulate a Little Free Library. At the top of your program, after the docstring, assign a variable named books that contains the list of books in the library:

books = ["Project Hail Mary", "The Hobbit", "Sherlock Holmes", "The Hobbit", "Dune"]
(Note: If we were to submit this lab, which we won't, then Gradescope would replace the contents of this variable with other books.) The first thing the program should do is display a title, the number of books in the library, and all the books, followed by a blank line:
= Little Free Library =
Count: 5 book(s)
['Project Hail Mary', 'The Hobbit', 'Sherlock Holmes', 'The Hobbit', 'Dune']
The program should then ask the user which book they would like and tell the user how many copies of that book are available:
Enter the title of a book to find: The Hobbit
We have 2 copies of "The Hobbit" available.
Then, the program should ask the user for the index of a book to take, print the name of the book, then remove it from the list and print the updated list, followed by a blank line:
Enter the index of a book to take: 1
You took "The Hobbit".
['Project Hail Mary', 'Sherlock Holmes', 'The Hobbit', 'Dune']
Finally, ask the user for the name of a book they would like to leave, and put the book at the end of the list. Print out the full list of books again to show what the list looks like now.
What book would you like to leave? The Martian
['Project Hail Mary', 'Sherlock Holmes', 'The Hobbit', 'Dune', 'The Martian']

Your To-Do List

Due Wednesday September 27th at 11pm

  • Submit HW 5 through Canvas and check out the Help page. Remember the attribution statement

Prepare for Quiz 2B

  • Thursday, beginning of class, 25 minutes, in EnGeo 2204.
  • Two parts: concepts and programming (will use lab machine account, canvas and gradescope)
  • Study chapters 3 and 4: both reading and activities
  • Review class activities
  • Review homework programming assignments