WEBVTT

00:00.080 --> 00:04.880
One of the most important things to learn first is how to use variables with Python.

00:04.880 --> 00:08.240
And first let's understand why you need variables.

00:08.270 --> 00:10.730
And so I'm starting with a very simple example.

00:10.760 --> 00:14.360
Here we just print the number four okay.

00:14.360 --> 00:17.660
So here we don't have quotes because we can also print a number.

00:17.660 --> 00:20.390
We're going to come back to data types just a bit later.

00:20.390 --> 00:23.960
So we print four and we do this three times okay.

00:24.170 --> 00:28.940
Here the goal is not to focus on the code and logic because yes this could be improved.

00:29.000 --> 00:31.970
But here we are going to focus on variables.

00:31.970 --> 00:34.700
So we print four and then we print four and we print four.

00:34.730 --> 00:38.210
The result is going to be four four and four.

00:38.240 --> 00:43.430
But you can see that's not really convenient because what if I want to change the value instead.

00:43.430 --> 00:46.250
I don't want to print four but I want to print seven.

00:46.280 --> 00:49.100
I will have to modify it three times.

00:49.130 --> 00:50.480
And I just have three lines.

00:50.510 --> 00:54.380
Imagine now that I have this 100 times in the code.

00:54.380 --> 00:57.170
So I run again and it's seven, seven, seven.

00:57.170 --> 01:01.460
And if we just do this well, we are going to make a lot of mistakes.

01:01.470 --> 01:08.160
and as the code becomes bigger, it will be impossible to scale it and it's going to become a real mess.

01:08.160 --> 01:10.980
So what we're going to do is to use variables.

01:10.980 --> 01:12.600
I'm just going to start.

01:12.690 --> 01:16.380
I'm going to create a variable here and then explain how to do it.

01:16.380 --> 01:22.260
So I'm going to write number equals and then seven.

01:22.260 --> 01:23.730
So that's a variable.

01:23.730 --> 01:30.930
And then instead of writing seven here I'm going to write number I'm also going to write number here.

01:31.080 --> 01:33.900
And I'm going to write number here.

01:33.900 --> 01:37.350
So now what will happen if I click on play.

01:37.380 --> 01:39.930
You can see we have 777 okay.

01:39.960 --> 01:42.510
We don't print the word number.

01:42.510 --> 01:45.840
We print the variable number which is equal to seven.

01:45.870 --> 01:52.800
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,

01:52.830 --> 01:53.190
12.

01:53.190 --> 01:56.340
So I only had to modify one value.

01:56.340 --> 02:01.080
And then it was modified everywhere where I used the variable number.

02:01.110 --> 02:01.680
Okay.

02:01.680 --> 02:04.040
So now let's create a new file.

02:04.400 --> 02:08.540
Let's click on file and you can do a new file here.

02:08.570 --> 02:12.380
We're going to start from an empty program and start from the beginning.

02:12.380 --> 02:14.450
So what is a variable.

02:14.480 --> 02:20.510
Well a variable is basically a container where you store an information to use later.

02:20.540 --> 02:21.950
A variable has a name.

02:21.950 --> 02:25.580
So this is easier for you and your computer to recognize it.

02:25.790 --> 02:31.490
Basically almost all values that you will use in your programs will be stored in variables.

02:31.490 --> 02:33.740
You can give any name you want to a variable.

02:33.740 --> 02:40.670
For example, let's create a new variable here called a just a and that will store a number.

02:40.670 --> 02:43.760
So to create a variable you first need to write.

02:43.790 --> 02:47.600
So on the left you will write the name of the variable.

02:47.600 --> 02:51.170
Then you have an equal sign and then the value.

02:51.200 --> 02:53.120
So for example 45.

02:53.150 --> 02:55.370
Note that I use spaces here.

02:55.370 --> 03:00.020
I don't have to use spaces, but it's much better for readability.

03:00.050 --> 03:00.320
Okay.

03:00.410 --> 03:06.810
So what will happen when Python when the Python interpreter goes to line one, it will see this and

03:06.810 --> 03:08.610
it will first read.

03:08.640 --> 03:13.590
Okay, here's the value 45 that we put inside the variable a.

03:13.620 --> 03:13.890
Okay.

03:13.920 --> 03:15.870
So A becomes 45.

03:15.870 --> 03:21.570
And then after I have created my variable and put a value to it I can print.

03:21.570 --> 03:23.130
So I will use print.

03:23.160 --> 03:28.350
Open parentheses the name of the variable and then close parenthesis.

03:28.350 --> 03:32.340
And you can see here we don't print the letter A.

03:32.370 --> 03:35.640
We're going to print the value 45.

03:35.670 --> 03:38.010
All right so you can click on play.

03:38.040 --> 03:41.640
You can also if you want save the file.

03:42.390 --> 03:44.910
Okay let's name it variables.

03:44.970 --> 03:45.390
Okay.

03:45.390 --> 03:48.180
So for each lesson I'm not necessarily going to do that myself.

03:48.180 --> 03:51.480
But for each lesson you can just create a new file.

03:51.510 --> 03:56.340
You name it with the name of the lesson so that it will be easier for you to follow the course.

03:56.340 --> 04:04.790
So here, for example, I go to my Python programs folder and I name my file variables dot P y.

04:04.820 --> 04:07.370
Always use the p y extension.

04:07.430 --> 04:08.570
Click on okay.

04:08.570 --> 04:11.360
And now my file is viable dot p y.

04:11.390 --> 04:11.780
Great.

04:11.780 --> 04:15.740
So we have created a variable and we print the variable.

04:15.740 --> 04:16.880
So we don't print the variable.

04:16.910 --> 04:21.470
Actually we print the value that is inside the variable.

04:21.470 --> 04:27.110
Now here what we've done is just to say that a is equal to this number.

04:27.110 --> 04:30.920
We can also assign a value with a computation.

04:30.920 --> 04:34.760
With the sign plus minus we can do a multiplication.

04:34.760 --> 04:36.350
We can do a division.

04:36.350 --> 04:45.620
So for example if I say that a is equal to ten times two and once again I use spaces, there is no need.

04:45.620 --> 04:47.900
But it's much better for readability.

04:47.900 --> 04:55.040
So if you had to guess what will be printed here on line two, well, this is going to be 20 simply

04:55.040 --> 05:00.020
because we first evaluate what's here on the right of the equal sign.

05:00.020 --> 05:01.790
This is ten times two.

05:01.820 --> 05:02.870
So it's 20.

05:02.870 --> 05:08.400
And then we assign that to the variable a, and after that we print the variable A.

05:08.400 --> 05:10.620
And note that the order is very important.

05:10.620 --> 05:15.780
So let's say that I'm going to just copy I'm going to cut this and paste it here.

05:15.810 --> 05:16.380
Okay.

05:16.380 --> 05:20.430
So here we print a and then we assign a what's going to happen.

05:21.240 --> 05:26.040
You see we have an error I'm just going to reduce this okay.

05:26.280 --> 05:28.170
We don't really need the assistant.

05:28.170 --> 05:31.020
And you can see that A is not defined.

05:31.050 --> 05:32.850
Okay I have defined A here.

05:32.880 --> 05:38.850
But once again the order of the commands is very important.

05:38.850 --> 05:40.560
So we start on line one here.

05:40.560 --> 05:42.030
There is nothing I could remove it.

05:42.030 --> 05:44.460
Even so we start on line one.

05:44.460 --> 05:48.900
We print a but a has not been defined yet.

05:48.930 --> 05:51.000
Okay A is defined on line two.

05:51.030 --> 05:57.150
So at this point the Python interpreter only knows what has been done before.

05:57.150 --> 06:00.690
So that's why you have to do this in this order.

06:00.990 --> 06:01.290
Okay.

06:01.320 --> 06:02.820
I'm just going to remove that line.

06:02.940 --> 06:09.620
You first declare a valuable and you define it with a value and then you can use it.

06:09.650 --> 06:11.480
Now it's going to work.

06:11.480 --> 06:14.930
And we have an internal I don't know what's that.

06:15.170 --> 06:15.800
It's okay.

06:15.800 --> 06:18.500
You can just ignore you just press again okay.

06:18.530 --> 06:19.520
And you have 20.

06:19.550 --> 06:21.740
After you create a variable you can also modify it.

06:21.740 --> 06:24.740
So let's say do a is equal to three.

06:25.280 --> 06:27.860
And then I print a again.

06:28.130 --> 06:30.170
So what's going to happen here.

06:30.200 --> 06:31.670
First time a is 20.

06:31.670 --> 06:33.740
But then we change the value inside A.

06:33.770 --> 06:37.940
So after line three A is going to be equal to three.

06:37.970 --> 06:40.220
Once again order is very important.

06:40.220 --> 06:43.220
So you can create a variable and you can modify it.

06:43.250 --> 06:45.140
We can also create another variable.

06:45.140 --> 06:46.910
Let's name this one b.

06:47.330 --> 06:53.030
And here instead of just setting a value I'm going to say that b is equal to a.

06:53.060 --> 06:54.410
What does that mean.

06:54.410 --> 07:01.700
It means that on line six, A is going to be evaluated to its current value, which is here three,

07:01.700 --> 07:04.310
and then B is going to be assigned this value.

07:04.310 --> 07:06.810
So B is going to be equal to three.

07:06.840 --> 07:08.640
Let's verify that.

07:08.670 --> 07:13.740
Let's print b and you can see b is equal to three.

07:13.770 --> 07:17.220
I can also use computations for example plus one.

07:17.430 --> 07:22.470
And then b here is equal to four because it's going to be three plus one okay.

07:22.500 --> 07:33.150
And the same thing as I said before, if you try to set B for example before okay you set A you're going

07:33.180 --> 07:34.560
to have an error okay.

07:34.590 --> 07:37.440
Because A is not defined here on line one.

07:37.470 --> 07:41.100
You cannot use A if you have not defined it before.

07:41.940 --> 07:43.620
So I'll put it back here.

07:43.620 --> 07:45.750
And now it's going to work correctly.

07:45.780 --> 07:46.230
All right.

07:46.230 --> 07:49.470
And finally well here we use A and B for names.

07:49.470 --> 07:52.650
This is fine if you just make a quick example.

07:52.710 --> 07:54.930
Or if you do computation with two numbers for example.

07:54.930 --> 07:59.010
But in your programs you are going to use names that are more meaningful.

07:59.040 --> 07:59.520
Okay.

07:59.520 --> 08:07.880
So if you want to store a temperature you're going to write temperature for example is equal to.

08:07.910 --> 08:09.170
I don't know, 25.

08:10.040 --> 08:13.310
If you want to store a counter, you're going to write counter.

08:13.430 --> 08:14.000
Okay.

08:14.180 --> 08:19.160
And maybe start at zero if you want to store a user age.

08:19.190 --> 08:21.590
You're going to write user age like this.

08:22.220 --> 08:22.490
Okay.

08:22.520 --> 08:23.810
For example 30.

08:23.810 --> 08:29.270
And you can note that here we have we have basically two worlds into one.

08:29.270 --> 08:34.670
So I could just write like this user age, but it's more readable if I add an underscore.

08:34.670 --> 08:36.560
One thing for sure is you should not.

08:36.560 --> 08:43.100
So this is not valid to add a space in a variable name, and you would probably get an error anyway.

08:43.100 --> 08:45.320
You see we have a syntax error here.

08:46.580 --> 08:52.880
Okay, so if you want to separate different worlds inside a variable then simply use an underscore.

08:52.880 --> 08:56.600
And with this you have three variables with a meaningful name.

08:56.600 --> 09:03.710
So to recap, a variable is a container that you can use to store a value and reuse that value later

09:03.710 --> 09:04.880
in your program.

09:04.880 --> 09:09.290
To create a variable, you must give it a name and assign a value to it.
