WEBVTT

00:01.140 --> 00:04.080
Many reasons exist to store a set of numbers.

00:04.080 --> 00:08.940
For example, you will need to keep track of the positions of each character in a game, and you might

00:08.940 --> 00:11.800
want to keep track of a player's high scores as well.

00:11.820 --> 00:19.110
In data visualizations you will almost always work with a sets of numbers such as temperatures, distances,

00:19.110 --> 00:25.040
population size or latitude, longitude, values, among other types of numerical sets.

00:25.050 --> 00:30.990
So lists are ideal for storing sets of numbers, and Python provides a variety of tools to help you

00:30.990 --> 00:34.470
work efficiently with lists of numbers.

00:34.470 --> 00:43.170
So once you understand how to use these tools effectively, your code will work well even when your

00:43.170 --> 00:45.510
list contains millions of items.

00:45.690 --> 00:48.090
So you can also use the range function.

00:48.090 --> 00:52.100
So Python's range function makes it easy to generate a series numbers.

00:52.110 --> 00:57.120
For example, you can use the range function to print a series of numbers like this.

00:57.120 --> 01:03.820
For my value, my value here in range, for example, from 1 to 10.

01:03.820 --> 01:09.280
And here we will print the print my value.

01:09.280 --> 01:12.430
And here you will see us here.

01:12.970 --> 01:19.630
And here you will see range from one, two, three, four, five, six, seven, eight, nine here.

01:19.630 --> 01:26.890
So although this code looks like it should print the numbers from 1 to 10, it doesn't print the number

01:26.890 --> 01:34.030
ten in this example range prints only the numbers from one through ten.

01:34.030 --> 01:39.610
So I want to explain this by drawing something on the screen here like that, for example, this is

01:39.610 --> 01:40.570
Range.

01:42.600 --> 01:43.380
Range.

01:45.240 --> 01:48.090
Here, for example, we wanted variables.

01:48.090 --> 01:57.340
One, two, three, four, five, six, seven, eight, nine, nine.

01:57.510 --> 01:57.770
Oops.

01:58.830 --> 02:00.840
Nine and ten.

02:00.990 --> 02:09.390
Here we when we ask range for a number from 1 to 10, it will give us from here.

02:10.130 --> 02:11.060
And.

02:11.760 --> 02:14.400
From 1 to 9.

02:14.400 --> 02:16.980
So this is the number.

02:17.720 --> 02:19.580
This code will give us here.

02:19.730 --> 02:20.690
So.

02:21.840 --> 02:30.420
As I said in this code, the range prints only numbers from one truth for truth nine.

02:30.780 --> 02:32.250
It's let's run it again.

02:32.250 --> 02:33.390
And here.

02:33.390 --> 02:38.460
So this is the another result of the off by one behavior you will see often in programming languages

02:38.490 --> 02:45.510
and the range function causes Python to start counting at the first value you give it and it stops when

02:45.510 --> 02:49.680
it reaches the second value you provide because it stops at the second value.

02:49.710 --> 02:58.380
The output never contains the end value, which would have been ten in this case to print numbers one

02:58.750 --> 03:01.770
from 1 to 10, you will do this.

03:02.220 --> 03:03.360
Just 11, right?

03:03.360 --> 03:04.950
11 And that's it.

03:04.950 --> 03:05.130
Here.

03:05.130 --> 03:07.770
As you can see, we printed values from 1 to 10.

03:07.770 --> 03:14.550
So if your output is different from what you expect when you are using range, try adjusting your end

03:14.550 --> 03:16.050
value by one.

03:16.050 --> 03:23.230
So you can also pass range only one argument and it will start a sequence of numbers at zero, for example

03:23.230 --> 03:24.220
range.

03:25.110 --> 03:26.010
Range.

03:27.860 --> 03:29.060
Six here.

03:30.930 --> 03:35.460
Will return the numbers from 0 to 6.

03:35.670 --> 03:39.060
So you can also use the range to make a list of numbers.

03:39.060 --> 03:45.090
So if you want to make a list of numbers, you can convert the results of range directly into list using

03:45.090 --> 03:46.320
the list function.

03:46.320 --> 03:54.690
So when you wrap list around the call to range function, the output will be a list of numbers in the

03:54.690 --> 03:56.790
example the previous section.

03:57.600 --> 04:04.950
In the example of previous section, we simply printed out series of numbers and we can use list to

04:04.950 --> 04:07.560
convert that same set of numbers into a list.

04:07.590 --> 04:13.710
So in order to do that, we will do numbers or my special numbers.

04:13.710 --> 04:15.060
My special.

04:16.540 --> 04:18.070
Actual numbers.

04:18.220 --> 04:24.010
Numbers equals list here and range one.

04:25.820 --> 04:29.600
One 6 or 111 here.

04:29.600 --> 04:30.890
So now.

04:32.610 --> 04:38.940
We have to add here and we will add after that, we will print the my special numbers.

04:38.940 --> 04:39.660
That's it.

04:39.690 --> 04:45.360
Here you can see the one, two, three, four, five, six, seven, eight, nine, ten.

04:45.690 --> 04:50.940
So we can also use the range function to tell Python the skip numbers in a given range.

04:50.940 --> 04:57.930
If you pass a third argument to a range here, Python uses that value as a step size when generating

04:57.930 --> 04:58.530
numbers.

04:58.530 --> 05:00.630
For example, here is our list.

05:00.630 --> 05:11.700
The even numbers between 1 and 10 we will do here too, and we will do two here again and here.

05:11.700 --> 05:15.480
And as you can see here, two, four, six, eight and ten.

05:15.480 --> 05:23.790
So in this example, the range function starts with the value two and then adds two to that value.

05:23.790 --> 05:28.380
It adds repeatedly until it reaches or passes the end value.

05:28.380 --> 05:32.310
11 I want to also illustrate this on screen here.

05:32.310 --> 05:38.010
So here we have one, two, three, four, five.

05:40.070 --> 05:43.820
Five, six, seven, eight.

05:44.990 --> 05:45.710
Nine.

05:47.650 --> 05:48.220
Seven.

05:49.010 --> 05:51.080
Eight, nine, ten.

05:51.110 --> 05:51.770
Here.

05:52.220 --> 05:53.630
So here.

05:54.920 --> 05:56.170
What you see is.

05:56.290 --> 06:04.090
So here we will we are seeing that we will start from two and end four from 1011.

06:04.090 --> 06:07.900
But this means ten here, as you as we explained here.

06:07.900 --> 06:10.570
So we will start from the two here.

06:10.570 --> 06:12.820
So we will start first from this.

06:14.190 --> 06:20.310
Two four, because as you can see here, we are passing two in each number.

06:20.310 --> 06:25.410
And then we here we come here and here we go to.

06:27.200 --> 06:27.740
Six.

06:27.740 --> 06:34.130
And here we come, here and here we go to eight, and here we are.

06:34.130 --> 06:37.550
And then we are going to ten.

06:37.550 --> 06:39.650
And this is where our.

06:41.100 --> 06:42.180
Range ends.

06:42.180 --> 06:47.630
So you can create almost any sets of numbers or you want to use the.

06:47.640 --> 06:55.470
So you can also create you can create almost any set of numbers you want to using the range function.

06:55.470 --> 07:00.630
So for example, consider how might make a list of the first ten square numbers.

07:00.630 --> 07:07.530
That is the square of each integers from one through ten in Python, which is two asterisks like that.

07:07.800 --> 07:09.690
So let's actually close this.

07:09.690 --> 07:16.650
So now here's how you might plot the first ten square numbers onto a list.

07:16.650 --> 07:26.070
So here we have my special or my squares here, squares, and here we will create an empty list.

07:26.100 --> 07:32.310
Now we will use the for loop my my value here in range.

07:32.310 --> 07:36.420
And here we will start from 1 to 11.

07:36.960 --> 07:46.890
And here we will use the my squares here equals my value asterisk to add an asterisk here.

07:47.160 --> 07:54.330
And two and here we will use my my ears here that append.

07:55.460 --> 07:56.110
Append.

08:00.080 --> 08:00.500
Yeah.

08:00.500 --> 08:01.760
My value here.

08:03.260 --> 08:03.800
You must.

08:07.970 --> 08:11.120
Here my squares that append.

08:25.790 --> 08:30.320
My square here and it will change with my square equals my value.

08:30.440 --> 08:36.920
62 And here we will use my squares that append the my.

08:38.790 --> 08:39.360
Square.

08:39.750 --> 08:40.710
And that's it.

08:42.300 --> 08:45.660
And after that, of course, we will need to print the squares here.

08:49.040 --> 08:50.510
Here, but outside the for loop.

08:50.510 --> 08:57.920
As you can see here, we are printing outside for loop workers or my spare.

08:59.180 --> 09:00.770
Yeah, that's it.

09:00.800 --> 09:02.600
Now let's run it.

09:03.820 --> 09:12.260
And as you can see here, one, four, nine, 16, 25, 38, 49, 64, 81 and 100.

09:12.280 --> 09:15.730
So we start with an empty list called squares.

09:15.850 --> 09:17.110
Actually, let me here.

09:17.140 --> 09:24.550
So here we start with the empty list called my squares.

09:24.820 --> 09:26.530
Here, let's actually use that marker.

09:26.560 --> 09:29.860
So here we start with the empty list called my squares.

09:29.890 --> 09:35.800
Then we tell Python to loop through each value.

09:36.400 --> 09:38.470
Loop through each value.

09:39.400 --> 09:40.540
From one.

09:41.790 --> 09:43.500
210.

09:43.800 --> 09:48.930
In this case, we read write 11 using the range function.

09:48.930 --> 09:57.300
So inside the loop, the current value is raised to the second power and assigned to the variable.

09:57.720 --> 09:59.220
My square.

09:59.220 --> 09:59.880
Here.

10:00.090 --> 10:06.480
Each new value of square is then appended to my square list here.

10:06.570 --> 10:09.150
As you can see here we are using append here.

10:09.150 --> 10:10.260
So this worked like that.

10:10.260 --> 10:16.740
For example, this is our my squares here and here we added and then another loop comes.

10:16.740 --> 10:20.310
We added another value and then another look loop comes.

10:20.310 --> 10:25.230
We added another value inside it and until the ten times here.

10:25.950 --> 10:27.840
So that's it.

10:28.110 --> 10:36.840
And finally, when the loop has finished running, the list of squares is printed on the screen.
