Python for data

Lesson 14 of 15

Quiz ยท 10 min

Quiz, Python for data

Answer the questions about the course. You pass with 70 percent. When you do not know an answer, look at the lesson again and come back.

Question 1 of 14

What happens to the file when the block of this with statement ends?

with open("notes.txt", encoding="utf-8") as file:
    text = file.read()
Question 2 of 14

Why do you read a CSV file with the csv module, and not with line.split(",")?

Question 3 of 14

A file has the header row trip_id,minutes and the row 1017,52. You read it with csv.DictReader into the list trips. What is trips[0]["minutes"]?

Question 4 of 14

Which line makes a list of the trips that took more than 30 minutes?

Question 5 of 14

What does this line give?

sorted(trips, key=lambda trip: trip["minutes"], reverse=True)[:3]
Question 6 of 14

This loop counts the trips of each station. Why does it use counts.get(station, 0) and not counts[station]?

counts = {}
for trip in trips:
    station = trip["start"]
    counts[station] = counts.get(station, 0) + 1
Question 7 of 14

The times of five trips are [10, 12, 14, 20, 600]. Which statements are true? Choose all that apply.

Choose all answers that apply.

Question 8 of 14

What does this code print?

def to_number(text):
    try:
        return int(text)
    except ValueError:
        return None


print(to_number("7 km"))
Question 9 of 14

You remove 5 rows from a file of 14 rows, because their values cannot be true. What do you do with that fact?

Question 10 of 14

datetime.fromisoformat("2026-06-06T10:20").weekday() gives 5. Which day is it?

Question 11 of 14

A trip starts at Ferry Dock, and zone_of has no key with that name. What does zone_of.get(trip["start"], "unknown") give?

Question 12 of 14

Which statements about JSON are true? Choose all that apply.

Choose all answers that apply.

Question 13 of 14

Is "art" in "The start was hard" true or false, and why?

Question 14 of 14

Which mode of open adds text to the end of a file and keeps the old content?

Answer every question to check.