2nd Semester Intro to Computer Science

What you will learn

Reading

Do Now

1. In your Console

Type the following

weekend_dates = {
  'April 2018': [7, 8, 14, 15, 21, 22, 28, 29],
  'May 2018': [5, 6, 12, 13, 19, 20, 26, 27]
}

print(weekend_dates['April 2018'])

In your Notebook

Respond to the following

2. In your Console

Challenge yourself with the following

Lab

In this lab your job is to create a week-long to-do list using a Python dictionary. Each key in the dictionary is a day of the week. Each associated value is a list of items to do that day.

The program repeatedly asks the user what action they wish to take ( add or get).

Example

Here’s an example. The program output is shown in bold text, the user input in regular text.

>>>python3 daily_to_do_list.py
What would you like to do ('add' or 'get')?
add
What day?
Friday
What would you like to add to Fridays to-do list?
practice clarinet
What would you like to do ('add' or 'get')?
get
What day?
Friday
You have to practice clarinet.
What would you like to do('add' or 'get')?

Bonus

It’s a bit tedious for the user to have to type in three different lines to add an item to a to-do list. Use split() to allow the user to input add Friday watch tv and relax.

Create a variation of the program that doesn’t allow any duplicates across any of the days. Make sure when you add a to-do item that it doesn’t exist in the to-do lists of any of the days before adding.

Check your understanding

Pick an answer for each question. You can try again until you get it right.

1. In scores = {'ann': [90, 85], 'bo': [70]}, what type are the values?

  • Strings
  • Lists
  • Integers
  • Dictionaries

Each key maps to a list, and dictionary values can be any type.

2. Which line adds 100 to Bo’s list of scores?

  • scores['bo'] = 100
  • scores.append(100)
  • scores['bo'].append(100)
  • scores[100] = 'bo'

Looking up scores['bo'] gives the list, and append adds an item to that list.

3. What does scores['bo'] = 100 do to the list that was stored for 'bo'?

  • It replaces the whole list with the number 100
  • It adds 100 to the end of the list
  • It causes an error
  • It removes only the first item

Assigning to a key overwrites the old value entirely, so the list is lost.

Downloads