OIM3640 - Problem Solving and Software Design

2026 Spring

Session 13 (3/5)

contain

Today's Agenda

  • Announcements/Updates
  • What We've Learned So Far
  • Review Questions
  • Lecture: Chapter 9 - Lists
  • Your Turn

Announcements/Updates

  • Mini Project 1 - keep working!
    • Core functionality should be done or close to done
    • Come to office hours if you're stuck
  • 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
  • Working with AI: Copilot Ask/Plan/Agent, reading AI-generated code

🙋 Review: Strings

stocks = 'AAPL,MSFT,GOOG,AMZN'

  • What is stocks[0]? stocks[-1]?
  • How do you make it become 'AAPL,MSFT,GOOG,AMZN,TSLA'?
  • What is 'GOOG' in stocks? 'AA' in stocks?
  • What is stocks.lower()?
  • What is stocks.find('MSFT')?
  • Sort stocks in reverse?
  • What is stocks.strip('A')?

🙋 Review: Loop Patterns

What does this print?

def count_vowels(s):
    count = 0
    for c in s:
        if c in 'aeiou':
            count += 1
            return count
    return count

print(count_vowels('apple'))
print(count_vowels('sky'))

Chapter 9 - Lists

What we'll learn:

  • Lists as mutable sequences
  • List operations and methods
  • Lists and strings (split, join)
  • Sorting
  • Aliasing (important!)

A List Is a Sequence

stocks = ['AAPL', 'GOOG', 'MSFT', 'AMZN']
prices = [182.30, 141.80, 415.20, 178.50]
mixed = ['AAPL', 182.30, True]    # can mix types
empty = []

Access elements the same way as strings:

stocks[0]          # 'AAPL'
stocks[-1]         # 'AMZN'
len(stocks)        # 4
'GOOG' in stocks   # True

Lists Are Mutable!

Unlike strings, you can change a list in place:

stocks = ['AAPL', 'GOOG', 'MSFT', 'AMZN']
stocks[2] = 'META'    # replace MSFT with META
stocks   # ['AAPL', 'GOOG', 'META', 'AMZN']

Compare with strings:

ticker = 'MSFT'
ticker[0] = 'X'   # TypeError! Strings are immutable.

This is the key difference between lists and strings.

List Slices

Same syntax as string slices:

stocks = ['AAPL', 'GOOG', 'META', 'AMZN']
stocks[1:3]    # ['GOOG', 'META']
stocks[:2]     # ['AAPL', 'GOOG']
stocks[2:]     # ['META', 'AMZN']
stocks[:]      # ['AAPL', 'GOOG', 'META', 'AMZN'] (copy!)

A slice returns a new list (not an alias).

List Methods

stocks = ['AAPL', 'GOOG', 'META']

stocks.append('TSLA')           # ['AAPL', 'GOOG', 'META', 'TSLA']
stocks.extend(['NVDA', 'AMZN']) # [..., 'TSLA', 'NVDA', 'AMZN']

stocks.pop()                    # returns 'AMZN' (removes last)
stocks.remove('META')           # removes first 'META'

Important: most list methods modify in place and return None!

result = stocks.append('NFLX')
print(result)   # None  (common trap!)

Lists and Strings

Convert between lists and strings:

list('AAPL')                          # ['A', 'A', 'P', 'L']

'AAPL,GOOG,META,AMZN'.split(',')     # ['AAPL', 'GOOG', 'META', 'AMZN']

','.join(['AAPL', 'GOOG', 'META'])   # 'AAPL,GOOG,META'

split() and join() are inverses of each other.

Sorting

sorted() returns a new sorted list (original unchanged):

stocks = ['GOOG', 'AAPL', 'META']
sorted(stocks)    # ['AAPL', 'GOOG', 'META']
stocks            # ['GOOG', 'AAPL', 'META']  (unchanged!)

Useful trick with strings:

''.join(sorted('listen'))   # 'eilnst'
''.join(sorted('silent'))   # 'eilnst'  — same! They're anagrams.

⚠️ Aliasing

Two variables can refer to the same list:

a = [1, 2, 3]
b = a          # b is NOT a copy — it's the SAME list!

b[0] = 99
print(a)       # [99, 2, 3]  — a changed too!

To make a copy:

b = a[:]       # slice copy
b = list(a)    # list() copy

This is a common source of bugs. Be careful when passing lists to functions!

The Accumulator Pattern with Lists

Build up a list inside a loop:

palindromes = []
for word in word_list:
    if word == word[::-1]:    # reverse the word
        palindromes.append(word)

This pattern is everywhere: filter a collection by some condition, collect results into a new list.

📝 Your Turn

Open Chapter 9 notebook and work through the examples and exercises:

  • Create lists, try indexing, slicing, and methods
  • Practice split() and join() conversions
  • Try the aliasing examples (predict before you run!)
  • Use the accumulator pattern to filter a list

Done early? Continue working on Spelling Bee if you haven't finished.

Questions? Ask now!

Before You Leave

  • Any questions?
  • Continue your Learning Log for this week (logs/wk07.md)
  • Work on Chapter 9 exercises
  • Continue working on Mini Project 1
  • Push your work to GitHub

Next session: Dictionaries and Tuples (Chapters 10-11)

global styles

Answer: 1, 0 — premature return inside the if block. Returns on FIRST vowel found instead of counting all.