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


The last key-value pair in the dictionary is this list. What is the name of the list? "discounts": ["standard", "volume", "loyalty"], "discounts"^ *•discounts• *$
What is the element in the list with an index number of 1? "discounts": ["standard", "volume", "loyalty"], "volume"^ *•volume• *$
Fill in the blank to specify the last element of the list, "loyalty"—and close the list. Remember to end the line with a comma. "discounts": ["standard", "volume", _____________"loyalty"], "`loyalty`"`]`,
Extend this dictionary by coding the next key-value pair. It's a list named "a list". The elements are the integers 1, 2, 3. Remember to indent and to end the line with a comma. dict = {
  "nation": "Uganda",
  "a list": [1, 2, 3], "a list": [1, 2, 3],
Code a dictionary that contains just one item. The item is a list that contains just one element. The key is a string. The single element in the list is an integer. Make everything up. Remember to end the list definition with a comma. x = {
  "count": [788],
} 
^ *[a-zA-Z_][a-zA-Z0-9_]* = {\n •.+•: \[-?[0-9]+\],\n} *$
Code a dictionary that contains just one item. The item is a list that contains two elements. The key is a string. The elements in the list are strings. Make everything up. Remember to end the list definition with a comma. x = {
  "names": ["Don", "Dean"],
}
^ *[a-zA-Z_][a-zA-Z0-9_]* = {\n •.+•: \[•.+•, •.+•\],\n} *$
Code a dictionary that contains two items. The first item is key-value pair. The key and the value are both integers. The second item is a list that contains two elements. The key is an integer. The elements in the list are also integers. Make everything up. Remember to end the list definition with a comma. x = {
  3: 4,
  5: [6, 7],
}
^ *[a-zA-Z_][a-zA-Z0-9_]* = {\n -?[0-9]+: -?[0-9]+,\n -?[0-9]+: \[-?[0-9]+, -?[0-9]+\],\n} *$
Code a dictionary that contains two items. Both items are lists. Each list contains two elements. All keys, values, and list items are strings. Make everything up. x = {
  "flowers": ["rose", "daffodil"],
  "trees": ["palm", "fir"],
}
^ *[a-zA-Z_][a-zA-Z0-9_]* = {\n •.+•: \[•.+•, •.+•\],\n •.+•: \[•.+•, •.+•\],\n} *$
Code a list that contains one dictionary. The dictionary contains one pair. The key is a string. the value is an integer. Make everything up. You'll need two commas: one after the pair, and one after the closing curly bracket. tiny_list = [
  {
    "universe": 1,
  },
]
^ *[a-zA-Z_][a-zA-Z0-9_]* = \[\n {\n •.+•: -?[0-9]+,\n },\n\] *$
Code a dictionary that contains two lists. The keys are strings. Each list contains three integers. Make everything up. dict = {
  "some numbers": [1, 2, 3],
  "other numbers": [4, 5, 6],
}
^ *[a-zA-Z_][a-zA-Z0-9_]* = {\n •.+•: \[-?[0-9]+, -?[0-9]+, -?[0-9]+\],\n •.+•: \[-?[0-9]+, -?[0-9]+, -?[0-9]+\],\n} *$
  1. Complete the dictionary by adding a list containing three numbers. (Don't forget to close the dictionary with a curly bracket after adding the list.) Then display the dictionary.
  2. Click the Run button   above your code. If you've coded correctly, the dictionary including the list will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
124c57b0df
  1. Code a dictionary containing two lists. The first list contains all strings. The second list contains all numbers
  2. Display the dictionary.
  3. Click the Run button   above your code. If you've coded correctly, the dictionary containing two lists will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
7c8c89614e