1 of 7

CSE 163

Lists and Files

Hunter Schafer�

🎶 Listening to: Whitney

💬 Before class: What was the most interesting thing you learned last week? �(Doesn’t have to be in this class)

2 of 7

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 7

Files

  • Process a file line by line

  • Process each word on a file in a line

3

with open(file_name) as file:

lines = file.readlines()

for line in lines:

# do something with line

with open(file_name) as file:

lines = file.readlines()

for line in lines:

words = line.split()

for word in words:

# do something with word

4 of 7

Strings

vs.

Lists

4

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)

5 of 7

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

5

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

if 'dogs' in words:

print('Found it')

if 'cats' not in words:

print('Not there')

6 of 7

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!

6

7 of 7

Before Next Time

  • Checkpoint 0 Due Tonight
    • Remember includes your introduction video
  • Complete Lesson 4
    • Remember not for points, but do go towards Checkpoint Tokens
  • Get started on TH0!
  • Office hours start today! Come stop by!

Next Time

  • More Data Structures
    • Tuples
    • Sets
    • Dictionaries

7