Conditional Logic Workout
- 03:44
Practice using conditional logic in Python.
Downloads
Glossary
Conditional Logic PythonTranscript
Let's get some practice with conditional logics. Go to your Jupyter Notebook and you're gonna find the following ROI dictionary containing the ROI calculations from our previous exercise. Execute that cell to define the dictionary, then complete the following steps. First, create three empty lists named buy recommendations, hold recommendations, and sell recommendations. Then write a for loop that cycles through all of the keys in ROI dict and completes the following actions. First, if the ROI is greater than 10% append the key to buy recommendations. If the ROI is less than 10%, but greater than 5%, append the key to hold recommendations. And if the ROI is less than 5% append the key to cell recommendations. And remember to get the keys, just write the name of the dictionary and then dot keys and that will create an iterable of the keys in the dictionary.
We're gonna start by executing this code block to create ROI dict.
And then we're going to initialize our lists. Next we're gonna create our for loop. So for every key in roi, dick dot keys. And so that is gonna give us an iterable and in fact, I'm gonna add a cell right here just to show you what that looks like. Since we're still kind of new to dictionaries, roi. Dick dot keys you'll remember gives me this list of the keys for all of the items in ROI Dict. So that is the iterable that we're going to be cycling through. So here I have four key in ROI, dict keys, and then my colon. And now I wanna look at the value in that dictionary and compare it. I wanna create conditions for the values that are correlated to that key. So I'm gonna say if ROI dicked and then in square brackets key. So what's gonna happen here is it's gonna take whatever this key is that it gets from ROI, diced keys. It's gonna plug that into these square brackets right here. And as you know, so I'm gonna add another one here too.
For example, if I was going to say ROI dict and then j and j, then that is going to give me the value in the item with the key j and j. So that's what's happening in our, in our for loop down here. So I say if ROI, dict key is greater than 0.1, then I want to append it to buy recommendations. So I'm gonna say buy recommendations, append.
Key else if ROI DIC key. So if that value is greater than 0.05, so we've already, we've already sorted out everything that's greater than 10%. So everything that's left that's going to hit this second condition is guaranteed to be less than 10%. So I'm saying else if it's still greater than 5%, then I want to append it to my hold recommendations. So hold recommendations, append key, and then I have my final L statement. Everything that's left, I want to append to my cell recommendations. So cell recommendations, append key. Finally, I wanna print all three of my lists and when I execute that we'll see that these were sorted out correctly.