A Smarter Way to Learn Python / Chapter 75 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


Save this dictionary to a JSON file. (2 lines of code) Assume the json module has already been imported. Make up the filename and file handle. bird_dict = {
  "crow": "corvid",
  "robin": "thrush",
  "owl": "raptor",
}
with open("bird_file.json", "w") as j:
  json.dump(bird_dict, j)
^ *with open\(•.+•, •w•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n json\.dump\(bird_dict, \1\) *$
Open a JSON file for reading. Make everything up. with open("bird_file.json") as j:^ *with open\(•.+•\) as [a-zA-Z_][a-zA-Z0-9_]*: *$
Fill in the blank. with open("customer_29876.json") as f:   customer_29876 = ____________ json.load(f) json`.`load`(`f`)
Code the next line. Assign the content to owl_dict. Start by indenting. with open("owls.json") as file_to_read: owl_dict = json.load(file_to_read) owl_dict = json.load(file_to_read)
Code the next line. Make up the variable. Remember to indent. with open("owls.json") as j:   owls_dict = json.load(j) ^ * [a-zA-Z_][a-zA-Z0-9_]* = json\.load\(j\) *$
Read the file and assign its content to a variable. Then print the content. Indent your first line, but not your second line. Make up the name of the variable. with open("products.json") as f:   dict = json.load(f)
print(dict)
^ * ([a-zA-Z_][a-zA-Z0-9_]*) = json\.load\(f\)\nprint\(\1\) *$
Open a JSON file for reading, then read it. Make everything up. with open("team_list.json") as j:
  teams = json.load(j)
^ *with open\(•.+\.json•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = json\.load\(\1\) *$
A dictionary has been assigned to the variable products. Save it in a JSON file. Then open the file and read it. Assume that the json module has already been imported. Make up whatever you need to make up. with open("prods.json", "w") as j:
  json.dump(products, j)
with open("prods.json") as j:
  p_dict = json.load(j)
^ *with open\(•(.+)\.json•, •w•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n json\.dump\(products, \2\)\nwith open\(•\1\.json•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = json\.load\(\3\) *$
Open and read a JSON file containing a list. Assign one of the list elements to a variable. Don't indent the line that assigns the list element. Make everything up. with open("genius_list.json") as j:
  the_list = json.load(j)
third_genius = the_list[2]
^ *with open\(•.+\.json•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n ([a-zA-Z_][a-zA-Z0-9_]*) = json\.load\(\1\)\n[a-zA-Z_][a-zA-Z0-9_]* = \2\[-?[0-9]+\] *$
Open and read a JSON file containing a dictionary. Assign one of the values to a variable. The key is a string. Don't indent the line that assigns the dictionary value. Make everything up. with open("animal_sounds.json") as j:
  sounds = json.load(j)
cat_sound = sounds["cat"]
^ *with open\(•.+\.json•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n ([a-zA-Z_][a-zA-Z0-9_]*) = json\.load\(\1\)\n[a-zA-Z_][a-zA-Z0-9_]* = \2\[•.+•\] *$
  1. In IDLE code a Python program that imports the json module...
  2. ...reads the JSON file containing a list that you coded in the last chapter...
  3. ...and displays the list.
  4. Save the Python program to the Desktop and run it.
  5. Open the JSON file in IDLE.