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)
This Time
Last Time
2
Files
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
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)
Advanced Lists
5
words = ['I', 'love', 'dogs']
if 'dogs' in words:
print('Found it')
if 'cats' not in words:
print('Not there')
Group Work:
Best Practices
When you first working with this group:
Tips:
6
Before Next Time
Next Time
7