WEBVTT

00:00.080 --> 00:02.870
This is the solution for the activity number three.

00:02.870 --> 00:08.420
And in this activity you had to compute the max value from a number list.

00:08.420 --> 00:13.010
So as a first step you just write the code here and as a second step with a function.

00:13.010 --> 00:13.820
So let's do that.

00:13.820 --> 00:15.050
Let's start with the first step.

00:15.050 --> 00:16.460
We have our list.

00:16.520 --> 00:22.790
I have already created a variable here max value that I initialize to what I need to initialize it with

00:22.790 --> 00:23.300
something.

00:23.300 --> 00:23.810
So.

00:23.840 --> 00:25.790
So the first element of the list.

00:25.820 --> 00:32.540
Now to be able to find the max value from this list, I will need to basically go through each element.

00:32.570 --> 00:35.090
To go through each element I will use a for loop.

00:35.090 --> 00:39.170
So for number because it's the number list, we use number.

00:39.200 --> 00:44.420
In number list I put a colon and I go to a new line.

00:44.420 --> 00:46.670
Now what do I do with each element.

00:46.670 --> 00:53.630
Well I will simply compare that element with the current value that we have inside the max value variable.

00:53.630 --> 01:03.420
So I can put an if here, and if I find that the number is greater strictly than the max value.

01:04.260 --> 01:07.530
Then it means that I have found a new maximum.

01:07.530 --> 01:12.570
So then I can say that the max value is equal to the number.

01:12.960 --> 01:15.690
And that's how we find our max value.

01:15.690 --> 01:17.730
And that's all the code that you need.

01:17.760 --> 01:17.910
Okay.

01:17.940 --> 01:21.540
So let's go back to this with a bit more explanation.

01:21.540 --> 01:23.430
If you didn't manage to get this.

01:23.430 --> 01:25.110
So we go through each element.

01:25.110 --> 01:27.240
And so for example for the first element.

01:27.240 --> 01:30.120
So we're going to go to the first element we're going to compare.

01:30.120 --> 01:33.900
If so number variable is going to be equal to that element.

01:33.930 --> 01:37.020
Max value is one at this point okay.

01:37.050 --> 01:39.960
So is one strictly greater than one.

01:39.960 --> 01:40.410
No.

01:40.410 --> 01:41.520
So we don't enter this.

01:41.550 --> 01:46.260
If so for the first element of course that's obvious because it's already this value.

01:46.260 --> 01:49.800
But then we go to the second element which is five.

01:49.800 --> 01:57.120
So is here five strictly greater than one which is the current max value.

01:57.180 --> 01:57.930
Yes.

01:57.930 --> 02:03.150
So in this case we enter the if and we say that the max value is now five.

02:03.180 --> 02:07.800
After this we go back actually to the four because we still have elements in the list.

02:07.830 --> 02:09.900
We go to minus six.

02:09.900 --> 02:14.400
So is minus six strictly greater than five.

02:14.430 --> 02:15.930
Because now max value is five.

02:15.960 --> 02:16.800
Well no.

02:16.800 --> 02:19.680
So we don't enter the if then we go with 12.

02:19.710 --> 02:21.990
Is 12 greater strictly than five?

02:22.020 --> 02:22.560
Yes.

02:22.560 --> 02:24.900
So now the max value is 12.

02:24.900 --> 02:26.700
And finally we do it with seven.

02:26.700 --> 02:29.490
Is seven strictly greater than 12.

02:29.520 --> 02:30.120
No.

02:30.120 --> 02:34.950
So we don't enter the if and after that we exit from the four.

02:34.950 --> 02:41.490
So I'm going to go back to indentation and print the max value.

02:42.030 --> 02:44.010
And let's run that.

02:45.270 --> 02:47.340
And the max value is 12.

02:47.550 --> 02:47.790
Okay.

02:47.820 --> 02:48.870
That's that one.

02:48.900 --> 02:49.260
Okay.

02:49.290 --> 02:54.270
And one thing that you can notice here that I talked about, but here we really do it is that well you

02:54.270 --> 03:01.870
can combine any for and if so, basically any loop and condition, you can combine them inside other

03:01.870 --> 03:02.260
ones.

03:02.290 --> 03:02.500
Okay.

03:02.530 --> 03:04.870
So here we have an if inside the for.

03:04.930 --> 03:09.490
And then you see we have a block of code inside the if you can add another if and for etc..

03:09.520 --> 03:09.700
Okay.

03:09.730 --> 03:13.000
Just make sure that you have the correct indentation.

03:13.000 --> 03:20.920
Because if for example that one is here, then that line is not in the if it's going to be directly

03:20.920 --> 03:22.540
executed in the for.

03:22.570 --> 03:25.090
So you want that to be inside the if.

03:25.210 --> 03:25.420
Okay.

03:25.450 --> 03:30.160
So for each new block of code inside the loop or condition you add a new indentation.

03:30.160 --> 03:33.550
So here we have four spaces plus four spaces.

03:33.580 --> 03:34.030
Great.

03:34.030 --> 03:37.420
And now what we can do is create a function.

03:37.420 --> 03:41.980
Because what we did here is we got the max value from a number list.

03:41.980 --> 03:45.370
But what if you want to do that again with another list.

03:45.400 --> 03:45.610
Okay.

03:45.640 --> 03:46.180
In your program.

03:46.180 --> 03:50.020
So what if you want to do this several times we can create a function to do that.

03:50.020 --> 03:56.510
So I'm gonna I'm gonna write stuff at the beginning of the file here because you should first define

03:56.510 --> 03:57.800
a function and then use it.

03:57.800 --> 03:58.490
So let's do that.

03:58.490 --> 04:02.420
At the beginning of the program I'm going to use the def keyword.

04:02.420 --> 04:04.070
And then what do we want to do.

04:04.100 --> 04:07.820
Well we want to get the max value from a number list.

04:07.820 --> 04:15.560
So let's just write get max value from number list.

04:16.190 --> 04:20.120
It's not the smallest name, but it's very explicit.

04:20.120 --> 04:26.990
And I prefer to write explicit names than try to find very short combination of words or letters that

04:26.990 --> 04:28.490
in the end are confusing.

04:28.490 --> 04:33.350
So get max value from number list and we get a number list as a parameter.

04:33.350 --> 04:36.530
That's going to be the list that we're going to work with.

04:36.530 --> 04:38.930
So I put the colon and then I go back to a new line.

04:38.930 --> 04:40.400
I have an indentation.

04:40.400 --> 04:42.890
And what do I do in this function.

04:42.890 --> 04:44.810
Well I do exactly what I did here.

04:44.810 --> 04:48.110
I need to go through this number list.

04:48.110 --> 04:53.820
So first I have to put a max value and then go through all the elements of the number list, find the

04:53.820 --> 04:55.530
max value and return it.

04:55.560 --> 04:58.320
So I'm just going to copy this code here.

04:59.670 --> 05:03.570
So copy and paste inside the function.

05:03.990 --> 05:07.080
So for example you can see it's like this.

05:07.080 --> 05:10.350
But because it's in the function I need to add another indentation.

05:10.350 --> 05:12.870
So here I select everything and I press tab.

05:12.870 --> 05:15.060
And I'm just going to remove this space here.

05:15.330 --> 05:18.720
You can add as many spaces as you want but I'm just going to remove the space.

05:18.720 --> 05:24.570
So now you can see that for example this instruction here has four spaces here plus four plus four because

05:24.570 --> 05:26.310
we have one two and three indentation.

05:26.310 --> 05:29.400
So in this function we receive a parameter that's number list.

05:29.400 --> 05:30.990
That's going to be local variable.

05:30.990 --> 05:33.810
Inside this function we set an initial value.

05:33.810 --> 05:35.190
And then we compute.

05:35.220 --> 05:38.850
Well we use this for loop that I just described to get the max value.

05:38.850 --> 05:40.680
But then that's not it.

05:40.710 --> 05:43.530
We need to return this max value.

05:43.530 --> 05:45.360
So I'm going back to a new line.

05:45.360 --> 05:53.350
And I need to go back to indentations here to be On that level and I can do return.

05:53.500 --> 05:55.390
Max value.

05:55.420 --> 05:55.780
Okay.

05:55.810 --> 06:00.730
It's very important to write the return here because if you do that the return is going to be inside

06:00.730 --> 06:01.570
the for loop.

06:01.570 --> 06:07.000
So after we check the first element we are going to already return from the function and not check the

06:07.000 --> 06:08.590
other elements okay.

06:08.620 --> 06:13.750
And if I do this, then as soon as we enter the if we are going to return from the function.

06:13.750 --> 06:20.500
So you have the return exactly at this indentation and also not here because there it will be invalid

06:20.530 --> 06:22.960
because this is the return outside of the function.

06:22.990 --> 06:23.200
Okay.

06:23.230 --> 06:26.650
So exactly here with this indentation.

06:26.680 --> 06:27.220
Great.

06:27.220 --> 06:30.910
And so now let's just remove that code here.

06:30.910 --> 06:32.380
We don't need it anymore.

06:33.070 --> 06:37.570
I have my list and I can do max value is equal to.

06:37.600 --> 06:43.960
And then instead of doing all the computation again I just call the function get max value.

06:43.960 --> 06:47.230
And actually, as I told you before you can use the auto completion.

06:47.230 --> 06:54.130
So here with Tony you press Ctrl space and you have a suggestion, then you can press Tab or enter to

06:54.160 --> 06:55.570
accept this.

06:55.570 --> 06:59.530
And you already have the name and you're going to pass the number list.

07:00.880 --> 07:03.490
Close the parentheses okay.

07:03.730 --> 07:05.650
Let's just remove the space here.

07:05.710 --> 07:07.120
Make it a bit cleaner.

07:07.120 --> 07:08.500
So we create a number list.

07:08.530 --> 07:09.490
We call the function.

07:09.490 --> 07:12.310
We pass the number list and then we print the max value.

07:12.340 --> 07:14.230
Let's run that.

07:14.260 --> 07:16.390
And you can see same behavior.

07:16.420 --> 07:18.010
We have 12.

07:18.340 --> 07:20.560
And note again that here we have a number list.

07:20.560 --> 07:21.820
And here as well.

07:21.850 --> 07:25.690
But this number list is a local variable inside the function.

07:25.900 --> 07:27.700
So that's why we can have it here.

07:27.730 --> 07:28.780
It's not going to be the same.

07:28.780 --> 07:33.250
And we also have a max value variable here and max value variable there okay.

07:33.250 --> 07:37.120
But that one as you see is created inside the function.

07:37.120 --> 07:40.030
So it's also local to this function okay.

07:40.060 --> 07:45.400
So the max value variable is not the same as that one because different scopes.

07:45.430 --> 07:46.000
Great.

07:46.000 --> 07:48.010
And that's the end of this activity.
