1 00:00:01,140 --> 00:00:04,080 Many reasons exist to store a set of numbers. 2 00:00:04,080 --> 00:00:08,940 For example, you will need to keep track of the positions of each character in a game, and you might 3 00:00:08,940 --> 00:00:11,800 want to keep track of a player's high scores as well. 4 00:00:11,820 --> 00:00:19,110 In data visualizations you will almost always work with a sets of numbers such as temperatures, distances, 5 00:00:19,110 --> 00:00:25,040 population size or latitude, longitude, values, among other types of numerical sets. 6 00:00:25,050 --> 00:00:30,990 So lists are ideal for storing sets of numbers, and Python provides a variety of tools to help you 7 00:00:30,990 --> 00:00:34,470 work efficiently with lists of numbers. 8 00:00:34,470 --> 00:00:43,170 So once you understand how to use these tools effectively, your code will work well even when your 9 00:00:43,170 --> 00:00:45,510 list contains millions of items. 10 00:00:45,690 --> 00:00:48,090 So you can also use the range function. 11 00:00:48,090 --> 00:00:52,100 So Python's range function makes it easy to generate a series numbers. 12 00:00:52,110 --> 00:00:57,120 For example, you can use the range function to print a series of numbers like this. 13 00:00:57,120 --> 00:01:03,820 For my value, my value here in range, for example, from 1 to 10. 14 00:01:03,820 --> 00:01:09,280 And here we will print the print my value. 15 00:01:09,280 --> 00:01:12,430 And here you will see us here. 16 00:01:12,970 --> 00:01:19,630 And here you will see range from one, two, three, four, five, six, seven, eight, nine here. 17 00:01:19,630 --> 00:01:26,890 So although this code looks like it should print the numbers from 1 to 10, it doesn't print the number 18 00:01:26,890 --> 00:01:34,030 ten in this example range prints only the numbers from one through ten. 19 00:01:34,030 --> 00:01:39,610 So I want to explain this by drawing something on the screen here like that, for example, this is 20 00:01:39,610 --> 00:01:40,570 Range. 21 00:01:42,600 --> 00:01:43,380 Range. 22 00:01:45,240 --> 00:01:48,090 Here, for example, we wanted variables. 23 00:01:48,090 --> 00:01:57,340 One, two, three, four, five, six, seven, eight, nine, nine. 24 00:01:57,510 --> 00:01:57,770 Oops. 25 00:01:58,830 --> 00:02:00,840 Nine and ten. 26 00:02:00,990 --> 00:02:09,390 Here we when we ask range for a number from 1 to 10, it will give us from here. 27 00:02:10,130 --> 00:02:11,060 And. 28 00:02:11,760 --> 00:02:14,400 From 1 to 9. 29 00:02:14,400 --> 00:02:16,980 So this is the number. 30 00:02:17,720 --> 00:02:19,580 This code will give us here. 31 00:02:19,730 --> 00:02:20,690 So. 32 00:02:21,840 --> 00:02:30,420 As I said in this code, the range prints only numbers from one truth for truth nine. 33 00:02:30,780 --> 00:02:32,250 It's let's run it again. 34 00:02:32,250 --> 00:02:33,390 And here. 35 00:02:33,390 --> 00:02:38,460 So this is the another result of the off by one behavior you will see often in programming languages 36 00:02:38,490 --> 00:02:45,510 and the range function causes Python to start counting at the first value you give it and it stops when 37 00:02:45,510 --> 00:02:49,680 it reaches the second value you provide because it stops at the second value. 38 00:02:49,710 --> 00:02:58,380 The output never contains the end value, which would have been ten in this case to print numbers one 39 00:02:58,750 --> 00:03:01,770 from 1 to 10, you will do this. 40 00:03:02,220 --> 00:03:03,360 Just 11, right? 41 00:03:03,360 --> 00:03:04,950 11 And that's it. 42 00:03:04,950 --> 00:03:05,130 Here. 43 00:03:05,130 --> 00:03:07,770 As you can see, we printed values from 1 to 10. 44 00:03:07,770 --> 00:03:14,550 So if your output is different from what you expect when you are using range, try adjusting your end 45 00:03:14,550 --> 00:03:16,050 value by one. 46 00:03:16,050 --> 00:03:23,230 So you can also pass range only one argument and it will start a sequence of numbers at zero, for example 47 00:03:23,230 --> 00:03:24,220 range. 48 00:03:25,110 --> 00:03:26,010 Range. 49 00:03:27,860 --> 00:03:29,060 Six here. 50 00:03:30,930 --> 00:03:35,460 Will return the numbers from 0 to 6. 51 00:03:35,670 --> 00:03:39,060 So you can also use the range to make a list of numbers. 52 00:03:39,060 --> 00:03:45,090 So if you want to make a list of numbers, you can convert the results of range directly into list using 53 00:03:45,090 --> 00:03:46,320 the list function. 54 00:03:46,320 --> 00:03:54,690 So when you wrap list around the call to range function, the output will be a list of numbers in the 55 00:03:54,690 --> 00:03:56,790 example the previous section. 56 00:03:57,600 --> 00:04:04,950 In the example of previous section, we simply printed out series of numbers and we can use list to 57 00:04:04,950 --> 00:04:07,560 convert that same set of numbers into a list. 58 00:04:07,590 --> 00:04:13,710 So in order to do that, we will do numbers or my special numbers. 59 00:04:13,710 --> 00:04:15,060 My special. 60 00:04:16,540 --> 00:04:18,070 Actual numbers. 61 00:04:18,220 --> 00:04:24,010 Numbers equals list here and range one. 62 00:04:25,820 --> 00:04:29,600 One 6 or 111 here. 63 00:04:29,600 --> 00:04:30,890 So now. 64 00:04:32,610 --> 00:04:38,940 We have to add here and we will add after that, we will print the my special numbers. 65 00:04:38,940 --> 00:04:39,660 That's it. 66 00:04:39,690 --> 00:04:45,360 Here you can see the one, two, three, four, five, six, seven, eight, nine, ten. 67 00:04:45,690 --> 00:04:50,940 So we can also use the range function to tell Python the skip numbers in a given range. 68 00:04:50,940 --> 00:04:57,930 If you pass a third argument to a range here, Python uses that value as a step size when generating 69 00:04:57,930 --> 00:04:58,530 numbers. 70 00:04:58,530 --> 00:05:00,630 For example, here is our list. 71 00:05:00,630 --> 00:05:11,700 The even numbers between 1 and 10 we will do here too, and we will do two here again and here. 72 00:05:11,700 --> 00:05:15,480 And as you can see here, two, four, six, eight and ten. 73 00:05:15,480 --> 00:05:23,790 So in this example, the range function starts with the value two and then adds two to that value. 74 00:05:23,790 --> 00:05:28,380 It adds repeatedly until it reaches or passes the end value. 75 00:05:28,380 --> 00:05:32,310 11 I want to also illustrate this on screen here. 76 00:05:32,310 --> 00:05:38,010 So here we have one, two, three, four, five. 77 00:05:40,070 --> 00:05:43,820 Five, six, seven, eight. 78 00:05:44,990 --> 00:05:45,710 Nine. 79 00:05:47,650 --> 00:05:48,220 Seven. 80 00:05:49,010 --> 00:05:51,080 Eight, nine, ten. 81 00:05:51,110 --> 00:05:51,770 Here. 82 00:05:52,220 --> 00:05:53,630 So here. 83 00:05:54,920 --> 00:05:56,170 What you see is. 84 00:05:56,290 --> 00:06:04,090 So here we will we are seeing that we will start from two and end four from 1011. 85 00:06:04,090 --> 00:06:07,900 But this means ten here, as you as we explained here. 86 00:06:07,900 --> 00:06:10,570 So we will start from the two here. 87 00:06:10,570 --> 00:06:12,820 So we will start first from this. 88 00:06:14,190 --> 00:06:20,310 Two four, because as you can see here, we are passing two in each number. 89 00:06:20,310 --> 00:06:25,410 And then we here we come here and here we go to. 90 00:06:27,200 --> 00:06:27,740 Six. 91 00:06:27,740 --> 00:06:34,130 And here we come, here and here we go to eight, and here we are. 92 00:06:34,130 --> 00:06:37,550 And then we are going to ten. 93 00:06:37,550 --> 00:06:39,650 And this is where our. 94 00:06:41,100 --> 00:06:42,180 Range ends. 95 00:06:42,180 --> 00:06:47,630 So you can create almost any sets of numbers or you want to use the. 96 00:06:47,640 --> 00:06:55,470 So you can also create you can create almost any set of numbers you want to using the range function. 97 00:06:55,470 --> 00:07:00,630 So for example, consider how might make a list of the first ten square numbers. 98 00:07:00,630 --> 00:07:07,530 That is the square of each integers from one through ten in Python, which is two asterisks like that. 99 00:07:07,800 --> 00:07:09,690 So let's actually close this. 100 00:07:09,690 --> 00:07:16,650 So now here's how you might plot the first ten square numbers onto a list. 101 00:07:16,650 --> 00:07:26,070 So here we have my special or my squares here, squares, and here we will create an empty list. 102 00:07:26,100 --> 00:07:32,310 Now we will use the for loop my my value here in range. 103 00:07:32,310 --> 00:07:36,420 And here we will start from 1 to 11. 104 00:07:36,960 --> 00:07:46,890 And here we will use the my squares here equals my value asterisk to add an asterisk here. 105 00:07:47,160 --> 00:07:54,330 And two and here we will use my my ears here that append. 106 00:07:55,460 --> 00:07:56,110 Append. 107 00:08:00,080 --> 00:08:00,500 Yeah. 108 00:08:00,500 --> 00:08:01,760 My value here. 109 00:08:03,260 --> 00:08:03,800 You must. 110 00:08:07,970 --> 00:08:11,120 Here my squares that append. 111 00:08:25,790 --> 00:08:30,320 My square here and it will change with my square equals my value. 112 00:08:30,440 --> 00:08:36,920 62 And here we will use my squares that append the my. 113 00:08:38,790 --> 00:08:39,360 Square. 114 00:08:39,750 --> 00:08:40,710 And that's it. 115 00:08:42,300 --> 00:08:45,660 And after that, of course, we will need to print the squares here. 116 00:08:49,040 --> 00:08:50,510 Here, but outside the for loop. 117 00:08:50,510 --> 00:08:57,920 As you can see here, we are printing outside for loop workers or my spare. 118 00:08:59,180 --> 00:09:00,770 Yeah, that's it. 119 00:09:00,800 --> 00:09:02,600 Now let's run it. 120 00:09:03,820 --> 00:09:12,260 And as you can see here, one, four, nine, 16, 25, 38, 49, 64, 81 and 100. 121 00:09:12,280 --> 00:09:15,730 So we start with an empty list called squares. 122 00:09:15,850 --> 00:09:17,110 Actually, let me here. 123 00:09:17,140 --> 00:09:24,550 So here we start with the empty list called my squares. 124 00:09:24,820 --> 00:09:26,530 Here, let's actually use that marker. 125 00:09:26,560 --> 00:09:29,860 So here we start with the empty list called my squares. 126 00:09:29,890 --> 00:09:35,800 Then we tell Python to loop through each value. 127 00:09:36,400 --> 00:09:38,470 Loop through each value. 128 00:09:39,400 --> 00:09:40,540 From one. 129 00:09:41,790 --> 00:09:43,500 210. 130 00:09:43,800 --> 00:09:48,930 In this case, we read write 11 using the range function. 131 00:09:48,930 --> 00:09:57,300 So inside the loop, the current value is raised to the second power and assigned to the variable. 132 00:09:57,720 --> 00:09:59,220 My square. 133 00:09:59,220 --> 00:09:59,880 Here. 134 00:10:00,090 --> 00:10:06,480 Each new value of square is then appended to my square list here. 135 00:10:06,570 --> 00:10:09,150 As you can see here we are using append here. 136 00:10:09,150 --> 00:10:10,260 So this worked like that. 137 00:10:10,260 --> 00:10:16,740 For example, this is our my squares here and here we added and then another loop comes. 138 00:10:16,740 --> 00:10:20,310 We added another value and then another look loop comes. 139 00:10:20,310 --> 00:10:25,230 We added another value inside it and until the ten times here. 140 00:10:25,950 --> 00:10:27,840 So that's it. 141 00:10:28,110 --> 00:10:36,840 And finally, when the loop has finished running, the list of squares is printed on the screen.