Midterm 2 on March 26, next Wednesday
modules 1 - 8
iterating a string/list/dictionary (+ if statements)
while loop, for loops (two ways, difference?)
draw a loop table
purpose of iterating, for example:
mutate (list/dictionary methods)
aggregate (e.g., sum, factorial)
get max or min
concatenate strings (string methods)
On (at least most of) our computers, there is a file system via which we can create, save, modify, and remove files
On Mac: can browse with Finder
On Windows: can browse with windows explorer
File systems are often hierarchical
To open a file in a python program:
info = open(file_name, mode)
file_name
: the name of the file to open
mode
: the mode in which to open in
An absolute path describes the location from the root directory.
A relative path describes the location of a file relative to the current (working) directory.
.py
and example.txt
are in the same folder, the relative path is the filename itself - example.txtinfo.txt
read_file.py
Use info
object and call readline()
to read a line
read_file.py
Function name read_first_line
: given a filename
string, open file in read ('r'
) mode, read one line, close file, return that line.
Name your file first_read.py
and submit to gradescope.
info.txt
first_read.py
file.readline()
file.readlines()
file.read()
Use readlines()
:
Here info
is iterable, so we can use for x in file
loop:
We worked with .isnumeric()
, a string method to check if a string contains only digit characters
Here are some other useful string methods:
string.strip(chars)
– removes any of the characters in chars from the beginning or end of string, returns a stringstring.split(chars)
– splits string at the chars, returns a list.strip()
and .split(" ")
Use .strip()
and .split(" ")
to print a list: ['The', 'quick', 'brown', 'fox']
Use only .strip()
:
Use .strip()
and .split(" ")
:
It opens the file in read mode.
It iterates over the lines, stripping the line breaks and splitting each line by space.
It returns a list of lists, where each inner-list is a line containing all the words in that line
Test case (download text file):
def lines_and_words(file_name):
f = open(file_name, "r")
all_words = []
for line in f:
all_words.append(line.strip("\n").split(" "))
f.close()
return all_words
def main():
assert lines_and_words("info.txt") == [ ['The', 'quick', 'brown', 'fox'],
['jumped', 'over'], ['the', 'lazy'],
['bear'], ['sitting', 'by', 'the', 'tree'] ]
main()
It takes a string file_name
as argument and returns a dictionary with word counts.
Use str.lower()
to convert a string to lowercase.
Test case:
def count_words(file_name):
f = open(file_name, "r")
counts = {}
for line in f:
words = line.strip("\n").split(" ")
for w in words:
lower_case_w = w.lower()
if lower_case_w not in counts:
counts[lower_case_w] = 1
else:
counts[lower_case_w] += 1
return counts
def main():
assert count_words("info.txt") == {"the": 3, "quick": 1, "brown": 1,
"fox": 1 , "jumped": 1, "over": 1,
"lazy": 1, "bear": 1, "sitting": 1,
"by": 1, "tree": 1}
main()
You have 10 minutes to complete the quiz.
No need to write main()
function.