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


Condense this code to one line, using the function as a variable. result = calc(20, 30)
  print(result)
print(calc(20, 30))^ *print\(calc\(20, 30\)\) *$
Condense this code to one line using the functions as variables. first_result = calc(20, 30)
second_result = calc(40, 50)
total = first_result + second_result
total = calc(20, 30) + calc(40, 50)^ *total = calc\(20, 30\) \+ calc\(40, 50\) *$
In a single line of code, concatenate the value returned by the function greet and the value returned by the function name. Assign the result to the variable whole_thing. whole_thing = greet() + name() whole_thing `= `greet`(`) `+ `name`(`)
Code the first line of an if statement that tests whether the value returned by the function calc is equal to 1. if calc() == 1:if calc() == 1:
Code a two-line function that returns the result of one function divided by another function. The functions have no arguments. Make everything up. def calc():
  return first_operation() / second_operation()
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(\):\n return [a-zA-Z_][a-zA-Z0-9_]*\(\) / [a-zA-Z_][a-zA-Z0-9_]*\(\) *$
Call a function, assigning its returned value to a variable. The function's argument is another function with one positional argument, a string. Make everything up. result = format(abbreviate("Laughing Out Loud")) ^ *[a-zA-Z_][a-zA-Z0-9_]* = [a-zA-Z_][a-zA-Z0-9_]*\([a-zA-Z_][a-zA-Z0-9_]*\(•.+•\)\) *$
In a single line of code, do all this: Concatenate the value returned by one function and the value returned by another function. Assign the concatenated string to a variable. The functions have no arguments. Make everything up. combo = generate_string_1() + generate_string_2() ^ *[a-zA-Z_][a-zA-Z0-9_]* = [a-zA-Z_][a-zA-Z0-9_]*\(\) \+ [a-zA-Z_][a-zA-Z0-9_]*\(\) *$
Code the first line of a for loop. It loops through the result returned by a function. Make everything up. for each_element in create_list(): ^ *for [a-zA-Z_][a-zA-Z0-9_]* in [a-zA-Z_][a-zA-Z0-9_]*\(\): *$
Code a function that loops through a result returned by a function and appends each returned value to a list. The outer function has one positional parameter, the name of the list. Make everything up. def add_to_list(customer_list):
  for a_customer in add_new_customers():
    customer_list.append(a_customer) 
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(([a-zA-Z_][a-zA-Z0-9_]*)\):\n for ([a-zA-Z_][a-zA-Z0-9_]*) in [a-zA-Z_][a-zA-Z0-9_]*\(\):\n \1\.append\(\2\) *$
Code the first line of an if statement that tests whether the result returned by one function is the same as the result returned by another function. Both functions have keyword arguments whose values are variables. if x(y=z) == a(b=c): ^ *if [a-zA-Z_][a-zA-Z0-9_]*\([a-zA-Z_][a-zA-Z0-9_]*=[a-zA-Z_][a-zA-Z0-9_]*\) == [a-zA-Z_][a-zA-Z0-9_]*\([a-zA-Z_][a-zA-Z0-9_]*=[a-zA-Z_][a-zA-Z0-9_]*\): *$
  1. In a single line display the result returned by the first function divided by the result returned by the second function. Provide two numbers as positional arguments for each function. The arguments can be the same for both functions, or different.
  2. Click the Run button   above your code. If you've coded correctly, the result of all the calculations will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
83bf3ac931
  1. Call the function, providing three positional arguments. The first argument is the name of the list, list_of_numbers. The second argument is the the index number of the element to increment. The third argument is a math expression, for example 9 / 3.
  2. Display the changed element.
  3. Click the Run button   above your code. If you've coded correctly, the incremented element will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
49ed293b35