Legacy - Naming and Storing Variables Workout
- 04:24
Learn how to name and store variables in PyCharm.
Transcript
Let us now see how to name and store values of variables in PyCharm. In this example, we can either use the Python console or the code editor in which we run the code and the output is shown in this window. But rather than using the Python console, we shall be using the code editor. For the first example, let us create a variable called assets and give it a value of 1000. We shall ignore whether it is a current or a long term asset for this scenario, in order for PyCharm to output the value, we will have to use the print function. But before doing that, in order to run this variable, we have to go to the tab and say run, this has to be done the first time in order to register the tab which are running. Now in order for PyCharm to output the value, we have to use the print function and say assets, the print function prints the value of the variable in the window. Now, we can use this shortcut at the top of the window to run the command. It pastes the value of 1000. Now let us create another variable called liabilities.
And give it a value of 5000. Let us again ignore whether it is a current or a long term liability. And also the side. If we want to print the value of the liability, we're going to use the print function and ask liabilities. If you observe PyCharm has the advantage of auto completion because it registered a variable as liabilities. And in order to run it, we can use the shortcut up top if you observe Shift F 10 is the shortcut of running the command. And again, the value 5000 is pasted at the bottom. Now imagine that there is a addition of $2,000 to the company's assets. So we are going to rename the variable as assets equals assets plus 2000. And we are going to use the shortcut Shift f1. To run it, the code has been executed if we ask PyCharm to print the value of assets, it's going to give it a value of 3000. So when a variable is initialised, or created, a value is stored at the first time, you can use the same variable in other expressions with other variables or values. When a variable is reassigned with a new value, the old value is forgotten. This is the reason the value of the assets is 3000 rather than 1000. This is called overriding the variable. Let's try to do this with a string and see what happens. So if we create a new variable lag, and say that WACC stands for weighted average capital cost and you run the code, but then you realise 'oh no I made a mistake, what should I do?' It's easy. You can rename the variable, say, Okay, PyCharm I made a mistake. It's weighted average cost of capital. And you run the same code using the shortcut Shift f10 simple and now you can try to print work and see what happens. And it says it's weighted average cost of capital at the bottom of the screen, which is right. So we have overwritten the mistake that we made in the first instance. They are also case sensitive, meaning the word WACC with an all uppercase is totally different from WACC with lowercase and similarly with some uppercase and some lowercase. Note that we use the hash sign to commit in Python so when we run the code, the sentence or values following the hash wouldn't be executed as part of the code