OIM3640 - Problem Solving and Software Design

2026 Spring

Session 14 (3/10)

contain

Today's Agenda

  • Announcements/Updates
  • What We've Learned So Far
  • Review Questions
  • Lecture: Chapter 10 - Dictionaries
  • Your Turn: Mini Project 1 Code Review

Announcements/Updates

  • Mini Project 1 - due soon!
    • Make sure your code runs and README is complete
  • Communication
    • Office Hours: Walk-in or by appointment
    • Email: Specify course # in subject, e.g., "OIM3640: GitHub settings"
    • You are required to meet with me at least once this semester
  • Questions?

What We've Learned So Far

  • Python Fundamentals
    • Variables & types, operators, expressions
    • Functions: def, parameters, return vs print()
    • Conditionals: if/elif/else
  • Iteration & Patterns
    • for loops, while loops, break, continue
    • Search patterns: in operator, early return, flag variables, counters
  • Strings: indexing, slicing, immutability, methods
  • Lists: mutable sequences, append/extend/pop, aliasing, sorting
  • Working with AI: Copilot Ask/Plan/Agent, reading AI-generated code

🙋 Review: Strings vs Lists

  • What common operations work on both strings and lists?
  • Are strings mutable? Are lists mutable?
  • How do you add to the end of a string? To the end of a list?
  • How do you sort a list in-place? (sort() or sorted()?)
  • How do you make a copy of a string? A copy of a list?

🙋 Review: Copying vs Aliasing

What does this print?

a = [1, 2, 3]
b = a
b.append(4)
print(a)
print(a is b)

What about this?

a = [1, 2, 3]
b = a[:]
b.append(4)
print(a)
print(a is b)

🙋 Review: split() Gotcha

What does each produce?

'a  b  c'.split()       # ?
'a  b  c'.split(' ')    # ?
  • split() treats consecutive whitespace as one separator
  • split(' ') splits on each single space (empty strings!)

When reading a file, why is line.strip() important?

Chapter 10 - Dictionaries

What we'll learn:

  • Dictionaries as key-value mappings
  • Creating, accessing, and modifying dicts
  • Looping over dictionaries
  • The counting pattern (histogram)
  • Common dict methods

A Dictionary Is a Mapping

eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}

Access values by key (not by index):

eng2sp['two']        # 'dos'
eng2sp['four']       # KeyError!
'two' in eng2sp      # True (checks keys, not values)
len(eng2sp)          # 3

Keys must be immutable (strings, numbers, tuples). Values can be anything.

Creating and Modifying Dicts

# Start empty, add entries
prices = {}
prices['AAPL'] = 178.50
prices['GOOG'] = 141.80
prices['MSFT'] = 415.20

# Update an existing key
prices['AAPL'] = 182.30

# Delete a key
del prices['GOOG']

print(prices)   # {'AAPL': 182.30, 'MSFT': 415.20}

Looping Over Dictionaries

prices = {'AAPL': 182.30, 'MSFT': 415.20, 'GOOG': 141.80}
for key in prices:                # loop over keys
    print(key, prices[key])

for value in prices.values():     # loop over values
    print(value)

for key, value in prices.items(): # loop over both
    print(f'{key}: ${value}')

The Counting Pattern

Count how many times each item appears:

def histogram(s):
    d = {}
    for c in s:
        if c not in d:
            d[c] = 1
        else:
            d[c] += 1
    return d

histogram('brontosaurus'){'b': 1, 'r': 2, 'o': 2, ...}

This pattern is everywhere: counting words, votes, frequencies, etc.

Useful Dict Methods

d = {'a': 1, 'b': 2, 'c': 3}

d.get('a', 0)     # 1 (key exists)
d.get('z', 0)     # 0 (key missing, returns default)

list(d.keys())     # ['a', 'b', 'c']
list(d.values())   # [1, 2, 3]
list(d.items())    # [('a', 1), ('b', 2), ('c', 3)]

get() avoids KeyError - very useful in the counting pattern:

d[c] = d.get(c, 0) + 1    # shorter version of if/else counting

📝 Your Turn: Mini Project 1 Code Review

Open your Mini Project 1 code and check:

  1. Did you use if __name__ == '__main__':? If not, add it!
  2. Did you use a list or dict? Where and why?
  3. If not, where could you? (e.g., storing results, counting, mapping inputs to outputs)
  4. Are your functions small and focused, or one giant block?
  5. Are your variable names clear to someone else?

📝 Code Review (continued)

More things to check in your code:

  • Any hardcoded values that could be a list or dict?
  • Are your functions using return or just print()?
  • If you read files, are you using strip() properly?

Discuss with a neighbor, then share!

Before You Leave

  • Any questions?
  • Start your Learning Log for this week (logs/wk08.md)
  • Work on Chapter 10 exercises
  • Review and improve your Mini Project 1 code
  • Push your work to GitHub

Next session: Tuples, Sets, and Choosing Data Structures

global styles