1 00:00:00,760 --> 00:00:01,330 Welcome back. 2 00:00:01,780 --> 00:00:10,750 Let's try a simple exercise, I want you to build a simple counter and this counter is going to count 3 00:00:11,440 --> 00:00:13,130 the items in our list. 4 00:00:13,660 --> 00:00:14,410 What does that mean? 5 00:00:14,650 --> 00:00:18,970 Well, let's say I have a list. 6 00:00:20,040 --> 00:00:27,990 And this is a reserved award who remember, it's an actual word in Python, so let's name it my list 7 00:00:28,020 --> 00:00:28,890 as a variable. 8 00:00:29,280 --> 00:00:33,900 And this list has one, two, three, four, five, six, seven, eight, nine. 9 00:00:36,150 --> 00:00:45,990 What I want you to do is using looping to loop over this iterable list and sum up the total of the list. 10 00:00:47,210 --> 00:00:48,750 Pause the video if you need to. 11 00:00:49,010 --> 00:00:50,800 Otherwise, let's see how we can do that. 12 00:00:53,300 --> 00:01:02,540 What I'll do is create a counter variable that will equal to zero to start off with, and then I'll 13 00:01:02,540 --> 00:01:10,820 say for item in my list, I'm going to counter. 14 00:01:12,390 --> 00:01:14,250 Equals counter. 15 00:01:15,300 --> 00:01:16,770 Plus item. 16 00:01:18,140 --> 00:01:23,510 And then at the end, I'm going to print counter. 17 00:01:24,530 --> 00:01:32,840 Let's see if this works, I'm going to run and I get 55, which, if my quick math is right, should 18 00:01:32,840 --> 00:01:33,930 be should be correct. 19 00:01:34,490 --> 00:01:39,250 Now, when doing this exercise, you may have gotten tripped up a bit. 20 00:01:39,980 --> 00:01:41,510 So let's think about this. 21 00:01:41,630 --> 00:01:48,230 Some of you may have made an error where you did the print here, but if you did the print here, you 22 00:01:48,230 --> 00:01:50,750 remember that it's going to get looped over. 23 00:01:50,750 --> 00:01:58,970 It's part of this code block because of the indentation and it's going to print as many times as there 24 00:01:59,000 --> 00:02:00,110 are items. 25 00:02:01,210 --> 00:02:05,980 So you have to make sure that the indentation is like this so that you get the total. 26 00:02:07,080 --> 00:02:15,420 The other thing that you noticed is that in order for you to keep a counter or a total of all these 27 00:02:15,420 --> 00:02:22,890 items, you had to make sure that you had a variable outside of the loop because the loop runs the code 28 00:02:22,890 --> 00:02:23,970 over and over. 29 00:02:24,210 --> 00:02:31,680 And you needed something on the outside that doesn't change because if you moved the counter to zero 30 00:02:31,680 --> 00:02:40,050 here, well, every time the counter would be reset to zero so that by the time we get to ten, counter 31 00:02:40,050 --> 00:02:40,800 would be zero. 32 00:02:40,950 --> 00:02:46,230 So you get zero plus 10, which equals 10, and then you'd get, well, 10. 33 00:02:47,440 --> 00:02:51,070 So you have to be careful here that the indentation. 34 00:02:52,200 --> 00:02:58,560 Is an indication of us looping and when you loop something and you want to keep a tally or total of 35 00:02:58,560 --> 00:03:05,490 something, that you have these issues of making sure that what you want to loop over is inside of this 36 00:03:05,490 --> 00:03:06,300 code block. 37 00:03:06,660 --> 00:03:12,060 But why you don't want to if you want to have some sort of information that's outside of this loop that 38 00:03:12,060 --> 00:03:13,650 you keep it like this. 39 00:03:14,900 --> 00:03:20,990 If you weren't tricked by this question, good job if you were, don't worry, it's all part of learning. 40 00:03:21,230 --> 00:03:23,090 I'll see in the next one, Bhabhi.