© Distribution of this video is restricted by its owner
Transcript ×
Auto highlight
Font-size
00:09 Hello. This is the lecture for 4th module of the cosh and you'll

00:18 that this lecture is not totally aligned the content in the book. Um

00:26 complement each other. So the same is being motivated different ways in the

00:33 . And this lecture. Uh So do need to pay attention to

00:40 Alright. Back to the basics. we have seen that the computer program

00:50 the following major components and that's And the input, output, math

00:57 logic that we have already seen in previous lecture. So far, the

01:04 components that we have not got into the conditional execution and reputation. So

01:13 is what we will focus on Right. So the program so far

01:26 have seen just run from top to and not repeat the same statements

01:35 But that of course is not what happens. So let's keep moving.

01:43 so the main topic today is loops alterations. Loops allow us to specify

01:50 set of statements, some things that to be repeated multiple times, one

01:56 , two times 10 times 1000 And another way to think of it

02:03 that computers are really good at automating and they're really good at repeating things

02:11 and again, they don't get And so most programs will require us

02:18 do something many many times. And examples you can think of say

02:24 You need to do it for the class, not just for one student

02:28 parole for employees, voice recognition, might be the same. Basic

02:34 but it has to be applied over over again and that's where iterations and

02:39 come in. So python, of provides features to support repeating things and

02:48 is a four loop. The other the range function. And there's something

02:52 the wild loop today. We'll go the four loop and range function.

02:58 and in a couple of weeks we'll to the white loops. So let's

03:05 at something very simple to get a for uh, for iteration or for

03:13 things. Here is a program that a poster for a fruit shop.

03:19 it says we have apples, we bananas, we have grapes, we

03:22 oranges. This is actually a python here, which not surprisingly is going

03:30 print those things because those are four statements. So now you can see

03:36 could have done this more efficiently. if they were like instead of

03:40 there were 20 fruits. This is the best way to be organizing this

03:50 . So we would use something like four loop which will achieve this task

03:55 little bit more efficiently. So here's few terms I'm going to throw in

04:01 four loops. Iterate. Iterate means some lines of the program over each

04:10 out of a collection of elements, at a time. And if you're

04:15 quite clear on what what is? okay? We have to iterate over

04:20 elements in a collection because we're gonna the next few minutes trying to get

04:26 clear on what all that means. for luke illiterates executes for a

04:35 So now think what do we need iterate over here? It's clear that

04:41 is this line of code. It's same line we need to repeat several

04:48 . And whatever the collection the collection the list of elements over which we

04:54 . And you can see this is fruits. The list of fruits is

05:00 collection we're talking about. So summarize . The collection in this particular example

05:06 the list of fruits and iteration is print line. The code that we

05:10 to repeat is a print line. how does this all look the equal

05:19 program to achieve exactly this. Uh uh whatever is achieved by this chord

05:28 here as a whole loop. It for fruit and apples, bananas,

05:36 , oranges, print we have And the expectation is that the program

05:42 will do exactly what the four lines the program above are doing. So

05:47 dig in a little deeper so that make sure we understand this example well

05:54 that we can apply this concept to other example too. So here is

06:01 same program again um copied over from previous slide. So here fruit.

06:11 fruit is the loop variable. So variable is the one that takes on

06:17 values every time you go through the . And here is the list of

06:23 collection of elements that we interpret or over. So we need a

06:28 we need the this collection of And the loop body is what is

06:34 every time the process goes forward and just the print statement. Okay.

06:48 . So let's just go through this and see what is supposed to happen

06:58 each iteration of the loop. When start executing this loop, there is

07:03 variable fruit. The variable fruit is one of the values from the

07:09 It can be apples, bananas, , oranges, one by one.

07:14 will go over and take that value proceed. Then you go to the

07:22 body here you have the loop We have and then the loop variable

07:28 is being printed and this line is . So as you can imagine every

07:34 you go through this section we will a different fruit over there. So

07:46 try to make this even more clear happens when this this is executed and

07:54 output. So first time you go have the loop variable fruit, it

08:01 on the value apples and goes to statements and Prince, we have fruit

08:08 apple. So we have apples. time it takes the next one bananas

08:15 we have Prince the thing and it's and so on. So you have

08:22 , bananas, grapes oranges and then done because there's nothing else in the

08:28 the list that we are taking the variable is taking values from.

08:37 Um So here is we'll just spend minute go over this one more

08:44 Now, that was one example. try to think how that fits in

08:51 generally what uh four loop is supposed do as you enter the four

08:58 The first thing that the python logic look at is are you done with

09:05 items in the sequence? If you're then you're done. The loop is

09:10 . If you're not it's saying assigned element to the loop variable and then

09:16 all the statements in the loop. , so let's look at this uh

09:27 , in terms of how this uh general logic works for our particular

09:35 So when you start you have your value that the variable fruit texas,

09:45 . So once you go in you see, are you done with

09:48 items in the sequence? No, number of elements left. So it

09:53 no. And then assigned next item the loop variable. Actually, that's

09:58 point at which fruit is assigned And then it says execute all statements

10:04 the body. The only statement here this so you execute, we have

10:10 and then when you're done, you back here again, it asks,

10:14 you done with everything? You're done apples, but you're not done with

10:18 here. So you said then take next item. So you take

10:24 and then you print we have bananas then you go back repeat that for

10:30 , repeat that for oranges. And after that you go 1234. And

10:36 that point this logic asks, are done with all elements in the

10:41 The answer is yes and you're So hopefully this is super clear that

10:49 particular way of execution is not just , you know, apples,

10:55 It is for any sequence of elements might be there in the uh in

11:02 list of elements. Okay, so look at one more example of this

11:14 , very general construct. The example have in front of us is summing

11:21 bunch of numbers. So look at program some equal zero sum equals sum

11:27 one, sum equals sum plus two three plus four plus five. And

11:31 you print the sum. Now. , this could be more concise.

11:37 ? So you're just going adding one adding to than adding three, then

11:42 four and five. There's a clear here, that's one thing. And

11:47 any case, even if there wasn't structure, you're executing a very similar

11:53 many times. And that's what the four construct does for you. It

12:01 you to express this kind of a in a much more compact fashion.

12:08 let's see what we can do. here is here is our same old

12:15 and here is how it looks with for loop, whether with this edition

12:24 one after the other is also something the accumulator pattern that will get back

12:30 in this course um at multiple So to look at this for a

12:38 , um now you have four Again, the loop variable is number

12:43 the list of elements is 12345, that's 12345. So if you um

12:54 here then um it's just summing this inside the loop is essentially doing exactly

13:04 same thing as each of these statements that instead of one or two or

13:09 , four or five, it has variable number and when we execute the

13:15 will first are the value one, the value two, then the value

13:19 , then the value for then the of five. So you should get

13:22 the same results in both these so hopefully this should be pretty

13:35 Okay, now this is the same here, nothing has been changed,

13:40 been copied over and as I this kind of computation is fairly common

13:52 , um this was only tell from to 5? Uh it's more compact

13:59 the previous code you have that had edition statements, but uh it's still

14:07 . So what if this edition or of this series wasn't from 1 to

14:14 , it was from 1 to 100 1 to 1000. So at some

14:19 this becomes like you keep writing And it gets longer and longer.

14:26 ? So that is problematic. You have to type 12345, 600 102

14:32 100 so on. So it stands reason that there should be a more

14:39 way of expressing what you'd like get . And in fact there is just

14:46 patterns like this that's called the range , another very important component of python

14:56 . So next few minutes we're going just see what the range function can

15:01 for us. So back to that that we had before. Let's the

15:13 that we had before was here with difference. It was 12345. We've

15:21 a zero here, that was not . But as you can tell,

15:27 a zero in this program is not make any difference. So we'll temporarily

15:34 make a life easier. Put a there and you'll see in a second

15:38 ? So the computation is sum equals plus number. And first time you

15:43 zero, which is meaning less. then you continue to add +12345.

15:49 the reason we put a zero, is uh the to introduce the range

15:57 , this program here can be replaced the program here with this entire sequence

16:05 by a range of six. So of sex means the sequence a set

16:15 elements 012345. Notice that it starts zero, not one and it ends

16:25 55 is six minus one, not . So it does have six elements

16:31 the first element is 012345. If count it has six elements. So

16:37 rain six. The six elements with with zero and each subsequent element is

16:44 more than the previous element. So find this by the way in general

16:49 computer signs that counting often starts with , not one, so might as

16:55 get used to it. So this program is exactly the same as

17:00 it will compute the sum with some is some plus zero plus one plus

17:06 plus three plus four plus five. this program here, we produce the

17:11 here should also produce exactly the same . Next couple of minutes we focus

17:18 on what else can we do with range function general thinking of it in

17:25 general terms than this specific case. the first issue is what if it

17:36 important not to have zero. Now can imagine that in this series in

17:43 series it did not matter. But if instead of plus you had multiplication

17:49 ? Your goal was to some to multiplied by one, then add multiplied

17:55 two and so on. And in case if you multiplied by zero,

18:03 the answer will be very very So sometimes you don't want that zero

18:08 we just added for convenience in this . So there is something we can

18:14 about it. Two more um accurately this list as a range function you

18:25 to this example. So the only the court here is different from the

18:31 one. In the previous page we range six and here we have range

18:39 comma six. So range one comma is exactly this. So one the

18:46 parameter says you start counting, you the series at one then you keep

18:53 and your last in the series will one less than six. That would

18:57 fine. So let me give you . This program will be exactly the

19:02 sum equals equals sum plus one sum sum plus two until some plus

19:09 Okay. All right Now what if wanted to go from 1-1000? No

19:26 deal. The only thing that has is instead of five, you have

19:31 the previous example you have 1000. for the five, the corresponding um

19:38 here was 600 range function. There's one more than five. So from

19:44 we have 1,001. So um this be a large number. It doesn't

19:50 . The court is just as Now what let's make it a little

19:57 more complex. What if you wanted total even numbers from 1 to

20:04 So here we are totaling all numbers 1 to 1000. Now this is

20:09 little bit more complex. You only to total 2468, 10 up to

20:17 the last one would be 1000. actually the range function does have the

20:24 to do that to that we have looked at yet. So this is

20:35 you do that. So basically the element of this range function is to

20:45 then it is showing that the last should be one less than 1001.

20:52 from 1000 you have 1,001 and here last one is the step. So

21:01 all previous examples, this step was that means you started the to the

21:07 will be 345678. But you can the step here. The step is

21:14 as to that means every time you your uh this the item or the

21:23 that the loop variable will take by . So you will have 2468 and

21:30 will go all the way until So again, if this was not

21:38 , the step parameter was not The step is assumed to be one

21:43 you can give any step parameter that that you want. You could give

21:48 , any individuals here. Okay, there is a general counting kind of

21:58 sequence of numbers or a series. range function can capture more similar simple

22:07 . So here is a general definition the rain function range and produces integers

22:17 zero to n minus one. This basically a review of everything we've seen

22:22 the last few minutes range. Start producing sequence of integers. First one

22:28 start and the last one is stop one. Okay. And then we

22:37 range, start, stop and So this produces a sequence starting with

22:46 going up to stop but not including , whatever it is, that is

22:52 than stop. It could be stopped or it could be something else but

22:56 the counting reaches stock or exceeds that we don't uh that will not be

23:06 . So the number before that would the last one. And step is

23:10 integer. So here is where we'll with start and start the steps,

23:15 this two step and so on until reach or exceed. Stop. And

23:20 when that's when we end. So should be fairly straightforward I hope.

23:32 one last thing. Um what if wanted to count backwards. Okay,

23:41 the trick here is that all this we assume that step is a positive

23:48 but we never explicitly said that it to be a positive integer. So

23:55 can um count backwards by just making step negative. So the simplest case

24:04 -1 for step for a negative and is an example of something that will

24:14 backwards until from 10 to 1. , so basically it the the variable

24:26 increased by one every time. So will go from 10 nine, all

24:34 way until it reaches the other. the uh with the detriments of of

24:42 and uh it will actually stop at because The it um the two would

24:55 one. Um Yeah, the sense limit is is one here. All

25:09 , we're going to stop this lecture . There are some interesting cases with

25:17 range function and and the step. and the best way to figure

25:24 you know, you know what what if, what if and the

25:27 way to figure that out would be just, you know, run the

25:32 function on your interface with various combinations see if your intuition with what it

25:39 do matches with what python does. if there are any questions then we

25:45 discuss, I'll stop here and we see you at the next in class

25:52

-
+