1 00:00:01,350 --> 00:00:03,210 -: When looping in Python, one 2 00:00:03,210 --> 00:00:08,210 of the most common tools is this "range" function 3 00:00:08,427 --> 00:00:11,820 that we get out of the box with Python. 4 00:00:11,820 --> 00:00:16,410 And you see here that range, well, is a range object 5 00:00:16,410 --> 00:00:20,340 a special type of object in Python that returns an object 6 00:00:20,340 --> 00:00:22,980 that produces a sequence of integers 7 00:00:22,980 --> 00:00:24,963 from start to stop. 8 00:00:26,490 --> 00:00:28,340 Let's explore that a little bit more. 9 00:00:29,820 --> 00:00:34,820 If I print here a range, and I say "range 100" 10 00:00:35,400 --> 00:00:36,747 and I click "run," 11 00:00:38,100 --> 00:00:42,303 I get range of 0 to 100. 12 00:00:43,950 --> 00:00:47,250 Hmm. So it looks like a topple, but it's not. 13 00:00:47,250 --> 00:00:49,140 It's a range object. 14 00:00:49,140 --> 00:00:52,860 It's a special type of object. And range, 15 00:00:52,860 --> 00:00:55,770 you can give it like this, which gives you a default 16 00:00:55,770 --> 00:00:59,790 of 0 to whatever you give it, which is 100 17 00:00:59,790 --> 00:01:03,180 Or you can just simply type 0, 100. 18 00:01:03,180 --> 00:01:06,780 And if I click "run," it's the same thing. 19 00:01:06,780 --> 00:01:10,680 But where "range" becomes really useful, and you'll see this 20 00:01:10,680 --> 00:01:15,680 in 99% of cases in Python, is in 4 loops. 21 00:01:15,900 --> 00:01:16,733 You see, 22 00:01:16,733 --> 00:01:19,087 besides "topples and lists and sets and dictionaries" 23 00:01:19,087 --> 00:01:22,770 "and strengths," you can iterate over a range. 24 00:01:22,770 --> 00:01:26,220 So for example, if I do "for i" 25 00:01:26,220 --> 00:01:28,290 in "range," and let's just 26 00:01:28,290 --> 00:01:32,967 say "item," or in our case, let's just do "number in range." 27 00:01:33,840 --> 00:01:37,287 And in here I do "print number." 28 00:01:38,160 --> 00:01:42,360 If I run this, you'll see that I'll get numbers 1 29 00:01:42,360 --> 00:01:46,080 to 100. And why is this useful? 30 00:01:46,080 --> 00:01:50,283 Well, using "range," I was able to loop 100 times, 31 00:01:51,450 --> 00:01:54,690 as you can see, all the way till 99 because our account 32 00:01:54,690 --> 00:01:58,680 starts at 0, and we're able to loop as many times 33 00:01:58,680 --> 00:02:01,080 as I want. So I can tell the loop, 34 00:02:01,080 --> 00:02:04,320 maybe we wanna loop a certain number of times. 35 00:02:04,320 --> 00:02:08,340 Maybe you wanna loop 10 times where I can tell the loop how 36 00:02:08,340 --> 00:02:09,570 many times to run. 37 00:02:09,570 --> 00:02:11,193 It's extremely useful. 38 00:02:12,450 --> 00:02:16,510 Maybe here we're performing some action where it says 39 00:02:18,150 --> 00:02:21,330 I don't know, email, "email list." 40 00:02:21,330 --> 00:02:26,330 So we wanna send 10 emails to an email list, for example. 41 00:02:26,670 --> 00:02:31,670 Well, again, I can just click "run" and I sent that email 42 00:02:31,950 --> 00:02:34,560 well a bunch of times, 10 times. 43 00:02:34,560 --> 00:02:37,590 Another neat trick is that, well, you might not 44 00:02:37,590 --> 00:02:39,510 even need "number," right? 45 00:02:39,510 --> 00:02:41,940 We're not really using it in our code. 46 00:02:41,940 --> 00:02:45,180 And often while you'll find in Python programs is, 47 00:02:45,180 --> 00:02:48,570 if a programmer doesn't need this variable, you just 48 00:02:48,570 --> 00:02:50,100 do an underscore. 49 00:02:50,100 --> 00:02:52,050 And this is just a variable. 50 00:02:52,050 --> 00:02:55,000 I mean, you could use it like this 51 00:02:56,910 --> 00:02:58,950 and you'll still get 52 00:02:58,950 --> 00:03:01,110 these numbers, but it just indicates 53 00:03:01,110 --> 00:03:02,370 to other people that, "hey, 54 00:03:02,370 --> 00:03:04,350 I don't really care what this variable is. 55 00:03:04,350 --> 00:03:05,880 I'm just trying to use 'range' 56 00:03:05,880 --> 00:03:07,680 so that we can loop over things." 57 00:03:07,680 --> 00:03:08,793 Let's say 10 times. 58 00:03:09,870 --> 00:03:13,473 Now, "range" also comes with a third parameter. 59 00:03:14,340 --> 00:03:16,830 And what that is, is the step over. 60 00:03:16,830 --> 00:03:18,930 So if I do two, and by the way 61 00:03:18,930 --> 00:03:21,310 default is one here, if I do two 62 00:03:22,560 --> 00:03:26,613 it jumps from 0 to 10 by 2, 63 00:03:28,020 --> 00:03:29,973 which is very, very handy. 64 00:03:31,170 --> 00:03:32,640 So what is "range?" 65 00:03:32,640 --> 00:03:34,620 Well, "range" creates a special kind 66 00:03:34,620 --> 00:03:37,263 of object that we can iterate over. 67 00:03:38,310 --> 00:03:41,910 Okay, what if I do minus oh, right here? 68 00:03:41,910 --> 00:03:44,310 What if I do -1? 69 00:03:44,310 --> 00:03:47,302 Well, if I run this, hmm, 70 00:03:47,302 --> 00:03:51,480 that's not going to work because, well, let me show you why. 71 00:03:51,480 --> 00:03:53,530 If I change this to 0 72 00:03:54,600 --> 00:03:56,650 and 10 and I do -1 73 00:04:01,050 --> 00:04:05,280 it's going to go in the opposite direction. 74 00:04:05,280 --> 00:04:07,953 But if I did just 10 and 0, 75 00:04:09,660 --> 00:04:10,860 that's not going to work. 76 00:04:10,860 --> 00:04:12,870 If we wanna do something in reverse, 77 00:04:12,870 --> 00:04:16,230 we just have to make sure that we add that -1 78 00:04:16,230 --> 00:04:21,209 or, if we wanna step back twice, then we click "run." 79 00:04:21,209 --> 00:04:26,210 So you have a lot of options to play with the range. 80 00:04:26,340 --> 00:04:28,053 You can do ascending, descending. 81 00:04:28,890 --> 00:04:32,670 One final thing, another place that you might see ranges. 82 00:04:32,670 --> 00:04:35,970 And, like I said, most of the time, they're in four loops 83 00:04:35,970 --> 00:04:38,100 but he can also do something interesting 84 00:04:38,100 --> 00:04:40,720 like, let's say, a range 85 00:04:42,840 --> 00:04:44,760 of 10. 86 00:04:44,760 --> 00:04:48,570 And then if we wrap this in a list, 87 00:04:48,570 --> 00:04:53,310 remember a "list function" allows us to convert something 88 00:04:53,310 --> 00:04:54,270 to a list. 89 00:04:54,270 --> 00:04:57,483 If I run this, did you see that? 90 00:04:58,440 --> 00:05:01,710 Let's simplify this so that this range is 91 00:05:01,710 --> 00:05:03,660 going to just be 2. 92 00:05:03,660 --> 00:05:07,920 If I click "run," you see that I 93 00:05:07,920 --> 00:05:09,630 loop two times 94 00:05:09,630 --> 00:05:12,360 and in here I'm going to print, First of all 95 00:05:12,360 --> 00:05:14,340 I'm going to create a range, 96 00:05:14,340 --> 00:05:18,150 an Iterable object that has 10 items 97 00:05:18,150 --> 00:05:20,730 and then I'm going to convert that into a list. 98 00:05:20,730 --> 00:05:24,540 So you can see here 0 to 9 into a list. 99 00:05:24,540 --> 00:05:28,800 So this is a quick way to create a list that well, 100 00:05:28,800 --> 00:05:30,633 has integers. 101 00:05:31,590 --> 00:05:34,560 A neat trick that you'll definitely use along your 102 00:05:34,560 --> 00:05:35,523 programmer journey. 103 00:05:36,360 --> 00:05:37,873 I'll see you in the next one. Bye-bye! 104 00:05:37,873 --> 00:05:39,206 -: -Captions done by Rev. Visit Rev.com for details.