"OIM3640: GitHub settings"
def
return
print()
if
elif
else
for
while
break
continue
in
stocks = 'AAPL,MSFT,GOOG,AMZN'
stocks[0]
stocks[-1]
'AAPL,MSFT,GOOG,AMZN,TSLA'
'GOOG' in stocks
'AA' in stocks
stocks.lower()
stocks.find('MSFT')
stocks
stocks.strip('A')
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'))
What we'll learn:
split
join
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
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.
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).
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!
None
result = stocks.append('NFLX') print(result) # None (common trap!)
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.
split()
join()
sorted() returns a new sorted list (original unchanged):
sorted()
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.
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!
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.
Open Chapter 9 notebook and work through the examples and exercises:
Done early? Continue working on Spelling Bee if you haven't finished.
Questions? Ask now!
logs/wk07.md
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.