A Smarter Way to Learn Python / Chapter 33 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 loop through a dictionary getting both its keys and values, what keyword do you use? items^ *items *$
Type the character that's missing in this code. for each_key each_value in products.items(): ,^ *, *$
Complete the line to loop through the dictionary, getting both keys and values. for x, y in z__________.items(): .`items`(`)`:
Code the first line to loop through z to get both keys and values. Assign keys to x and values to y. for x, y in z.items():for x, y in z.items():
Code the first line to loop through keys. Make everything up. for state_or_province in cities.keys(): ^ *for [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*\.keys\(\): *$
Code the first line to loop through values. Make everything up. for state_or_province in cities.values(): ^ *for [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*\.values\(\): *$
Code the first line to loop through a dictionary, getting both keys and values. Make everything up. for city, province_or_state in countries.items(): ^ *for [a-zA-Z_][a-zA-Z0-9_]*, [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*\.items\(\): *$
Loop through a dictionary, getting both keys and values. Display the values. Make everything up. for the_key, the_value in z.items():
  print(the_value)
^ *for [a-zA-Z_][a-zA-Z0-9_]*, ([a-zA-Z_][a-zA-Z0-9_]*) in [a-zA-Z_][a-zA-Z0-9_]*\.items\(\):\n print\(\1\) *$
Loop through a dictionary, getting both keys and values. Display "The key is " and then the key. Remember to include a space at the end of the string. Make everything up. for x, y in z.items():
  print("The key is " + x)
^ *for ([a-zA-Z_][a-zA-Z0-9_]*), [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*\.items\(\):\n print\(•The key is • \+ \1\) *$
Loop through a dictionary, getting both keys and values. If you find an item that has the same key and value, display "weird" and break the loop. Make up the names of everything. for x, y in z.items():
  if x == y:
    print("weird")
    break
^ *for ([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*) in [a-zA-Z_][a-zA-Z0-9_]*\.items\(\):\n if \1 == \2:\n print\(•weird•\)\n break *$
  1. Loop through the dictionary to get both keys and values. Display the values.
  2. Click the Run button   above your code. If you've coded correctly, the four Italian words will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
9c8a6b71df
  1. Loop through the dictionary to get both keys and values. Display:
    The Italian word for one is uno
    The Italian word for two is due
    The Italian word for three is tre
    The Italian word for four is quattro

    Don't forget to include spaces in the strings.
  2. Click the Run button   above your code. If you've coded correctly, four concatenations will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
4db7c6a799