1 of 22

CSE 160 Section 4

File I/O!

CSE 160: Section 5

2 of 22

Logistics

  • Section Assignment 4 due Friday, April 24th @11:59pm
  • Programming Practice 5 due April 28th @11:59
  • Resubmission Cycle 4/20 - 4/24
    • Don’t forget to fill out the resubmission form!
  • Blurring HW due Friday, May 1st @11:59pm
    • Submitting on Gradescope
    • Wait for autograder!

CSE 160: Section 5

3 of 22

Lecture Review: File I/O

CSE 160: Section 5

4 of 22

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

5 of 22

Split

  • Split can be a really helpful function when dealing with strings (which is what you’re processing in file i/o)

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

6 of 22

Split

  • If you give split an argument, it will split the line of text on that. If you don’t give it anything, it will by default split on spaces.

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

7 of 22

Section Handout Problems

CSE 160: Section 5

8 of 22

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

9 of 22

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

10 of 22

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

11 of 22

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

12 of 22

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

13 of 22

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

14 of 22

Additional Problems

(from last week’s Section)

CSE 160: Section 5

15 of 22

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.

Python Tutor

CSE 160: Section 5

16 of 22

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)

Python Tutor

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)

PythonTutor

CSE 160: Section 5

17 of 22

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

18 of 22

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

19 of 22

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

20 of 22

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

21 of 22

Section Code:

CSE 160: Section 5

22 of 22

Looking ahead:

Advanced Data Structures

Be sure to remember the Section Code!

CSE 160: Section 5