1 of 10

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!

slido.com

#2348882

2 of 10

Announcements

  • (Optional) Lesson 1.2 Review due tonight at 11:59pm on Gradescope
  • Programming Practice 1 releases tomorrow (Thursday!)
  • Homework 1: Processing releases Friday!
    • Due next Friday, July 3rd.
    • Attend office hours if you get stuck!

slido.com

#2348882

3 of 10

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)

slido.com

#2348882

4 of 10

Advanced Lists

  • List methods can modify the contents of the list. Lists are mutable!
  • Can use the in keyword to see if a list contains a value

words = ['I', 'love', 'dogs']

if 'dogs' in words:

print('Found it')

if 'cats' not in words:

print('Not there')

slido.com

#2348882

5 of 10

  • Function comments are the best way for others to know what your code does!
    • Maintain good grammar
    • Focus on what your function does, rather than how it does it?
    • Ask yourself: If I came back to this code after 1-2 years, can I easily understand what this function does by reading my comment?
    • Someone else should be able to understand, maintain, and develop with your code
  • Function comments should be concise and provide a good explanation of what the function is doing. Some things to think about:
    • What is the overall purpose of your function?
    • Does your function take in any special parameters?
    • What does it output? What does it return?

Function Comments

slido.com

#2348882

6 of 10

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

7 of 10

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

slido.com

#2348882

8 of 10

  • None is a special value in Python that represents the absence of a value
  • You don’t have to know a lot about None for this course except:
    • It can cause your program to crash if you ask a None value to do something
    • You might have cases where you return None (can lead to unexpected behavior or errors in your program)
    • Check for None using the is keyword, instead of ==

None Value

def increment(x):

if x < 0:

return None

else:

return x + 1

if increment(-1) is None:

print('Failed')

9 of 10

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

slido.com

#2348882

10 of 10

Lesson Recap: Lists and Files

  • Lists
    • Introduced function to alter lists
    • Lists are mutable! Strings are not!
    • The in keyword
    • Indexed-sequence (like strings) but can hold any value!
    • For Java people – a lot like an ArrayList
  • File Processing: Line vs. Token Processing
  • File Paths
    • Absolute vs. relative paths
    • Make sure to use relative paths in Gradescope submissions!
  • Documentation
    • Doc-string (““ documentation ”” )

slido.com

#2348882