Added links. Renumber exercises

This commit is contained in:
David Beazley
2020-05-26 12:54:10 -05:00
parent e26c630082
commit 887f3c6f77
7 changed files with 123 additions and 86 deletions

View File

@@ -101,7 +101,7 @@ b'Dave has 37 messages'
## Exercises
In the previous exercise, you wrote a program called `report.py` that computed the gain/loss of a
In Exercise 2.7, you wrote a program called `report.py` that computed the gain/loss of a
stock portfolio. In this exercise, you're going to modify it to produce a table like this:
```code
@@ -116,12 +116,15 @@ stock portfolio. In this exercise, you're going to modify it to produce a table
IBM 100 106.28 35.84
```
In this report, "Price" is the current share price of the stock and "Change" is the change in the share price from the initial purchase price.
In this report, "Price" is the current share price of the stock and
"Change" is the change in the share price from the initial purchase
price.
### (a) How to format numbers
### Exercise 2.8: How to format numbers
A common problem with printing numbers is specifying the number of decimal places. One way to fix this is to use f-strings. Try
these examples:
A common problem with printing numbers is specifying the number of
decimal places. One way to fix this is to use f-strings. Try these
examples:
```python
>>> value = 42863.1
@@ -150,7 +153,8 @@ is also sometimes performed using the `%` operator of strings.
>>>
```
Documentation on various codes used with `%` can be found [here](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting).
Documentation on various codes used with `%` can be found
[here](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting).
Although its commonly used with `print`, string formatting is not tied to printing.
If you want to save a formatted string. Just assign it to a variable.
@@ -162,14 +166,15 @@ If you want to save a formatted string. Just assign it to a variable.
>>>
```
### (b) Collecting Data
### Exercise 2.9: Collecting Data
In order to generate the above report, youll first want to collect
all of the data shown in the table. Write a function `make_report()`
that takes a list of stocks and dictionary of prices as input and
returns a list of tuples containing the rows of the above table.
Add this function to your `report.py` file. Heres how it should work if you try it interactively:
Add this function to your `report.py` file. Heres how it should work
if you try it interactively:
```pycon
>>> portfolio = read_portfolio('Data/portfolio.csv')
@@ -187,9 +192,10 @@ Add this function to your `report.py` file. Heres how it should work if you t
>>>
```
### (c) Printing a formatted table
### Exercise 2.10: Printing a formatted table
Redo the above for-loop, but change the print statement to format the tuples.
Redo the for-loop in Exercise 2.9, but change the print statement to
format the tuples.
```pycon
>>> for r in report:
@@ -220,7 +226,7 @@ You can also expand the values and use f-strings. For example:
Take the above statements and add them to your `report.py` program.
Have your program take the output of the `make_report()` function and print a nicely formatted table as shown.
### (d) Adding some headers
### Exercise 2.11: Adding some headers
Suppose you had a tuple of header names like this:
@@ -257,7 +263,7 @@ When youre done, your program should produce the table shown at the top of th
IBM 100 106.28 35.84
```
### (e) Formatting Challenge
### Exercise 2.12: Formatting Challenge
How would you modify your code so that the price includes the currency symbol ($) and the output looks like this:
@@ -273,4 +279,4 @@ How would you modify your code so that the price includes the currency symbol ($
IBM 100 $106.28 35.84
```
[Next](04_Sequences)
[Contents](../Contents) \| [Previous (2.2 Containers)](02_Containers) \| [Next (2.4 Sequences)](04_Sequences)