Adding Arguments Workout
- 03:07
Practice adding arguments in Python.
Downloads
No associated resources to download.
Transcript
In your Jupyter Notebook file, you're going to see the following price list in the format, current price in the first position, and target price in the second position.
After you run that code cell, to create your list, complete the following steps. First, define a new function called calculate return on investment. And this function should accept one argument, call that argument price list. The function should return the result of the following formula, target price minus current price divided by current price. Third, create a list called companies that contains all of the price lists defined in the first cell. Finally, you're going to write a for loop that iterates through all of the objects and companies applies calculate return on investment to each object, rounds the result of each calculation to three decimal places, and then prints the rounded result of each calculation. In the Jupyter Notebook, we have these list defined with our current price and our target price. So to define those, let's execute this first cell. And now we're going to define a new function called calculate return on investment. So I'm going to define, calculate return on investment, and it's going to accept one argument called price list, then my colon, and now I'm gonna tell it what to do. I want it to return my target price minus my current price divided by the current price. So return, and looking at this, these lists based on the, the way that these lists are structured, I know my current price is in the first position, so I'm going to say price list index 0 or rather price less price list index one is going to give me the target minus price list index zero is going to be the current price. And I want to divide that by my current price. Price list index zero. And I'm going to execute that and I'm going to add a new cell. And now I'm going to create this new list called companies that's going to contain all of the companies in that first cell. So now I have an Iterable object, this list called Companies that contains all of these lists for each of my companies with their current price and target price. And since they're contained in an iterable, now I can write a four loop to apply calculate return on investment to all of those lists. So I'm going to say for every ticker, every object in the list companies I want you to print. And we're gonna round this value to three decimal places. So I want you to print the rounded value after calculating my return on investment for that ticker, that company. And here I'm gonna tell the round function I want three decimal places, and then I can execute that code cell and it's going to give me the ROI values for all six of those lists.