Appendix F. solutions to the challenges

Chapter 1

No challenges to get started with Python. You win!

Chapter 2

Section 2.1

We start with the following dict object:

product = {"name": "Vacuum", "price": 130.675}

Following is the solution for producing the desired output:

product_tag = f"{product['name']}: {{{product['price']:.2f}}}"
 
assert product_tag == "Vacuum: {130.68}"

Normally, we use curly braces to interpolate variables, so to make them mean the brace symbols themselves instead of interpolations, you need to use {{ to mean the brace symbol itself. Thus, {{{var_name} is interpreted as one left curly brace plus an interpolated string from var_name.

Section 2.2

When we use the input function to collect users’ input, we’re getting strings. ...

Get Python How-To now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.