paint-brush
Python for Beginners, Part 9: Loopsby@cleverprogrammer
156 reads

Python for Beginners, Part 9: Loops

by Clever ProgrammerMarch 30th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this one, we explore the concept of looping constructs, visually, in computer programming. In the previous part of the series, we talk about functions with their arguments and functions with multiple arguments. We also talked about how to download and install Python, how to download and install the turtle module, interactive shell vs. script, fun functions and strings.

Company Mentioned

Mention Thumbnail
featured image - Python for Beginners, Part 9: Loops
Clever Programmer HackerNoon profile picture

In this one, we explore the concept of looping constructs, visually, in computer programming.

In case you missed it, here are the previous parts of the series

Transcript

0:00 Okay, so now that we've talked a little bit

0:03 about functions with their arguments

0:05 and also a little bit about functions

0:07 with multiple arguments, this naturally leads us

0:11 to the next thing. What if I want to make that one square

0:16 but I want to make it multiple times?

0:19 Sure, I can copy it and paste this line

0:23 and paste it again, right,

0:26 and then paste it again,

0:28 so and and so forth. So, for example,

0:30 let's try having it pasted twice.

0:33 I'm gonna remove these comments here,

0:36 and just so you remember, this 50 goes in for the length,

0:40 it replaces every part of the length with 50.

0:43 This 90 goes for the angle, it replaces every angle with 90.

0:47 That's how it works, okay.

0:49 So let's go back real quick and simply run this now.

0:54 Okay, so I'm gonna hit the shortcut key to run it.

0:59 And you can see I said draw two squares and it did.

1:03 But, here is the most amazing concept in programming,

1:09 which is the concept of doing things over and over again,

1:12 also known as loops.

1:14 So if I said I want you to do 10 pushups,

1:19 that's like a loop.

1:22 And every time you do a pushup

1:23 I count one, two, three, four.

1:25 And then once you hit 10 I stop automatically.

1:28 So a way to say that here is you can say

1:32 for pushup in range 10.

1:35 So for each pushup for 10 times, do this.

1:40 And this pushup part, I can call it whatever I want.

1:42 I can call it this,

1:44 I can call it I,

1:46 we'll just keep it simple and leave it at one letter I.

1:50 And what we're gonna do is,

1:52 all this part says is run something 10 times, okay.

1:58 That's what that part essentially says.

2:01 If I run that code on the left hand side,

2:03 if I show you what a range of 10 gives me,

2:05 it gives me this.

2:07 So when I do range 10 it won't really show you,

2:10 it kinda hides what it gives you,

2:12 but in reality it gives you a list of 10 numbers, okay.

2:17 So we're gonna talk about more things like lists

2:20 and integers and floating points in our next video.

2:23 But for now, I just want you to think of it

2:26 like it gives us 10 things and so it runs 10 times.

2:30 Okay, so if I did for I in range 10,

2:35 and I said print I,

2:39 what actually happens is the first time in this list,

2:42 the first time you run through in this loop,

2:45 I is zero.

2:46 Then the next time,

2:48 this range 10 is really this list here,

2:52 so I is zero the first time then it's one,

2:55 then it's two, then it's three, then it's four.

2:57 And each time what happens in the loop

2:59 is the part that's indented inside of the loop,

3:02 that's the part that runs over and over and over again.

3:06 So what happens the first time I is zero,

3:08 we say print zero and we get the zero right here.

3:11 Then we say I is one, so I is one over here,

3:14 and then it says print one, we get one over here.

3:18 Then in the last one, I turns out to be nine,

3:21 so we print nine and then we get this nine over here.

3:25 Okay, that's the basic idea of how the loop works.

3:29 So the part that we want to run over and over again,

3:34 we're going to put it inside of this loop.

3:36 Notice the colon at the end, very important.

3:39 And I'm gonna put this part inside of the loop.

3:42 And I don't even need to put that,

3:44 I can just do this

3:48 and I'm basically saying,

3:49 do this thing here, draw a square, four times.

3:53 That's all I'm saying, okay.

3:55 And I will replace this code in that regular way

3:59 without a loop and I'll prove it to you.

4:00 So first let's run it like this.

4:08 Dang, that was cool, right?

4:11 That was really frickin cool.

4:13 It just ran that four times in a row.

4:15 Now, I can show you what I mean, okay,

4:21 and I'm gonna comment out these lines.

4:26 And you can see it's gonna draw that

4:28 same four squares that we just drew, okay, just like that.

4:32 But by using a loop, your code gets a lot more clear.

4:37 What if you didn't want to run it four times?

4:38 What if you wanted to run it one million times, yeah?

4:42 It would be a lot harder to write

4:44 that out one million times,

4:45 but it's really easy to write one million in here.

4:48 I think that's one million.

4:50 That's 10, 100, 1000,

4:54 10,000, 100,000,

4:56 1,000,000. That's actually 10 million.

4:59 So there you go, 10 million.

5:02 So this thing is gonna run 10 million times.

5:05 If I ran it, and I'm gonna close it really quickly

5:08 cause this is gonna keep running forever,

5:10 but you'll notice that it just keeps overlapping

5:13 on the same square and it's gonna do that

5:15 10 million times so if I went to sleep

5:17 and woke up this will probably still be happening

5:20 and then it'll still be happening after that.

5:23 So let's exit that before something crazy happens.

5:29 Okay, so that's loops for you guys.

5:33 That is it for this video,

5:35 I will see you in the next video.