1 of 12

CSE 163

Section XX

TA1 & TA2

Question of the Day: If you were a data structure, which one would you be and why?

2 of 12

Announcements

  • Homework 1: Processing - due next Wednesday 4/16 @ 11:59pm
  • Please use our course resources:
    • Style Guide
    • Citation Guide
  • If you are stuck on a task for more than 20 minutes, please reach out!
    • Office Hours
    • Ed Discussion
  • Resubmission information will be released soon

3 of 12

Recap

What we’ve Learned so far:

  • Week 1
    • Basic Python Syntax
      • Control Flow
    • Strings, Lists
  • Week 2
    • File Processing
    • Built-in Data Structures
      • List
      • Set
      • Dict
      • Tuple

4 of 12

Game Plan

What We’ll Cover Today

  • Review
  • In-section group activity 1!

After section, please fill out the groupmate evaluations for today through the Canvas assignment!

5 of 12

List

  • General purpose multi value storage
    • Multi-type
  • Mutable
    • append()
    • extend()
    • remove(), pop()
  • Indexable

Use Cases:

  • Need to store multiple values
  • Unknown number of values

new_list = list()

new_list = []

new_list.append("a") # ["a"]

new_list.append(2) # ["a", 2]

new_list[0] # "a"

new_list.pop(0) # returns “a”,

new_list is [2]

6 of 12

Tuple

  • General purpose multi value storage
    • Multi-type
  • Immutable
    • Memory benefits
  • Indexable
  • Can be defined arbitrarily with parentheses or commas
  • Can hold more than two values

Use Cases:

  • Fixed size multi-value situations

tup = ("hello", "world")

tup[0] # "hello"

# takes in params a and b

def pack(a, b):

# returns a tuple of (a, b)

return a, b

7 of 12

Sets

  • Same-type unordered storage
    • Can only store immutable values
    • Does not store duplicates
  • Lightning fast membership queries
  • Mutable
    • add(),remove(),update()
    • union(), intersection(),

difference()

  • Note: Cannot be initialized with {} Why?

Use Cases:

  • Need fast membership queries
  • Need to store only unique values

my_set = set()

my_set.add(10) # {10}

my_set.add(30) # {10, 30}

my_set.add(30) # {10, 30}

my_set.add([]) # Error!

if 10 in my_set: # Membership Query

print("Found 10!")

8 of 12

Dictionaries

  • Stores unordered Key-Value pairs
    • Keys must be immutable and unique
    • Values can be mutable and duplicated
  • Lots of tooling
    • .keys()
    • .values()
    • .items()
  • Use key-checking pattern

Use Cases:

  • Storing corresponding mappings
    • Student Name -> ID Number
    • Region -> Area Code
  • Very powerful.

d = dict()

d = {}

d['Triangle'] = 3

d['Square'] = 4

d['Square'] # 4

items = ["cat", "hat", "bat", "cat"]

my_dict = dict()

for i in items: # also really fast!

if i not in my_dict:

my_dict[i] = 0

my_dict[i] += 1

my_dict

# {"cat": 2, "hat": 1, "bat": 1}

9 of 12

File Processing

  • “with open” syntax
  • Read with .readlines() or .read() function
  • Two approaches:
    • Line based
    • Token based

file_path = "home/doggies.txt"

with open(file_path) as f:

lines = f.readlines()

for l in lines:

print(l)

file_path = "home/doggies.txt"

with open(file_path) as f:

words = f.read().split()

for word in words:

print(word)

10 of 12

11 of 12

12 of 12