CSE 160 Section 4
File I/O!
CSE 160: Section 5
Logistics
CSE 160: Section 5
Lecture Review: File I/O
CSE 160: Section 5
File I/O
my_file = open(filepath)
for line_of_text in my_file:
# process line_of_text
my_file.close()
with open(filepath) as my_file:
for line_of_text in my_file:
# process line_of_text
Or
CSE 160: Section 5
Split
Example:
O
Output:
line_of_text = “you’re going to be amazing”
x = line_of_text.split()
print(x)
[“you’re”, “going”, “to”, “be”, “amazing”]
CSE 160: Section 5
Split
Example:
O
Output:
line_of_text = “you’re,going,to,be,amazing”
x = line_of_text.split(“,”)
print(x)
[“you’re”, “going”, “to”, “be”, “amazing”]
CSE 160: Section 5
Section Handout Problems
CSE 160: Section 5
Problem 1
Write a function called count_words(filename) that reads the file and returns the total number of words across all lines. The file is formatted so that each line is a sentence.
For example, if you had a file quotes.txt that contains:
Hi my name is Annie.
Let’s learn Python!
The returned value would of count_words(“quotes.txt”) be:
8
CSE 160: Section 5
Problem 1
def count_words(filename):
word_count = 0
my_file = open(filename):
for line in my_file:
words = line.split()
word_count += len(words)
return word_count
CSE 160: Section 5
Problem 2
Write a code snippet that reads list_of_points.txt
Point1: 1 2 3 4
Point2: 5 6 7 8
Point3: 0 0 0 0
and returns:
“Point1: [1, 2, 3, 4]”
“Point2: [5, 6, 7, 8]”
“Point3: [0, 0, 0, 0]”
CSE 160: Section 5
Problem 2
my_file = open("list_of_points.txt")
for line in my_file:
words = line.split()
point = []
for coordinate in words[1:]:
point.append(int(coordinate))
print(words[0], point)
my_file.close()
CSE 160: Section 5
Problem 3
Write a function called evens_and_odds() that takes two parameters, start and end, and returns a nested list composed of two lists. The first list contains all even numbers between start and end (both inclusive). The second list contains all odd numbers between start and end (both inclusive)
Sample outputs
evens_and_odds(0, 10)
→ [[0, 2, 4, 6, 8, 10], [1, 3, 5, 7, 9]]
evens_and_odds(5, 15)
→ [[6, 8, 10, 12, 14], [5, 7, 9, 11, 13, 15]]
CSE 160: Section 5
Problem 3
def evens_and_odds(start, end):
result = [[], []]
for num in range(start, end + 1):
if num % 2 == 0:
result[0].append(num)
else:
result[1].append(num)
return result
CSE 160: Section 5
Additional Problems
(from last week’s Section)
CSE 160: Section 5
Problem 4
Write a function called count(lst, value) that takes in a list and a value. It should return the number of times the value is in the list. For example: if my_lst = [1, 3, 5, 3, 2, 5, 6, 3, 3] when you call count(my_lst, 3) it should return 4.
CSE 160: Section 5
Problem 5
Given a list of students’ names (student_lst), write a function max_height(student_lst) that returns the maximum height in inches. Assume a function get_height(student) is given to you, and will return the height (in inches) of any student when given a student name.
For example: get_height(“Nicholas”) will return 75.
a) Implement the function max_height(student_lst)
b) What is the return type of max_height(student_lst)?
c) Suppose you printed the max height instead of returning it. What would be the return
type of max_height(student_lst)
CSE 160: Section 5
Problem 6 Dot Product
Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.
So if these were your two lists:
The dot product would be:
index: | 0 | 1 |
value: | 1 | 2 |
index: | 0 | 1 |
value: | 3 | 4 |
CSE 160: Section 5
Problem 6 Dot Product
Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.
So if these were your two lists:
The dot product would be:
1*3
index: | 0 | 1 |
value: | 1 | 2 |
index: | 0 | 1 |
value: | 3 | 4 |
CSE 160: Section 5
Problem 6 Dot Product
Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.
So if these were your two lists:
The dot product would be:
1*3 + 4*2
index: | 0 | 1 |
value: | 1 | 2 |
index: | 0 | 1 |
value: | 3 | 4 |
CSE 160: Section 5
Problem 6 Dot Product
Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.
So if these were your two lists:
The dot product would be:
1*3 + 4*2 = 11 Python Tutor
index: | 0 | 1 |
value: | 1 | 2 |
index: | 0 | 1 |
value: | 3 | 4 |
CSE 160: Section 5
Section Code: �
CSE 160: Section 5
Looking ahead:
Advanced Data Structures
Be sure to remember the Section Code!
CSE 160: Section 5