Variables Workout
- 11:36
Practise using variables in Python.
Downloads
Intro to Python Jupyter Notebook FullGlossary
Coding dynamic inputs Jupyter Notebook Python VariablesTranscript
In this exercise, you're going to incorporate everything that you've learned up until this point and put it into one piece of practice. So I encourage you, this is going to be a little bit more challenging than the exercises you've done previously in this lesson, which were pretty easy. And if you run into an issue, I encourage you to go back and review the corresponding part of this module and try and solve the problem yourself before looking at the solution. If you really get stuck, it's totally fine. Go ahead and look at the solution to get you over whatever kind of issue that you're having. But I encourage you first, give it a shot yourself and then if you run into a problem, try reviewing and figuring out the problem because that's going to help you retain this knowledge even more so that you can bring it up and use it when you need to on the job.
In your Jupyter Notebook file, you're going to find some data that I've already provided for you. And this corresponds to data for Apple and for Microsoft, and specifically their market cap preferred debt, their long-term debt, current long-term debt, minority interest, cash equivalents, and EBITDA for the last four quarters.
Your instructions for this exercise are first, calculate enterprise value and LTM EBITDA for both Apple and Microsoft and save as new variables. Second, calculate the enterprise value divided by LTM EBITDA, the enterprise value to EBITDA ratio for both Apple and Microsoft and save those as new variables. The formula that you need are right below for enterprise value and LTM EBITDA. After that, you're going to create a new variable that compares apple's ratio to Microsoft Ratio using the greater than operator. And just like Excel, the value that you get as an output will be either true or false. You're going to use the format function to then generate the following outputs that show Apple's enterprise value, Apple's LTM EBITDA, their enterprise value to EBITDA ratio.
You're gonna show the same three variables for Microsoft. And then finally you're going to create a line that has text and then uses the format function to say Apple's ratio is greater than Microsoft's false. And that false is going to be the variable that you create comparing apple's ratio to Microsoft ratio. This could be a little bit confusing first time, so I'm going to reiterate one more time. You're going to create a new variable and the value of that variable is going to be on one side, Apple's enterprise value to EBITDA ratio. On the other side, you're going to have Microsoft's enterprise value to EBITDA ratio and they're going to be separated by the greater than sign. And now that variable is what you're going to plug in to the format function and it's going to flow through and tell you Apple's ratio is greater than Microsoft's false. That false value is the value of your variable That then flows through the format function function.
Our first step is going to be to execute this code cell. So right here, there's a bunch of data provided for you already. In order for Jupyter Notebook to absorb that data and be able to use it, you first have to execute the co code cell by holding shift and pressing enter.
Now in this code sell below, I'm going to calculate the enterprise value and EBITDA of Apple and Microsoft using the formula provided in the exercise Just for the sake of time, I'm gonna copy this I and pasted it a few times and now I'm just gonna go back in and adjust the number of the quarter and the year.
And the formula for the enterprise value and EBITDA is going to be exactly the same for Microsoft, is just that all of the variables for Microsoft using M instead of an A. So I can copy these two lines of code, paste them down here again, and then replace the A with an M.
Alright? And now that code is ready to execute. So I've defined variables for apple's, enterprise value, Apple's EBITDA, Microsoft's enterprise value, and Microsoft's EBITDA.
The second step is to calculate the enterprise value to EBITDA ratio for both Apple and Microsoft. And that's gonna be a lot easier than typing in all of that stuff that we just did.
All we have to do is create two more variables in this next cell.
And we're dividing the two variables that we already created.
And again, I'm just gonna replace that A with an M for Microsoft.
Now our multiple variables are ready to define. I'm gonna execute that code block.
Moving on to the next step, we're going to create a new variable that compares Apple's enterprise value to EBITDA ratio to Microsoft's ratio using the greater than operator. And just like Excel, this value will be either true or false.
In this next cell, I'm gonna create a variable called compare. And I'm gonna make it equal to the value of that variable is equal to Apple's enterprise value to EBITDA ratio, and I'm going to use greater than Microsoft's enterprise value to EBITDA ratio. So what this gonna do is it's gonna compare the value in these two variables and it's gonna store the result of that comparison inside this new variable called compare. So we know that Apple is going to be either greater than or less than. So that true or false, which is the result of that comparison, saying that Apple is greater than Microsoft's ratio, that true or false is gonna be stored in our compare variable.
Now all we need to do is create an output to match this output in our exercise.
And when we do that, we're gonna use the format function to insert all of the values dynamically.
And here I'm just using the round function to limit the number of decimal places to two. If you didn't do that, totally fine. I'm just doing that to clean up the formatting a little bit in our output. However, the round function is a nice tool to have in your toolbox so that you can control the number of decimal places in your output. Now I'm gonna print a space and then I'm gonna copy that code and do the same thing for Microsoft.
So now I'm gonna print one more space and then the final comparison between Apple and Microsoft.
And note that here we had to escape those apostrophes with a backslash so that Python doesn't interpret that as an end to our block of text. Since we have the backslash, Python knows to interpret those apostrophes as a literal apostrophe within the block of text and to continue that block of text until it sees an apostrophe without the backslash in front of it. Now our code is ready to execute time to see if it matches the solution and the exercise.
Alright? And our output is exactly the same as what we were supposed to get from the exercise.