For Loops Workout
- 02:37
Practice using for loops in Python.
Downloads
No associated resources to download.
Transcript
Let's get some more practice with for loops. Let's do the following steps first, just like the example we just completed, write a for loop that calculates ROI for each of the listing companies and appends that ROI to the end of each list. And when you're finished with that, each list should have current price in the first position, target price in the second position, and then your ROI calculation in the third position. And you can check that by printing each list and comparing to the prior example if you need to. Second, create three new empty lists named current prices, target prices, and ROI calculations. Then you're going to write one for loop that iterates through each list in companies and performs the following three actions. It's going to append the first object in each list to current prices. It's going to append the second object to target prices and append the third object to ROI calculations. And then finally, you're going to print those three lists that you just filled up. Current prices, target prices, and ROI calculations. And basically what you're doing here is you're just taking all of your lists in companies and you're splitting up those values. You're putting all of your current prices together, all of your target prices together, and all of your ROI calculations together.
We're going to use some of the stuff that we built in that exercise. The first step is that we're going to calculate ROI and we're going to append the ROI to each list in companies. So here we say, for each ticker in companies colon, we wanna create this new little variable called ROI and give us the rounded value of calculating return on investment for the ticker, each ticker in companies around that value to three decimal places. And once you have that ROI value append it to that ticker list. This list object in companies append the ROI value to that. Next, we're going to initialize these lists, current prices, target prices, and ROI calculations so that we have a place to put these new values that we're going to create in step three. Next we're gonna fill these empty lists using a for loop that says, for each ticker in companies, for each object in companies append the first one, index zero to our list called current prices. Append the second one, index one, to target prices and append the third object, index two, to ROI calculations. And then finally, we're gonna print the new list that we've created and you can see that those values have all been appended to the correct list.