1 of 10

CSE 163

Lists and Files

Suh Young Choi

🎶 Listening to: Max LL

💬 Before class: Happy Friday! How has your first week been?

2 of 10

This Time

  • Lists
  • Files
  • Python Runtime
    • Script vs Jupyter
    • File Paths

Last Time

  • Python Crash Course - Day 2
    • Strings and String Functions
    • Slicing
    • Lists
    • Documentation
    • None

2

3 of 10

Quick Announcements!

  • Checkpoint 0 and Learning Reflection 0 released today (due Monday, 6/30 at 11:59pm)

  • Make sure to turn in Section 1 Assignment by 11:59pm today

  • Take-Home Assessment 0: Startup released today (due Thursday, 7/3 at 11:59pm)

  • Course website is online! Go check it out!

3

4 of 10

Additional Note on CP and THA

  • Optional code workspaces are given to you in Ed

  • There are no built-in tests in Ed

  • Submission checklist does not count as your submission!

4

5 of 10

Recap: Lesson

File processing

  • Line-based processing
  • Token-based

Lists

  • Introduced more functions to alter lists
  • Unlike strings, lists are mutable!
  • The in keyword

File paths

  • Absolute vs. relative paths
  • Make sure to use absolute paths in this class!

Jupyter Notebooks (briefly)

5

6 of 10

Files

  • Process a file line by line

  • Process each word on a file in a line

6

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:

words = line.split()

for word in words:

# do something with word

7 of 10

Strings

vs.

Lists

7

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)

8 of 10

Advanced Lists

  • List methods can modify the contents of the list (e.g. append)
  • Can use the in keyword to see if a list contains a value

8

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

if 'dogs' in words:

print('Found it')

if 'cats' not in words:

print('Not there')

  • Other functions we introduced (with the exception of .index()) will modify the list data structure!

9 of 10

Group Work:

Best Practices

When you first working with this group:

  • Introduce yourself!
  • If possible, angle one of your screens so that everyone can discuss together

Tips:

  • Starts with making sure everyone agrees to work on the same problem
  • Make sure everyone gets a chance to contribute!
  • Ask if everyone agrees and periodically ask each other questions!
  • Call TAs over for help if you need any!

9

10 of 10

Before Next Time

  • CP + LR 0 due Monday
  • Complete Lesson 4
  • Get started on TH0!

Next Time

  • More Data Structures
    • Tuples
    • Sets
    • Dictionaries

10