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


In Python a ______ is a template for organizing multiple instances of information, similar to a paper form. class^ *class *$
Rewrite this first line of a class definition so it follows conventional style. class product(): class Product():^ *class Product\(\): *$
Complete this first line of a class definition. class Country____(): (`)`:
Rewrite this first line of a class definition so it's valid. class Customer = ():class Customer():class Customer():
Code the first line of a class definition. The class is named Animal. class Animal(): ^ *class Animal\(\): *$
Code the first line of a class definition. Make up the name. class Patient(): ^ *class [A-Z][a-zA-Z0-9_]*\(\): *$
Code the first two lines of a function. Line 2 begins the definition of a class. Make everything up. def start_a_class_definition():
  class Secret_info():
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(\):\n class [A-Z][a-zA-Z0-9_]*\(\): *$
That's it for this chapter's exercises. The last three questions will review previous chapters. Rewrite the following code using a nested if statement. if a == b and c == d:
  match = True
if a == b:
  if c == d:
    match = True
^ *if a == b:\n if c == d:\n match = True *$
Code a list that contains one dictionary. The dictionary contains one item. Both the key and value are strings. Code it in five lines. Make everything up. relatives = [
  {
    "wife": "Angie",
  },
]
^ *[a-zA-Z_][a-zA-Z0-9_]* = \[\n {\n •.+•: •.+•,?\n },?\n\] *$
In a single line, target the correct list, dictionary, and key to display Leopold. relatives = [
  {
    "wife": "Angie",
  },
  {
    "brother": "Leopold",
  },
  {
    "cousin": "Beltran",
  }, 
]
print(relatives[1]["brother"]) ^ *print\(relatives\[1\]\[•brother•\]\) *$
  1. Code a dictionary whose only item is a list. The list has three elements.
  2. Display the dictionary.
  3. Click the Run button   above your code. If you've coded correctly, the dictionary with the list inside 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.)
9d4bc29a70
  1. I've coded a dictionary containing three lists. And I've assigned a variable, index_number, a value of 0.
  2. Loop through the third list looking for the element False. When the loop gets to the element you're looking for, display the current value of index_number, which will be the element's index number.
  3. Before the loop gets to that element, when the element being checked isn't False, increment index_number. This will be the index number of the next element to be checked in the list.
  4. Click the Run button   above your code. If you've coded correctly, 1 will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
c9430aec28