A Smarter Way to Learn Python / Chapter 70 Exercises

  • Index of exercises
  • Email me

1

2

3

4

5

6

7

8

9

10

11

12

 

Congratulations. You've aced all the exercises for this chapter.


To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to Trinket.


Post a review on Amazon for the paperback or the Kindle edition.


Email me to give me a compliment, complaint, or correction. I'll respond.


  All of my books include free interactive online exercises that make the knowledge stick.

A Smarter Way to Learn JavaScript
Shop for it at Amazon
A Smarter Way to Learn HTML and CSS
Shop for it at Amazon
A Smarter Way to Learn jQuery
Shop for it at Amazon

copyright © 2017 by Mark Myers. All rights reserved.

0,1,2,3,4,5,6,7,8,9,10,11

0

0

0

0


Open products.txt for writing. Make up the file handle. with open("products.txt", "w") as f:^ *with open\(•products\.txt•, •w•\) as [a-zA-Z_][a-zA-Z0-9_]*: *$
Rewrite the following code to open a different CSV file. Make up its name. with open("books.csv", "w", newline="") as f: with open("records.csv", "w", newline="") as f:^ *with open\(•.+\.csv•, •w•, newline=••\) as f: *$
Complete the line. The file handle is file_to_read. with open("stores.csv", "w", newline=__________"") as file_to_read: "`"`) `as `file_to_read`:
Complete the line. The file handle is f. with open("stores.csv", "w", ______________newline="") as f:newline="") as f:
Open laws.csv to write. The file handle is x. with open("laws.csv", "w", newline="") as x: ^ *with open\(•laws\.csv•, •w•, newline=••\) as x: *$
Open a CSV file to read. Use shorthand. Make everything up. with open("cities.csv") as f: ^ *with open\(•.+\.csv•\) as [a-zA-Z_][a-zA-Z0-9_]*: *$
Open a CSV file to write. Make everything up. with open("cats.csv", "w", newline="") as f: ^ *with open\(•.+\.csv•, •w•, newline=••\) as [a-zA-Z_][a-zA-Z0-9_]*: *$
Assign a CSV filename to a variable. Then use the variable to open the file for writing. Make everything up. filename = "histories_of_the_world_before_Genghis_Khan.csv"
with open(filename, "w", newline="") as f:
^ *([a-zA-Z_][a-zA-Z0-9_]*) = •.+\.csv•\nwith open\(\1, •w•, newline=••\) as [a-zA-Z_][a-zA-Z0-9_]*: *$
Open a text file and write a string to it. Make everything up. Remember to indent. with open("greeting.txt", "w") as f:
  f.write("Hi")
^ *with open\(•.+\.txt•, •w•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n \1\.write\(•.+•\) *$
Open a text file and append a string to it. Make everything up. with open("greeting.txt", "a") as f:
  f.write("Hi")
^ *with open\(•.+\.txt•, •a•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n \1\.write\(•.+•\) *$
  1. If you have a spreadsheet program like Excel that exports to CSV, create a spreadsheet with at least three rows and three columns. Export it to the Desktop as a CSV file.
  2. Alternatively, create a CSV file in IDLE—three lines of three comma-separated values each (no quotation marks, just text). Name the file whatever you like, but end it in .csv. Save it to the Desktop.
  3. Revise the Python program you created in the previous set of exercises to read and display a value in this CSV file. Save the program and run it.