1 00:00:00,750 --> 00:00:01,770 Instructor: Welcome back. 2 00:00:01,770 --> 00:00:04,440 Let's try a simple exercise. 3 00:00:04,440 --> 00:00:07,260 I want you to build a simple counter. 4 00:00:07,260 --> 00:00:12,260 And this counter is going to count the items in our list. 5 00:00:13,680 --> 00:00:14,640 What does that mean? 6 00:00:14,640 --> 00:00:19,143 Well, let's say I have a list, 7 00:00:20,070 --> 00:00:22,170 and this is a reserved word. 8 00:00:22,170 --> 00:00:24,900 Remember, it's an actual word in Python. 9 00:00:24,900 --> 00:00:29,310 So let's name it my_list as a variable. 10 00:00:29,310 --> 00:00:34,310 and this list has 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 11 00:00:36,180 --> 00:00:39,840 What I want you to do is using looping 12 00:00:39,840 --> 00:00:43,800 to loop over this iterable list 13 00:00:43,800 --> 00:00:46,143 and sum up the total of the list. 14 00:00:47,250 --> 00:00:49,020 Pause the video if you need to. 15 00:00:49,020 --> 00:00:51,020 Otherwise, let's see how we can do that. 16 00:00:53,310 --> 00:00:58,200 What I'll do is create a counter variable 17 00:00:58,200 --> 00:01:01,320 that will equal to 0 to start off with, 18 00:01:01,320 --> 00:01:06,320 and then I'll say for item in my_list, 19 00:01:08,310 --> 00:01:09,280 I'm going to 20 00:01:10,170 --> 00:01:15,170 counter = counter + item, 21 00:01:18,180 --> 00:01:20,370 and then at the end, 22 00:01:20,370 --> 00:01:23,643 I'm going to print counter. 23 00:01:24,510 --> 00:01:26,070 Let's see if this works. 24 00:01:26,070 --> 00:01:27,900 I'm going to hit run 25 00:01:27,900 --> 00:01:29,730 and I get 55, 26 00:01:29,730 --> 00:01:33,324 which, if my quick math is right, 27 00:01:33,324 --> 00:01:34,500 should be correct. 28 00:01:34,500 --> 00:01:37,290 Now, when doing this exercise, 29 00:01:37,290 --> 00:01:39,990 you may have gotten tripped up a bit. 30 00:01:39,990 --> 00:01:41,640 So let's think about this. 31 00:01:41,640 --> 00:01:43,890 Some of you may have made an error 32 00:01:43,890 --> 00:01:46,260 where you did the print here. 33 00:01:46,260 --> 00:01:48,030 But if you did the print here, 34 00:01:48,030 --> 00:01:50,730 you remember that it's going to get looped over. 35 00:01:50,730 --> 00:01:53,490 It's part of this code block, 36 00:01:53,490 --> 00:01:55,140 because of the indentation, 37 00:01:55,140 --> 00:02:00,140 and it's going to print as many times as there are items. 38 00:02:01,230 --> 00:02:02,280 So you have to make sure 39 00:02:02,280 --> 00:02:04,470 that the indentation is like this 40 00:02:04,470 --> 00:02:06,123 so that you get the total. 41 00:02:07,080 --> 00:02:09,030 The other thing that you noticed 42 00:02:09,030 --> 00:02:12,150 is that in order for you to keep a counter 43 00:02:12,150 --> 00:02:16,230 or a total of all these items, 44 00:02:16,230 --> 00:02:19,140 you had to make sure that you had a variable 45 00:02:19,140 --> 00:02:20,700 outside of the loop, 46 00:02:20,700 --> 00:02:24,240 because the loop runs the code over and over, 47 00:02:24,240 --> 00:02:26,670 and you needed something on the outside 48 00:02:26,670 --> 00:02:28,410 that doesn't change. 49 00:02:28,410 --> 00:02:32,310 Because if you moved the counter to 0 here, 50 00:02:32,310 --> 00:02:37,170 well, every time the counter will be reset to 0 51 00:02:37,170 --> 00:02:39,540 so that by the time we get to 10, 52 00:02:39,540 --> 00:02:40,980 counter will be 0, 53 00:02:40,980 --> 00:02:44,190 so you get 0 plus 10, which equals 10. 54 00:02:44,190 --> 00:02:46,353 And then you'd get, well, 10. 55 00:02:47,430 --> 00:02:49,560 So you have to be careful here 56 00:02:49,560 --> 00:02:54,560 that the indentation is an indication of us looping. 57 00:02:55,170 --> 00:02:56,550 And when you loop something 58 00:02:56,550 --> 00:02:59,250 and you wanna keep a tally or total of something, 59 00:02:59,250 --> 00:03:01,080 that you have these issues 60 00:03:01,080 --> 00:03:03,990 of making sure that what you wanna loop over 61 00:03:03,990 --> 00:03:06,720 is inside of this code block. 62 00:03:06,720 --> 00:03:08,130 But what you don't want to, 63 00:03:08,130 --> 00:03:10,140 if you wanna have some sort of information 64 00:03:10,140 --> 00:03:11,880 that's outside of this loop, 65 00:03:11,880 --> 00:03:13,803 then you keep it like this. 66 00:03:14,910 --> 00:03:16,980 If you weren't tricked by this question, 67 00:03:16,980 --> 00:03:18,150 good job. 68 00:03:18,150 --> 00:03:19,890 If you were, don't worry. 69 00:03:19,890 --> 00:03:21,180 It's all part of learning. 70 00:03:21,180 --> 00:03:22,740 I'll see you in the next one. 71 00:03:22,740 --> 00:03:23,573 Bye-bye.