Check your understanding of the primer: iterating by index, iterating over strings, and iterating over files.

Question 1

Why is creating a new list preferred over mutating the original one?

Select one answer, then check.

  • Correct. The primer says mutating a list means other parts of the program using that list "now have to be kept in mind," which is why we typically avoid mutation in favor of creating new lists.
  • Not quite. Mutating a list in place does not change where its items live; the primer's concern is about other code that still relies on the list.
  • Not quite. The primer gives a concrete reason (other code relying on the list), not a historical debate.
  • Not quite. enumerate works fine on a list you are mutating; that is not the primer's reason.

Question 2

Which of the following statements is true about iterating over a string in Python?

Select one answer, then check.

  • Not quite. Iterating directly over a string gives characters, not words; you get words only by iterating over the result of .split().
  • Not quite. split works on any string; without an argument it splits on whitespace, and you can pass any character to split on, not only a comma.
  • Correct. The primer says "if you iterate through a string directly, then it gives you each character one-by-one, just like if you indexed the string."
  • Not quite. A plain for loop over a string already works, character by character; split is only needed to iterate over chunks instead.

Question 3

Which of the following is NOT true about file iteration?

Select one answer, then check.

  • Correct, this is the false statement. The primer says each file is "uniquely identified by its path": if a path is unique to a file, two different files cannot share it.
  • Not quite, this is true. The primer stresses that "you must always remember to close files once you are done with them."
  • Not quite, this is true. The File Iteration example loops for line in grade_file.
  • Not quite, this is true. The primer describes folders holding other folders, "leading to a hierarchy of files and folders."