CSE 163
Lists and File Processing
Arpan Kapoor�Summer 2026��💭Icebreaker:
If you had a One Wish Willow, what would you wish for? Add to our Slido!
#2348882
Announcements
#2348882
Strings vs. Lists
s = 'hello world'
# Length
len(s) # 11
# Indexing
s[1] # 'e'
s[len(s) - 1] # 'd'
# Looping
for i in range(len(s)):
print(s[i])
for c in s:
print(c)
l = ['dog', 'says', 'woof']
# Length
len(l) # 3
# Indexing
l[1] # 'says'
l[len(l) - 1] # 'woof'
# Looping
for i in range(len(l)):
print(l[i])
for word in l:
print(word)
#2348882
Advanced Lists
words = ['I', 'love', 'dogs']
if 'dogs' in words:
print('Found it')
if 'cats' not in words:
print('Not there')
#2348882
Function Comments
#2348882
Recap: Anatomy of a Function
6
def function_name(parameter, ...):
"""
What does this function do?
"""
#Function code
return value
def keyword
Function name
Optional
parameters
Function code
colon
Indentation
Return statement
Docstring comment
Which function comment is “better”?
def sum_what(nums):
“””
Takes in a list of
numbers and returns the
sum of those numbers.
“””
total = 0
for i in nums:
total += i
return total
def sum_how(nums):
“””
Takes in a list of numbers,
then calculates the sum of
all numbers using a for loop
to iterate through all numbers.
“””
total = 0
for i in nums:
total += i
return total
#2348882
None Value
def increment(x):
if x < 0:
return None
else:
return x + 1
if increment(-1) is None:
print('Failed')
File Processing
Line-by-line processing
Token processing
When would you use one over the the other?
with open(file_name) as f:
lines = f.readlines()
for line in lines:
# do something with line
with open(file_name) as f:
lines = f.readlines()
for line in lines:
tokens = line.split()
for token in tokens:
# do something with token
#2348882
Lesson Recap: Lists and Files
#2348882