Lecture 18 Slides - Processing Data with Loops

1 of 7

Loops and Data

CS 110

1

2 of 7

Reminders and Announcements

  • PLEASE work on P1 (Ex 6 is due today)
  • P1 due 2/28 (no late submissions)
  • Quiz 2 is next Wednesday - focuses on loops and conditionals
    • Students with Testing accommodations see email for details
    • Any grading errors/mistakes need to be rectified by start of Q2. We will not change any grades for assignments from Q1 to Q2 after the start of the Quiz.

Next Week

  • Monday - Q2 Review (In-Class MQ 12)
  • Wednesday - Q2
  • Friday - Dictionaries (P1 Due!)

3 of 7

More Number Challenges: practice on your own!

numbers = [65, 1800, 12, 20, 1963, 5000, 260, 0, 40, 953, 775]

Given the list above, do the following:

  • Find the largest number
  • Find the smallest number
  • Find the sum of the numbers
  • Find the average of the numbers
  • Print every number in the list that is a multiple of 2 or 5

3

4 of 7

Loops and P1 (lava_lamp.py)

We can use loops to repeat ANY action.

4

5 of 7

Review: Loop Short Circuiting

  • Want to skip an iteration of a loop?
    • Use continue
  • Want to end a loop completely?
    • Use break

5

6 of 7

Data Filter Example: Warm-Up

  • Open the file data_filter.py
  • Do the following exercises using a for loop:
    • Print each “sub-list” (note that people is a list of lists) to the screen using a for loop
    • Print only the name for each person
    • Print the name and the job of each person

6

7 of 7

Data Filter Functions: data_filter.py

  • Create a function called any_90210 that takes a list of people as an argument and returns a boolean representing the answer to the question.
  • Create a function called get_list_of_zip_codes that takes a list of people as an argument and returns a list of zip codes (i.e., a list of strings). (Challenge: have it return a list of unique zip codes)
  • Create a function called is_jersey_film_editor that takes in a single person as an argument and returns whether or not that person has an occupation of "Editor, film/video" and is from New Jersey
  • Create a function called, get_jersey_film_editors that takes as input a list of people and returns a subset of the original list of people who are Film editors from New Jersey.
  • Challenge: Create a function called filter_by_occupations_or_states that takes as input three lists: one of people, one of occupations, and one of states; and returns a subset of the list of people who either have one of the given occupations or live in one of the given states.

7