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


Fill in the blank to assign pid4343's age to the variable. age_of_patient = pid4343_____ .age^ *\.age *$
Fill in the blank to assign sku_101's qty to the variable. qty_on_hand = _____________ sku_101.qty^ *sku_101\.qty *$
Get cushion's color. Assign it to the variable col_cushion. col_cushion = cushion.color col_cushion `= `cushion`.`color
Get the value of qty. Assign it to the variable on_hand. sku_1212 = Product("premium", 144, "instock")on_hand = sku_1212.qtyon_hand = sku_1212.qty
In a single line of code display the value of an attribute for an instance. Make everything up. print(patient0376.condition) ^ *print\([a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]*\) *$
Assign the value of an attribute of an instance to a list element. Make everything up. city_pops[16] = bangkok.population ^ *[a-zA-Z_][a-zA-Z0-9_]*\[-?[0-9]+\] = [a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]* *$
Assign the value of an attribute of an instance to a dictionary item. The dictionary key is a string. Make everything up. australian_cities["capital"] = australia.capital ^ *[a-zA-Z_][a-zA-Z0-9_]*\[•.+•\] = [a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]* *$
If an attribute value of an instance equals another attribute value of that same instance, display a message. Make everything up. if xyz.first_name == xyz.last_name:
  print("Weird!")
^ *if ([a-zA-Z_][a-zA-Z0-9_]*)\.[a-zA-Z_][a-zA-Z0-9_]* == \1\.[a-zA-Z_][a-zA-Z0-9_]*:\n print\(•.+•\) *$
An instance of this class has been created. Assign the value of one of its attributes to a variable. Make everything up, including the name of the instance.   def __init__(self, size, color, fabric): material = sku2020.fabric ^ *[a-zA-Z_][a-zA-Z0-9_]* = [a-zA-Z_][a-zA-Z0-9_]*\.(size|color|fabric) *$
Code a class that has just one attribute, an integer. Code an instance of the class. Then display the value of the instance's attribute. Make everything up. class Student():
  def __init__(self, age):
    self.age = age
id4044 = Student(19)
print(id4044.age)
^ *class ([A-Z][a-zA-Z0-9_]*)\(\):\n def __init__\(self, ([a-zA-Z_][a-zA-Z0-9_]*)\):\n self\.\2 = \2\n([a-zA-Z_][a-zA-Z0-9_]*) = \1\([0-9]+\)\nprint\(\3\.\2\) *$
  1. Code an instance of this class.
  2. Display the value of one of its attributes.
  3. Click the Run button   above your code. If you've coded correctly, the value will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
59796c2784
  1. Code a class that has three attributes.
  2. Code an instance of the class.
  3. Display the value of one of its attributes.
  4. Click the Run button   above your code. If you've coded correctly, the value will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
fed9f70bf2