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


Code the next line. Begin by indenting it two tabs. class Student():
  def __init__(self, name, age):
    self.name = name
    self.age = age^ * self\.age = age *$
Code the next two lines. Remember to indent. class Product():
  def __init__(self, color, size):
    self.color = color
    self.size = size  
^ * self\.color = color\n self\.size = size *$
These are lines 3 and 4 of a class definition. Code line 2.     self.population = population
    self.area = area
  def __init__(self, population, area): def `_`_`init`_`_`(`self`, `population`, `area`)`:
Code the second line of a class definition. It has three attributes—height, weight, and gender. Start by indenting.   def __init__(self, height, weight, gender): def __init__(self, height, weight, gender):
Code the second and third lines of a class definition with one attribute. Make everything up. Remember to indent.   def __init__(self, location):
    self.location = location
^ * def __init__\(self, ([a-zA-Z_][a-zA-Z0-9_]*)\):\n self\.\1 = \1 *$
Code an instance of a class that has a single attribute. Its value is a string. Make everything up. sku_569 = Product("hammer") ^ *[a-zA-Z_][a-zA-Z0-9_]* = [A-Z][a-zA-Z0-9_]*\(•.+•\) *$
Code a class with two attributes. Make everything up. class Person():
  def __init__(self, nationality, age):
    self.nationality = nationality
    self.age = age
^ *class [A-Z][a-zA-Z0-9_]*\(\):\n def __init__\(self, ([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*)\):\n self\.\1 = \1\n self\.\2 = \2 *$
Code another class with three attributes. Make everything up. class City():
  def __init__(self, nickname, population, mayor):
    self.nickname = nickname
    self.population = population
    self.mayor = mayor
^ *class [A-Z][a-zA-Z0-9_]*\(\):\n def __init__\(self, ([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*)\):\n self\.\1 = \1\n self\.\2 = \2\n self\.\3 = \3 *$
Code an instance of a class with three attributes. The first attribute is a string. The second attribute is an integer. The third attribute is a variable. Make everything up. city956 = City("The City Different", 84000, name) ^ *[a-zA-Z_][a-zA-Z0-9_]* = [A-Z][a-zA-Z0-9_]*\(•.+•, -?[0-9]+, [a-zA-Z_][a-zA-Z0-9_]*\) *$
Code a class that has two attributes. Then code an instance of the class. Both attributes are strings. Make everything up. class Animal():
  def __init__(self, family, diet):
    self.family = family
    self.diet = diet
hummingbird = Animal("Trochilidae", "nectar")
^ *class ([A-Z][a-zA-Z0-9_]*)\(\):\n def __init__\(self, ([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*)\):\n self\.\2 = \2\n self\.\3 = \3\n[a-zA-Z_][a-zA-Z0-9_]* = \1\(•.+•, •.+•\) *$
  1. Code a class that has two attributes
  2. Display the class.
  3. Click the Run button   above your code. If you've coded correctly, <\class '__main__.Performer'> with the name of your class instead of Performer will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
c0be73d90c
  1. Code a class with two attributes
  2. Code two instances of the class
  3. Display the first instance.
  4. Display the second instance.
  5. Click the Run button   above your code. If you've coded correctly, something like <__main__.Performer object at 0x7ff626bd6588><__main__.Performer object at 0x7ff626bd6710> will display in the right-hand panel. But it will be your class name instead of Performer. (You can click Instructions at the top of the right-hand panel to see the correct code.)
09c053b342