1 00:00:00,500 --> 00:00:04,470 Welcome back. I previously showed you 2 00:00:04,470 --> 00:00:11,580 the accumulator pattern iterating over a sequence like the numbers from one to 10. 3 00:00:11,580 --> 00:00:17,325 Python actually provides a function for us called the range function. 4 00:00:17,325 --> 00:00:19,815 It takes as an input, 5 00:00:19,815 --> 00:00:25,180 a number and what it produces as an output, 6 00:00:25,190 --> 00:00:31,859 range of five will produce a sequence zero, 7 00:00:31,859 --> 00:00:35,680 one, two, three, four. 8 00:00:38,480 --> 00:00:47,185 So, the sequence it produces has the same number of items as the value that we passed in. 9 00:00:47,185 --> 00:00:50,900 The first element will be zero and then we'll 10 00:00:50,900 --> 00:00:54,155 get all the numbers up to but not including five. 11 00:00:54,155 --> 00:00:56,060 If I say range of 10, 12 00:00:56,060 --> 00:00:59,400 I'll get the integers from zero up to nine. 13 00:00:59,410 --> 00:01:02,350 Now, there's one subtlety to this. 14 00:01:02,350 --> 00:01:07,805 The range function in a real Python implementation doesn't actually produce a list. 15 00:01:07,805 --> 00:01:10,865 It produces what's called an iterable, 16 00:01:10,865 --> 00:01:14,060 something that will generate the numbers zero through 17 00:01:14,060 --> 00:01:17,795 four when we iterate through them in a four loop. 18 00:01:17,795 --> 00:01:22,940 So if I, say for I in range of five it will print out the numbers zero, 19 00:01:22,940 --> 00:01:24,530 one, two, three and four. 20 00:01:24,530 --> 00:01:27,050 If I actually want a real list, 21 00:01:27,050 --> 00:01:28,715 I would have to cast it. 22 00:01:28,715 --> 00:01:31,610 I would take range of five and pass it to 23 00:01:31,610 --> 00:01:35,945 the list function which would turn it into a real list. 24 00:01:35,945 --> 00:01:40,445 Now, in our Runestone environment, 25 00:01:40,445 --> 00:01:45,410 we cheat a little bit and we actually have range of five producing a list object. 26 00:01:45,410 --> 00:01:47,300 So, in the Runestone environment, 27 00:01:47,300 --> 00:01:52,675 you don't strictly speaking have to do this casting of the output to make it a list. 28 00:01:52,675 --> 00:01:55,475 But it's a good idea to get into the habit of doing things like 29 00:01:55,475 --> 00:01:58,700 lines 12 and 13 because in a full Python environment, 30 00:01:58,700 --> 00:02:00,920 if you really wanted to have a list you would have 31 00:02:00,920 --> 00:02:03,200 to take the output of range of five and 32 00:02:03,200 --> 00:02:09,090 pass it to list to cast it into being a real list. 33 00:02:09,190 --> 00:02:12,080 One other thing I want to show you here and then 34 00:02:12,080 --> 00:02:15,430 we'll see the output to check everything we've done. 35 00:02:15,430 --> 00:02:19,435 Range, takes an optional second argument. 36 00:02:19,435 --> 00:02:24,620 So, it actually takes several additional arguments and 37 00:02:24,620 --> 00:02:30,600 we'll have a chance to see what impact this extra argument has. 38 00:02:31,540 --> 00:02:40,760 So, let's run it. Just to match things up here we're just 39 00:02:40,760 --> 00:02:44,555 printing that its range of five and then 40 00:02:44,555 --> 00:02:50,250 lines four and five are producing these numbers zero to four. 41 00:02:50,250 --> 00:02:57,290 That's the same as saying range starting at zero and going up to five. 42 00:02:57,290 --> 00:03:05,160 But if I said range of one five and we'd get something a little different. 43 00:03:05,160 --> 00:03:10,210 Now I would get starting from one up to but not including five. 44 00:03:10,210 --> 00:03:13,220 There's actually an optional third argument that can say 45 00:03:13,220 --> 00:03:15,790 you wanted to increment by a certain amount, 46 00:03:15,790 --> 00:03:17,240 but we won't go into that detail. 47 00:03:17,240 --> 00:03:22,309 Now as I was I was mentioning in the Runestone environment, 48 00:03:22,309 --> 00:03:26,385 range of five comes out as an actual list. 49 00:03:26,385 --> 00:03:28,440 In a full Python interpreter, 50 00:03:28,440 --> 00:03:31,940 it wouldn't be an actual list object unless you first 51 00:03:31,940 --> 00:03:35,995 passed it into the list function to cast it. 52 00:03:35,995 --> 00:03:39,230 The advantage of having the range function, 53 00:03:39,230 --> 00:03:41,840 is that we don't have to write code like on line 54 00:03:41,840 --> 00:03:45,665 one to ask for all the numbers between one and 10. 55 00:03:45,665 --> 00:03:48,320 In fact I'm going to take you back to our code for 56 00:03:48,320 --> 00:03:53,460 the accumulator pattern and show you how it could've been a little simpler. 57 00:03:58,330 --> 00:04:00,545 So, instead of saying, 58 00:04:00,545 --> 00:04:02,760 "nums equals that list", 59 00:04:02,760 --> 00:04:07,780 I could've said, "nums equals range of 1, 11." 60 00:04:10,600 --> 00:04:16,430 I would have gotten the same result of 55. 61 00:04:16,430 --> 00:04:20,540 The range function, will show up as a handy little utility for us throughout 62 00:04:20,540 --> 00:04:22,640 the rest of the specialization and throughout 63 00:04:22,640 --> 00:04:25,640 the rest of your time as a Python programmer. 64 00:04:25,640 --> 00:04:29,510 But keep in mind, the range function can just take 65 00:04:29,510 --> 00:04:34,340 one argument and then 66 00:04:34,340 --> 00:04:39,230 it would give you all the numbers from zero up to but not including 11, 67 00:04:39,230 --> 00:04:46,470 or it can take two arguments where you specify the first item and the last. 68 00:04:46,470 --> 00:04:48,740 So, three to 11 is going to give us less than 69 00:04:48,740 --> 00:04:51,905 55 because we're not including the one and the two. 70 00:04:51,905 --> 00:04:54,200 So, we get 52 instead. 71 00:04:54,200 --> 00:04:58,640 For the range function, takes a number and it gives you 72 00:04:58,640 --> 00:05:03,385 back a sequence like this list of numbers from one to 10. 73 00:05:03,385 --> 00:05:05,720 We'll see you next time.