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


To find how many dictionaries are in the customers list and load the number into the variable, complete the line. num_of_dicts = ____________ len(customers)^ *len\(customers\) *$
Measure the number of dictionaries in a list and load the number into a variable. Make everything up. num_of_dicts = len(customers)^ *[a-zA-Z_][a-zA-Z0-9_]* = len\([a-zA-Z_][a-zA-Z0-9_]*\) *$
We're adding a new dictionary to a list of dictionaries. The index number of the new dictionary will be the same as the ________ of the list before we add the new dictionary. length length
The length (number of dictionaries) of the customers list will be both the index number and the customer id of the new dictionary. Code the line that finds the length of the customers list and assigns it to the variable new_customer_id. new_customer_id = len(customers)new_customer_id = len(customers)
Find the length of a list and assign it to a variable. Make everything up. new_customer_id = len(customers) ^ *[a-zA-Z_][a-zA-Z0-9_]* = len\([a-zA-Z_][a-zA-Z0-9_]*\) *$
We're creating the new dictionary to add to the list customers. The first key-value pair is "id" and the value stored in new_customer_id. Code the next line. Remember to indent it and add a comma at the end. new_dictionary = {   "id": new_customer_id, ^ * •id•: new_customer_id, *$
Code the next key-value pair in the new dictionary. The key is "last name". The value is stored in the variable new_last_name. Remember to indent the line and end the line with a comma. new_dictionary = {
  "id": new_customer_id,
  "last name": new_last_name, ^ * •last name•: new_last_name, *$
The last pair in the dictionary is "first name" and the value stored in new_first_name. Code the line. Remember to indent and to end the line with a comma. new_dictionary = {
  "id": new_customer_id,
  "last name": new_last_name,
  "first name": new_first_name, ^ * •first name•: new_first_name, *$
All the pairs in the dictionary are now defined. Complete the definition of the new dictionary. new_dictionary = {
  "id": new_customer_id,
  "last name": new_last_name,
  "first name": new_first_name,
} ^ *} *$
You've defined a new dictionary and assigned it to the variable new_dictionary. Now append it to the customers list. customers.append(new_dictionary) ^ *customers\.append\(new_dictionary\) *$
  1. Find the number of elements in the list and assign the number to a variable. Then display the value of the variable.
  2. Click the Run button   above your code. If you've coded correctly, 6 will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
5ba0c4c492
  1. Using the variables new_customer_id, new_first_name and new_last_name, code a new dictionary.
  2. Append the new dictionary to the list
  3. Display the list.
  4. Click the Run button   above your code. If you've coded correctly, list with the new dictionary added to it will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
30fb8f1374