00:00Hi, welcome back. Please take a look at this program in here. Do you see any issue with
00:13this program? Well, the program works well. If we run it, we get seven as output, which
00:21is the expected output. So three plus four gives you seven. But there is an issue with
00:26the program. And the issue is that we have no idea what three means and what four means.
00:35So as programmers, we don't know what's going on in here. And that makes this program not
00:42readable. Now let me show you how I will fix that issue right away by using variables.
00:49So I'll delete this internally. And say spent is equal to three.
00:57Donated is equal to four.
01:02Total amount is equal to spent plus donated. And then print total amount. If I execute this program
01:14now, I'll get the same output. So it's seven again. But this time, we as programmers can understand
01:22this program. So we can give this program to some other person, some other programmers, or we can
01:27go back to this program. Later, and we can still understand this program, because now we have names
01:35for the values. These are referred to as variables. So spent donated and total amounts are all variables.
01:45The syntax is pretty simple. So all you have to do is just pick a name from your mind for a variable.
01:53So I chose to have spent as the first variable. So this is the amount of money I spent for purchasing
02:01things, for example. And then this is the amount I donated. And then I calculated the total amount.
02:08So now this program makes more sense. And of course, there are other examples, such as
02:14as items equal to 10. And price equal to two. Total price is equal to items times price. So now there
02:28is this asterisk here, which means multiplication operator.
02:33So again, we have one variable here, two, and three.
02:50Now, there are some rules regarding to the syntax of variables. The first rule is that you shouldn't have
02:57numbers in front of your variables. But you can have numbers in the middle somewhere, or at the end,
03:05but not in front of them. You can use underscores, such as I did in here, but you cannot use spaces. So this
03:15will not work in Python. Which means that if you have variables with more than one word, you should use
03:21an underscore is the best practice to use an underscore. And lastly, you can have as many spaces as you want
03:32around the assignment operator. So that will work. But it is recommended, according to the official Python
03:41style guide, it is recommended to have one space on both sides of the assignment operator to make the code
03:49more readable. So spaces serve only the purpose of readability here. The same goes for these operators,
03:57it's good to have spaces around the operator for readability again. So now our program makes more
04:06sense. We know what's going on in here. We understand the code. And that is one benefit of variables.
04:14There's a second benefit as well. Variables serve as buckets to carry data or objects. So 10 is a data
04:27value, or you can also refer to it as an object. So 10 is an object, 2 is an object. And items is the name
04:36of this object. Or you can say the variable of this object. And so these names, these variables are like buckets,
04:44you can they are used to carry objects around. For example, let's say we placed 10 inside the items bucket.
04:55And then we get that bucket and multiply that bucket with a price bucket, which has two inside. So instead of
05:04writing 10 in here, and two, we actually work on a bucket basis on a variable basis. There's a benefit
05:16for this. Because for example, let's say we decide to change the value of this to three. And that means
05:24items now here will also be three. So three times two, we're going to get six.
05:31That's why we don't want to have objects hard coded. For example, if this was 10, we run this, we would get
05:41the old output 20. So I hope that is clear. Always use variables. And that concludes this lecture about
05:54variables. I did go into a lot of details because variables are used in every single program that
06:01you write. So make sure you understand them. And I'll talk to you in the next video. Thank you.
06:09Hi, welcome back to a new lecture. Previously, we wrote this script, and we can execute it using this
06:17triangle button, and we get six as output. So three times two gives us six. So what we did here is we
06:28wrote the script in a dot py file, and then we send that script to the interpreter. So we send the entire
06:36dot py file to the interpreter. But there is another way to run Python code. That is by having the interpreter
06:47on demand. Let me demonstrate you what I mean by that. So you have to go to the terminal to the command
06:55command line. This is where you execute operating system commands. So I'm on Windows, so I can execute
07:05py space minus three. If you are on Mac or Linux, you will have to write Python 3.10
07:14or Python 3.9 depending on what version of Python you are using. I'll be using py space minus three and
07:22press enter. So that command starts the Python interpreter. Now the interpreter, as I said, is on
07:31demand, which means the interpreter now is waiting for Python code to interpret it. So think of the
07:39previous methods as hiring the interpreter on a contract basis. So you send the interpreter the entire
07:47script, and they send you back the output. But here, the interpreter is an employee, let's say,
07:54they are on site, and they are waiting for your input, they are listening to you. So what you do is
08:00you say items is equal to three, for example, you write code and you press enter. And the interpreter is
08:07listening to you. So it registers the items variable in memory. And you can keep writing codes price is
08:15equal to two, total price is equal to items, times price. And if you print total price,
08:25you get the output six, which is the same output as that. Now you're probably wondering when to use one
08:35way and when to use the other. So should you code in a Python file or should you code in a Python
08:43interactive shell? So this is now known as an interactive shell. So let me answer you that
08:50question. But before I answer you, I want to point out the differences between the two ways of coding.
08:58The biggest difference is that the codes you write in the interactive shell is not saved. So if I exit
09:06this shell now, this code will get lost. And there's no way to save it. And that takes us to the other
09:16point, which is that this way of coding is just for throwaway code. So if you want to just simply test
09:24things out quickly, perhaps you want to do some math operations such as addition, multiplication, etc.
09:33And you are not interested in creating a reusable program, then this interactive shell is fine to
09:42use. It's quicker than having to create a dot py file, and then writing the code there. So I think
09:49I've answered the question, you should use dot py files whenever you want to create reusable programs.
09:57And you use the Python interactive shell for quick one liners. So just to write one line, maybe another
10:04one in here. So math operations. Now, I want to point out a difference between the two also. Now, another
10:13difference you should be aware of is that in the Python interactive shell, you don't need to use a
10:20print function, you can just say total price. And you get the output. No need to print out total price.
10:33However, in a dot py file, you have to be very explicit. So if I remove the print function, and I
10:41execute this program, we will not get any output. So let me execute this program now. But I warn you
10:50that we will get an error. So run the program, this is the error. So what happened here? This code is
10:59fine. But what's happening is that Visual Studio codes is executing this Windows command
11:10and inside the Python shell, the Python shell only accepts Python code, but not Windows or Mac operating
11:22system commands. So this is a Windows command because it is starting it is trying to start the Python
11:32program and it is trying to execute that my program dot py file. So what you need to do is you need to exit
11:41the shell whenever you want to execute a dot py file. So if you are in an interactive Python shell,
11:48and you know you are whenever you see these three arrows, then you want to do exit with parentheses
11:55and press enter. And that will take you back to the operating system shell, the terminal the command line.
12:07That means now you can run your program. So let me press that triangle button. And we didn't get an error.
12:16However, there's no output either, as you can see, because as I pointed out earlier,
12:22we are not printing out anything in here. So what the interpreter does is it just assigns three to items
12:30variable. It does the same with price, it calculates the total price. And then here it does nothing.
12:38So in order to get output, we need to say print the variable run again, and you get six.
12:47If you want to go back to the Python shell again, write the py minus three command. And you go back to the
12:57shell. And you can run code again in here. So I'll go back to the shell. And that's about the Python
13:08interactive shell. Thanks for following.