One of the most important things to learn first is how to use variables with Python.
And first let's understand why you need variables.
And so I'm starting with a very simple example.
Here we just print the number four okay.
So here we don't have quotes because we can also print a number.
We're going to come back to data types just a bit later.
So we print four and we do this three times okay.
Here the goal is not to focus on the code and logic because yes this could be improved.
But here we are going to focus on variables.
So we print four and then we print four and we print four.
The result is going to be four four and four.
But you can see that's not really convenient because what if I want to change the value instead.
I don't want to print four but I want to print seven.
I will have to modify it three times.
And I just have three lines.
Imagine now that I have this 100 times in the code.
So I run again and it's seven, seven, seven.
And if we just do this well, we are going to make a lot of mistakes.
and as the code becomes bigger, it will be impossible to scale it and it's going to become a real mess.
So what we're going to do is to use variables.
I'm just going to start.
I'm going to create a variable here and then explain how to do it.
So I'm going to write number equals and then seven.
So that's a variable.
And then instead of writing seven here I'm going to write number I'm also going to write number here.
And I'm going to write number here.
So now what will happen if I click on play.
You can see we have 777 okay.
We don't print the word number.
We print the variable number which is equal to seven.
Now if I want to go to let's say I want to change to 12, I run the script again and you see 12, 12,
12.
So I only had to modify one value.
And then it was modified everywhere where I used the variable number.
Okay.
So now let's create a new file.
Let's click on file and you can do a new file here.
We're going to start from an empty program and start from the beginning.
So what is a variable.
Well a variable is basically a container where you store an information to use later.
A variable has a name.
So this is easier for you and your computer to recognize it.
Basically almost all values that you will use in your programs will be stored in variables.
You can give any name you want to a variable.
For example, let's create a new variable here called a just a and that will store a number.
So to create a variable you first need to write.
So on the left you will write the name of the variable.
Then you have an equal sign and then the value.
So for example 45.
Note that I use spaces here.
I don't have to use spaces, but it's much better for readability.
Okay.
So what will happen when Python when the Python interpreter goes to line one, it will see this and
it will first read.
Okay, here's the value 45 that we put inside the variable a.
Okay.
So A becomes 45.
And then after I have created my variable and put a value to it I can print.
So I will use print.
Open parentheses the name of the variable and then close parenthesis.
And you can see here we don't print the letter A.
We're going to print the value 45.
All right so you can click on play.
You can also if you want save the file.
Okay let's name it variables.
Okay.
So for each lesson I'm not necessarily going to do that myself.
But for each lesson you can just create a new file.
You name it with the name of the lesson so that it will be easier for you to follow the course.
So here, for example, I go to my Python programs folder and I name my file variables dot P y.
Always use the p y extension.
Click on okay.
And now my file is viable dot p y.
Great.
So we have created a variable and we print the variable.
So we don't print the variable.
Actually we print the value that is inside the variable.
Now here what we've done is just to say that a is equal to this number.
We can also assign a value with a computation.
With the sign plus minus we can do a multiplication.
We can do a division.
So for example if I say that a is equal to ten times two and once again I use spaces, there is no need.
But it's much better for readability.
So if you had to guess what will be printed here on line two, well, this is going to be 20 simply
because we first evaluate what's here on the right of the equal sign.
This is ten times two.
So it's 20.
And then we assign that to the variable a, and after that we print the variable A.
And note that the order is very important.
So let's say that I'm going to just copy I'm going to cut this and paste it here.
Okay.
So here we print a and then we assign a what's going to happen.
You see we have an error I'm just going to reduce this okay.
We don't really need the assistant.
And you can see that A is not defined.
Okay I have defined A here.
But once again the order of the commands is very important.
So we start on line one here.
There is nothing I could remove it.
Even so we start on line one.
We print a but a has not been defined yet.
Okay A is defined on line two.
So at this point the Python interpreter only knows what has been done before.
So that's why you have to do this in this order.
Okay.
I'm just going to remove that line.
You first declare a valuable and you define it with a value and then you can use it.
Now it's going to work.
And we have an internal I don't know what's that.
It's okay.
You can just ignore you just press again okay.
And you have 20.
After you create a variable you can also modify it.
So let's say do a is equal to three.
And then I print a again.
So what's going to happen here.
First time a is 20.
But then we change the value inside A.
So after line three A is going to be equal to three.
Once again order is very important.
So you can create a variable and you can modify it.
We can also create another variable.
Let's name this one b.
And here instead of just setting a value I'm going to say that b is equal to a.
What does that mean.
It means that on line six, A is going to be evaluated to its current value, which is here three,
and then B is going to be assigned this value.
So B is going to be equal to three.
Let's verify that.
Let's print b and you can see b is equal to three.
I can also use computations for example plus one.
And then b here is equal to four because it's going to be three plus one okay.
And the same thing as I said before, if you try to set B for example before okay you set A you're going
to have an error okay.
Because A is not defined here on line one.
You cannot use A if you have not defined it before.
So I'll put it back here.
And now it's going to work correctly.
All right.
And finally well here we use A and B for names.
This is fine if you just make a quick example.
Or if you do computation with two numbers for example.
But in your programs you are going to use names that are more meaningful.
Okay.
So if you want to store a temperature you're going to write temperature for example is equal to.
I don't know, 25.
If you want to store a counter, you're going to write counter.
Okay.
And maybe start at zero if you want to store a user age.
You're going to write user age like this.
Okay.
For example 30.
And you can note that here we have we have basically two worlds into one.
So I could just write like this user age, but it's more readable if I add an underscore.
One thing for sure is you should not.
So this is not valid to add a space in a variable name, and you would probably get an error anyway.
You see we have a syntax error here.
Okay, so if you want to separate different worlds inside a variable then simply use an underscore.
And with this you have three variables with a meaningful name.
So to recap, a variable is a container that you can use to store a value and reuse that value later
in your program.
To create a variable, you must give it a name and assign a value to it.