Lecture 24 Slides - Real-World Programming

1 of 12

Actual Programming w/ Dictionaries

1

CS 110

2 of 12

Reminders and Announcements

Announcements

  • P2 Instructions Available - SIGN UP FOR TEAMS (2 or 3) BY TUESDAY IF YOU WANT
  • Common EoQ questions thread on edSTEM

This Week

  • Friday - The Real World of Programming (MQ14) + HW 7 Due

Next Week

  • Monday - PIP and The Secrets of Computers
  • Wednesday - Project 2 Intro (Pre-Recorded + MQ15) + Tutorial 7: P2 Workshop
  • Friday - Q3 (for those who opt to take it)

Finals Week

  • P2 due (starter files available this weekend)
    • 3/18 at 5pm is the suggested due date (if you turn it in by this deadline you get 2 extra points – capped at 5 total)
    • 3/20 at 5pm is the end-all deadline (no late penalty; no submissions accepted after this point)

3 of 12

Dictionaries (Cheat Sheet)

3

Create a dictionary

my_dict = {}

Get element from a dictionary

my_dict.get(key)

Add/replace items in dictionary

my_dict[key] = value

Remove items from dictionary

del my_dict[key]

Iterate through a dictionary

for key in my_dict:� print(key, my_dict[key])

Check if key exists in dictionary

if my_dict.get(key) != None

if key in my_dict…

4 of 12

Dictionaries: When are they useful?

  • Lookups and crosswalksExamples: language translation; lookup state capitals
  • Representation�Useful for representing complex entities via heterogeneous dictionaries�Examples: Twitter data (status, user, comment, etc.)
  • Counting and grouping�Useful for tallying, counting, and assigning information to categories�Examples: how many violations per restaurant; how many representatives per state

4

5 of 12

Dictionary counting algorithms

Dictionaries also help us to count and group things. Examples:

  • Number of instances of each letter in a word
  • Number of restaurants with the most health violations (order by those who are the worst offenders)
  • The basic idea is to loop through a sequence of data and…
    • If we haven't seen that piece of data before, then we add it to our dictionary with a unique key and value of 1 (in other words "we've seen this piece of data once"
    • If we have seen that data before (i.e. it's already in our dictionary as a key), then we increment the count for that key

5

6 of 12

Tallying and Counting

How many times does each letter in the word supercalifragilisticexpialidocious appear?

The algorithm:

  • Create an empty dictionary
  • Loop through each letter in the word
  • If the letter is not in the dictionary, add a new entry to the dictionary
  • Otherwise, increment the existing dictionary entry by 1.

01_count_letters.py & Python Tutor

7 of 12

Activity: 02_count_words.py

Now, let's apply the same idea but counting unique WORDS in the novel Moby Dick!

  • The big difference here is instead of dealing with letters we need a way of going through word by word.

7

8 of 12

Activity: 03_restaurant_violations.py

Write a program that reads the contents of the Food_Establishment_Violations.csv for the City of Evanston and counts the number of violations that have occurred for each restaurant code. Then, print out the restaurant's name and the number of violations for that restaurant.

    • What should the key be?
    • What should the value be?

Note that the part that creates a lookup dictionary between restaurant "codes" (license number) and business name is already done for you!

8

9 of 12

Web APIs

  • You can think of Web APIs as functions that you access over the Internet using the http/https protocol.
  • They allow you to create, read, update, and delete data
  • Many organizations make their data available over REST (REpresentational State Transfer) so that third party apps (like your Project 2) can interoperate with their data

9

Your Computer

Server

APIs

10 of 12

Examples of Web APIs

Some examples of companies and organizations that have REST APIs include

  • Yelp
  • Spotify
  • CTA
  • Divvy Bikes
  • YouTube
  • Flickr

10

11 of 12

Access and Authentication

  • In order to access data from a REST API, you typically need to register with the provider and get an authorization token.
  • In the following examples, we'll show how you might create functions that do this from scratch, in Project 2, we'll give you a library of functions that already exist

11

12 of 12

Speaking of existing functions…

12