1 00:00:00,233 --> 00:00:02,430 In this video we'll take another look 2 00:00:02,430 --> 00:00:04,450 at that enumerate function that 3 00:00:04,450 --> 00:00:06,980 we just demonstrated in the preceding video. 4 00:00:06,980 --> 00:00:08,760 This time in the context of 5 00:00:08,760 --> 00:00:13,420 the script fig05_01.py. 6 00:00:13,420 --> 00:00:16,210 Let's go ahead and run that and see 7 00:00:16,210 --> 00:00:17,950 what the results look like. 8 00:00:17,950 --> 00:00:19,710 So basically what we're going to do 9 00:00:19,710 --> 00:00:22,180 is we're going to take a list of values, 10 00:00:22,180 --> 00:00:24,440 which you see in the middle column here, 11 00:00:24,440 --> 00:00:26,900 and we're going to use the enumerate function 12 00:00:26,900 --> 00:00:29,980 to help us produce the output that you see here 13 00:00:29,980 --> 00:00:33,270 consisting of the index number of each value, 14 00:00:33,270 --> 00:00:37,240 the actual value, and then a bar of asterisks 15 00:00:37,240 --> 00:00:38,620 representing that value. 16 00:00:38,620 --> 00:00:40,930 So basically a primitive bar chart. 17 00:00:40,930 --> 00:00:43,620 And we're going to show you later on in this lesson 18 00:00:43,620 --> 00:00:46,460 much nicer looking visualizations 19 00:00:46,460 --> 00:00:49,760 that produce bar charts automatically for you. 20 00:00:49,760 --> 00:00:51,210 So we'll take a look at that in 21 00:00:51,210 --> 00:00:55,420 the intro to data science section at the end of this lesson. 22 00:00:55,420 --> 00:00:57,720 So let's switch over to the script 23 00:00:57,720 --> 00:01:00,920 that actually produces the output you just saw there. 24 00:01:00,920 --> 00:01:03,190 Again anytime we create a script 25 00:01:03,190 --> 00:01:05,200 we're going to tell you the name of the file 26 00:01:05,200 --> 00:01:07,080 in a comment at the beginning so you can 27 00:01:07,080 --> 00:01:10,900 find that script in the corresponding lesson's examples. 28 00:01:10,900 --> 00:01:14,390 And all scripts should start with a doc string 29 00:01:14,390 --> 00:01:17,270 that states the purpose of that script. 30 00:01:17,270 --> 00:01:19,900 And since most of our scripts are very straightforward, 31 00:01:19,900 --> 00:01:22,430 we typically just have a one-line doc string; 32 00:01:22,430 --> 00:01:26,090 however, it's not uncommon for a complex script 33 00:01:26,090 --> 00:01:31,030 or a library to have a very detailed documentation string. 34 00:01:31,030 --> 00:01:33,170 Now we're going to be working in this example 35 00:01:33,170 --> 00:01:35,010 with the list called numbers, which 36 00:01:35,010 --> 00:01:37,300 just contains five values. 37 00:01:37,300 --> 00:01:39,020 And we have a couple of print statements 38 00:01:39,020 --> 00:01:40,730 to get us started here. 39 00:01:40,730 --> 00:01:43,020 Let's take a look at the second one for a moment 40 00:01:43,020 --> 00:01:46,310 because in this f string we have a placeholder. 41 00:01:46,310 --> 00:01:48,690 And I just want you to reemphasize 42 00:01:48,690 --> 00:01:52,580 the fact that we have a single quoted f string. 43 00:01:52,580 --> 00:01:55,030 And because of the fact that we're outputting 44 00:01:55,030 --> 00:01:58,490 a string value in this placeholder 45 00:01:58,490 --> 00:02:02,180 we're wrapping that string, that nested string, 46 00:02:02,180 --> 00:02:06,410 in double quote characters that way we don't get an error 47 00:02:06,410 --> 00:02:09,540 because of nesting single quote characters 48 00:02:09,540 --> 00:02:11,600 within single quote characters. 49 00:02:11,600 --> 00:02:14,030 So that's why we have double quote characters here. 50 00:02:14,030 --> 00:02:17,130 And you may recall that this is not an emoji, 51 00:02:17,130 --> 00:02:21,130 but rather a format specifier indicating 52 00:02:21,130 --> 00:02:24,370 that we want to write a line, the string value, 53 00:02:24,370 --> 00:02:27,130 in a field of eight characters. 54 00:02:27,130 --> 00:02:28,370 So what's going to happen here 55 00:02:28,370 --> 00:02:30,940 is we'll display the literal characters' index, 56 00:02:30,940 --> 00:02:32,980 which is five character positions, 57 00:02:32,980 --> 00:02:35,610 and then in the next eight character positions 58 00:02:35,610 --> 00:02:37,720 we'll display these five characters 59 00:02:37,720 --> 00:02:40,410 preceded by three spaces, and then 60 00:02:40,410 --> 00:02:43,490 we'll have a couple of spaces and the word Bar. 61 00:02:43,490 --> 00:02:46,340 And that will help us produce the three column heads, 62 00:02:46,340 --> 00:02:48,330 and we'll use some similar formatting 63 00:02:48,330 --> 00:02:52,200 down here in the for loop to produce the three columns. 64 00:02:52,200 --> 00:02:53,950 Now let's talk about this for loop. 65 00:02:53,950 --> 00:02:57,500 So we just showed you that the enumerate function 66 00:02:57,500 --> 00:03:02,500 can produce a sequence of tuples consisting of each value 67 00:03:02,950 --> 00:03:06,050 in the sequence that you give it as an argument, 68 00:03:06,050 --> 00:03:08,340 in this case the list numbers and 69 00:03:08,340 --> 00:03:11,650 the index for each of those values. 70 00:03:11,650 --> 00:03:14,100 And again the order within those tuples 71 00:03:14,100 --> 00:03:16,830 will be the index followed by the value. 72 00:03:16,830 --> 00:03:20,220 So because we're getting tuples from the enumerate function 73 00:03:20,220 --> 00:03:22,840 we are able to unpack those tuples 74 00:03:22,840 --> 00:03:25,890 into the appropriate number of variables. 75 00:03:25,890 --> 00:03:28,870 In the case of enumerate that will always be two. 76 00:03:28,870 --> 00:03:31,510 And so we have the index and the value, 77 00:03:31,510 --> 00:03:33,770 and we'd like to format the output. 78 00:03:33,770 --> 00:03:35,880 We're going to display the index 79 00:03:35,880 --> 00:03:37,850 right aligned in a field of five, 80 00:03:37,850 --> 00:03:40,650 which will right align it under the word index, 81 00:03:40,650 --> 00:03:42,120 five characters long. 82 00:03:42,120 --> 00:03:44,370 Then in a field of eight, right aligned we're 83 00:03:44,370 --> 00:03:48,350 going to display the value of the variable value. 84 00:03:48,350 --> 00:03:50,630 Then we have a couple of spaces here, 85 00:03:50,630 --> 00:03:52,200 same number as up above. 86 00:03:52,200 --> 00:03:54,900 And underneath the word bar, we are going to 87 00:03:54,900 --> 00:03:59,820 display the value of this highlighted expression. 88 00:03:59,820 --> 00:04:01,700 Now we haven't done this previously 89 00:04:01,700 --> 00:04:02,980 and this is another one of those 90 00:04:02,980 --> 00:04:05,130 cool little python features. 91 00:04:05,130 --> 00:04:09,370 When you multiply a string by an integer 92 00:04:09,370 --> 00:04:12,720 it repeats the string that number of times. 93 00:04:12,720 --> 00:04:14,680 So what we want in this example 94 00:04:14,680 --> 00:04:17,780 is a bar of asterisks that matches up 95 00:04:17,780 --> 00:04:21,630 with the value of each individual list element. 96 00:04:21,630 --> 00:04:26,630 So if I say multiply the string asterisk by the value 19, 97 00:04:27,620 --> 00:04:31,370 what I'm going to get is 19 consecutive asterisks 98 00:04:31,370 --> 00:04:34,210 as a string that is the value of 99 00:04:34,210 --> 00:04:35,830 this highlighted expression. 100 00:04:35,830 --> 00:04:38,480 Or if I multiply the asterisk string by three 101 00:04:38,480 --> 00:04:42,160 I'll get a string of three asterisks, etc. etc. 102 00:04:42,160 --> 00:04:47,000 So anytime you multiply a sequence by an integer, 103 00:04:47,000 --> 00:04:51,070 you repeat that sequence the specified number of times. 104 00:04:51,070 --> 00:04:53,960 And this little fore loop produced, 105 00:04:53,960 --> 00:04:57,643 going back over here, these rows of the output.