A Smarter Way to Learn Python / Chapter 46 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 add a parameter that receives optional positional arguments. Make up the name. def do_something(school, state, ________): *rank^ *\*[a-zA-Z_][a-zA-Z0-9_]* *$
Fill in the blank to add a parameter that receives optional keyword arguments. Make up the name. def do_something(school, state, ________): **rank^ *\*\*[a-zA-Z_][a-zA-Z0-9_]* *$
Call the function sort_out. Pass a keyword argument. The key is dict. The value is a variable, products. sort_out(dict = products) sort_out`(`dict `= `products`)
Code the first line of a function definition, analyze. qty is the parameter. 0 is the default. def analyze(qty = 0):def analyze(qty = 0):
This is the first line of a function definition. When optional arguments are passed, they're assigned to x, which is a ____________. def fill_dictionary(new_customer, first_name, last_name, **x): dictionary ^ *dictionary *$
Code the first line of a function definition. It has a single parameter that receives optional positional arguments from the calling code. Make up the function name and the parameter name. def calc(*numbers): ^ *def [a-zA-Z_][a-zA-Z0-9_]*\(\*[a-zA-Z_][a-zA-Z0-9_]*\): *$
The fourth and last parameter holds optional keyword arguments that are passed by the calling code. Complete the line. Make up the argument name. def a(b, c, d = 1, _________ **more_details): ^ *\*\*[a-zA-Z_][a-zA-Z0-9_]*\): *$
Code the first line of a function. Its only parameter holds optional keyword arguments. Make everything up. def maybe_maybe_maybe_not(**optional_info): ^ *def [a-zA-Z_][a-zA-Z0-9_]*\(\*\*[a-zA-Z_][a-zA-Z0-9_]*\): *$
I've coded the first line of a function. Tell Python to display each value in turn on a separate line. Start by displaying the value of the first parameter. The second line that you'll code is
  for value in extras.values():
Indent correctly.
def show_things(name, **extras):   print(name)
  for value in extras.values():
    print(value)
^ * print\(name\)\n for value in extras\.values\(\):\n print\(value\) *$
Try it again, without help: I've coded the first line of a function. Tell Python to display each value in turn on a separate line. Make up the name of the variable that holds each value in the dictionary. Remember to indent. def show_things(name, **extras):   print(name)
  for each_optional_name in extras.values():
    print(each_optional_name)
^ * print\(name\)\n for ([a-zA-Z_][a-zA-Z0-9_]*) in extras\.values\(\):\n print\(\1\) *$
  1. Code a function with only one parameter, one that handles optional keyword arguments. The function prints each optional value in turn.
  2. Call the function, providing two keyword arguments whose values are strings. Make everything up.
  3. Click the Run button   above your code. If you've coded correctly, the two values you passed to the function will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
9f5c63b7f8
  1. I've coded an empty list. Code a function that fills the list
  2. In the function definition include three parameters—two parameters that accept keyword arguments and a third parameter that accepts optional keyword arguments.
  3. Append each of the first two parameters to the list. Then loop through the dictionary of optional values and append each of those values to the list. Display the list.
  4. Call the function, providing four keyword arguments.
  5. Click the Run button   above your code. If you've coded correctly, the list containing four elements will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
ccae36e27b