1 00:00:00,000 --> 00:00:05,490 In this video we will cover Loops in particular for loops and while loops. 2 00:00:05,490 --> 00:00:08,400 We will use many visual examples in this video. 3 00:00:08,400 --> 00:00:10,570 See the labs for examples with data. 4 00:00:10,570 --> 00:00:12,570 Before we talk about loops, 5 00:00:12,570 --> 00:00:14,340 let's go over the range function. 6 00:00:14,340 --> 00:00:18,470 The range function outputs and ordered sequence as a list I. 7 00:00:18,470 --> 00:00:20,960 If the input is a positive integer, 8 00:00:20,960 --> 00:00:22,810 the output is a sequence. 9 00:00:22,810 --> 00:00:27,900 The sequence contains the same number of elements as the input but starts at zero. 10 00:00:27,900 --> 00:00:34,220 For example, if the input is three the output is the sequence zero, one, two. 11 00:00:34,220 --> 00:00:36,640 If the range function has two inputs where 12 00:00:36,640 --> 00:00:39,510 the first input is smaller than the second input, 13 00:00:39,510 --> 00:00:42,660 the output is a sequence that starts at the first input. 14 00:00:42,660 --> 00:00:46,980 Then the sequence iterates up to but not including the second number. 15 00:00:46,980 --> 00:00:51,500 For the input 10 and 15 we get the following sequence. 16 00:00:51,500 --> 00:00:55,140 See the labs for more capabilities of the range function. 17 00:00:55,140 --> 00:00:58,110 Please note, if you use Python three, 18 00:00:58,110 --> 00:01:02,490 the range function will not generate a list explicitly like in Python two. 19 00:01:02,490 --> 00:01:05,300 In this section, we will cover for loops. 20 00:01:05,300 --> 00:01:10,160 We will focus on lists, but many of the procedures can be used on tuples. 21 00:01:10,160 --> 00:01:13,080 Loops perform a task over and over. 22 00:01:13,080 --> 00:01:15,680 Consider the group of colored squares. 23 00:01:15,680 --> 00:01:19,820 Let's say we would like to replace each colored square with a white square. 24 00:01:19,820 --> 00:01:22,620 Let's give each square a number to make things a little 25 00:01:22,620 --> 00:01:26,670 easier and refer to all the group of squares as squares. 26 00:01:26,670 --> 00:01:29,900 If we wanted to tell someone to replace squares zero with 27 00:01:29,900 --> 00:01:33,960 a white square, we would say equals replace square 28 00:01:33,960 --> 00:01:38,010 zero with a white square or we can say four squares 29 00:01:38,010 --> 00:01:42,420 zero in squares square zero equals white square. 30 00:01:42,420 --> 00:01:48,200 Similarly, for the next square we can say for square one in squares, 31 00:01:48,200 --> 00:01:51,160 square one equals white square. 32 00:01:51,160 --> 00:01:55,650 For the next square we can say for square two in squares, 33 00:01:55,650 --> 00:01:58,370 square two equals white square. 34 00:01:58,370 --> 00:02:01,200 We repeat the process for each square. 35 00:02:01,200 --> 00:02:05,820 The only thing that changes is the index of the square we are referring to. 36 00:02:05,820 --> 00:02:11,560 If we're going to perform a similar task in Python we cannot use actual squares. 37 00:02:11,560 --> 00:02:14,600 So let's use a list to represent the boxes. 38 00:02:14,600 --> 00:02:17,920 Each element in the list is a string representing the color. 39 00:02:17,920 --> 00:02:21,680 We want to change the name of the color in each element to white. 40 00:02:21,680 --> 00:02:24,860 Each element in the list has the following index. 41 00:02:24,860 --> 00:02:27,920 This is a syntax to perform a loop in Python. 42 00:02:27,920 --> 00:02:31,930 Notice the indent, the range function generates a list. 43 00:02:31,930 --> 00:02:36,190 The code will simply repeat everything in the indent five times. 44 00:02:36,190 --> 00:02:40,750 If you were to change the value to six it would do it 6 times. 45 00:02:40,750 --> 00:02:45,320 However, the value of I is incremented by one each time. 46 00:02:45,320 --> 00:02:49,390 In this segment we change the I element of the list to the string white. 47 00:02:49,390 --> 00:02:51,840 The value of I is set to zero. 48 00:02:51,840 --> 00:02:55,730 Each iteration of the loop starts at the beginning of the indent. 49 00:02:55,730 --> 00:02:58,070 We then run everything in the indent. 50 00:02:58,070 --> 00:03:00,750 The first element in the list is set to white. 51 00:03:00,750 --> 00:03:02,670 We then go to the start of the indent, 52 00:03:02,670 --> 00:03:04,640 we progress down each line. 53 00:03:04,640 --> 00:03:07,750 When we reach the line to change the value of the list, 54 00:03:07,750 --> 00:03:10,670 we set the value of index one to white. 55 00:03:10,670 --> 00:03:13,260 The value of I increases by one. 56 00:03:13,260 --> 00:03:16,160 We repeat the process for index two. 57 00:03:16,160 --> 00:03:19,160 The process continues for the next index, 58 00:03:19,160 --> 00:03:22,090 until we've reached the final element. 59 00:03:22,090 --> 00:03:26,580 We can also iterate through a list or tuple directly in python, 60 00:03:26,580 --> 00:03:29,290 we do not even need to use indices. 61 00:03:29,290 --> 00:03:31,190 Here is the list squares. 62 00:03:31,190 --> 00:03:33,410 Each iteration of the list we pass 63 00:03:33,410 --> 00:03:36,510 one element of the list squares to the variable square. 64 00:03:36,510 --> 00:03:40,360 Lets display the value of the variable square on this section. 65 00:03:40,360 --> 00:03:42,020 For the first iteration, 66 00:03:42,020 --> 00:03:43,900 the value of square is red, 67 00:03:43,900 --> 00:03:46,070 we then start the second iteration. 68 00:03:46,070 --> 00:03:47,720 For the second iteration, 69 00:03:47,720 --> 00:03:49,860 the value of square is yellow. 70 00:03:49,860 --> 00:03:55,840 We then start the third iteration. For the final iteration, the value of square is green, 71 00:03:55,840 --> 00:03:59,470 a useful function for iterating data is enumerate. 72 00:03:59,470 --> 00:04:03,140 It can be used to obtain the index and the element in the list. 73 00:04:03,140 --> 00:04:08,090 Let's use the box analogy with the numbers representing the index of each square. 74 00:04:08,090 --> 00:04:12,770 This is the syntax to iterate through a list and provide the index of each element. 75 00:04:12,770 --> 00:04:18,150 We use the list squares and use the names of the colors to represent the colored squares. 76 00:04:18,150 --> 00:04:21,480 The argument of the function enumerate is the list. 77 00:04:21,480 --> 00:04:24,910 In this case squares the variable I is 78 00:04:24,910 --> 00:04:29,580 the index and the variable square is the corresponding element in the list. 79 00:04:29,580 --> 00:04:31,610 Let's use the left part of the screen to 80 00:04:31,610 --> 00:04:34,620 display the different values of the variable square 81 00:04:34,620 --> 00:04:39,100 and I for the various iterations of the loop. For the first iteration, 82 00:04:39,100 --> 00:04:42,080 the value of the variable is red corresponding to 83 00:04:42,080 --> 00:04:47,250 the zeroth index, and the value for I is zero for the second iteration. 84 00:04:47,250 --> 00:04:50,270 The value of the variable square is yellow, and 85 00:04:50,270 --> 00:04:53,880 the value of I corresponds to its index i.e. 86 00:04:53,880 --> 00:04:57,810 1. We repeat the process for the last index. 87 00:04:57,810 --> 00:05:02,490 While loops are similar to for loops but instead of executing 88 00:05:02,490 --> 00:05:07,850 a statement a set number of times a while loop will only run if a condition is met. 89 00:05:07,850 --> 00:05:09,840 Let's say we would like to copy 90 00:05:09,840 --> 00:05:14,230 all the orange squares from the list squares to the list New squares. 91 00:05:14,230 --> 00:05:17,720 But we would like to stop if we encounter a non-orange square. 92 00:05:17,720 --> 00:05:20,510 We don't know the value of the squares beforehand. 93 00:05:20,510 --> 00:05:23,430 We would simply continue the process while the square is 94 00:05:23,430 --> 00:05:26,600 orange or see if the square equals orange. 95 00:05:26,600 --> 00:05:29,580 If not, we would stop. For the first example, 96 00:05:29,580 --> 00:05:31,640 we would check if the square was orange. 97 00:05:31,640 --> 00:05:34,700 It satisfies the conditions so we would copy the square. 98 00:05:34,700 --> 00:05:38,900 We repeat the process for the second square. The condition is met. 99 00:05:38,900 --> 00:05:44,040 So we copy the square. In the next iteration, we encounter a purple square. 100 00:05:44,040 --> 00:05:45,540 The condition is not met. 101 00:05:45,540 --> 00:05:47,020 So we stop the process. 102 00:05:47,020 --> 00:05:50,160 This is essentially what a while loop does. 103 00:05:50,160 --> 00:05:53,730 Let's use the figure on the left to represent the code. We will 104 00:05:53,730 --> 00:05:57,640 use a list with the names of the color to represent the different squares. 105 00:05:57,640 --> 00:06:00,350 We create an empty list of new squares. 106 00:06:00,350 --> 00:06:03,980 In reality the list is of indeterminate size. 107 00:06:03,980 --> 00:06:08,020 We start the index at zero the while statement will repeatedly 108 00:06:08,020 --> 00:06:12,800 execute the statements within the indent until the condition inside the bracket is false. 109 00:06:12,800 --> 00:06:18,200 We append the value of the first element of the list squares to the list new squares. 110 00:06:18,200 --> 00:06:20,890 We increase the value of I by one. 111 00:06:20,890 --> 00:06:26,600 We append the value of the second element of the list squares to the list new squares. 112 00:06:26,600 --> 00:06:28,570 We increment the value of I. 113 00:06:28,570 --> 00:06:31,860 Now the value in the array squares is purple; 114 00:06:31,860 --> 00:06:36,290 therefore, the condition for the while statement is false and we exit the loop. 115 00:06:36,290 --> 00:06:40,080 Check out the labs for more examples of loop many with real data. 116 00:06:40,080 --> 00:06:45,000 (Music)