Python for Beginners, Part 20: For Loops by@cleverprogrammer

Python for Beginners, Part 20: For Loops

Clever Programmer HackerNoon profile picture

Clever Programmer

Clever Programmer is a community with over 100,000+ students who are learning to code by building real world projects.

Let's talk about 'for' loops and why they are so important! Anything that requires repetition. 'For' loops are your friends!

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


0:00 hey guys what's up in this video I want

0:02 to talk about for loops and how they

0:05 work okay so we're going to get into

0:07 loops you saw that we did a little bit a

0:09 lot earlier in when we were using the

0:13 turtle module so hopefully there are a

0:15 little intuitive to you by now but now

0:18 let's use them for more and guess

0:19 realistic purposes right things to do

0:23 with numbers and manipulation of data so

0:27 not only can you use it for visual

0:29 graphics but you can use it for anything

0:32 now that's what I'm going to teach you

0:33 so if you're making your own game you'll

0:35 be able to use it after learning these

0:37 things or if you were creating your own

0:40 script maybe you're at a job whatever is

0:44 the case you're going to become more

0:46 familiar with loops and it's going to

0:48 help you do a lot of powerful things

0:52 okay

0:52 so let's get started how loops work

0:55 essentially it's a way of doing

0:57 something multiple times whenever you

0:59 think hmmm I need this done a lot of

1:02 times what can I do for example if

1:05 somebody told you print out numbers from

1:08 1 to 2

1:09 you'll be like ok I'll just write print

1:11 1 in print too good but now what if that

1:16 same person told you print numbers from

1:18 1 through 10 you'll say okay I'll just

1:20 write print 1 print to print 3.4 print 5

1:23 all up to print 10 whoo a lot of typing

1:27 but hey it wasn't so bad now let's say I

1:31 tell you to print up to 1000 could you

1:37 type that of course you can would it

1:40 take you maybe 2 hours 3 hours maybe

1:42 you'll copy paste you'll do tricks it'll

1:45 be harder now if I tell you type in a

1:47 million is going to get much much much

1:49 harder okay so for loop helps us

1:51 overcome the burden of doing repetitive

1:54 tasks so let's try something okay let's

1:58 say I want to do something five times

2:00 that's how I would say it okay I want

2:02 you to look at it less from Python

2:04 syntax specific perspective of I want to

2:07 zoom in microscopically and what what is

2:10 I it's hurting my eyes and what is range

2:12 and what does it do and I'm

2:13 getting a headache over here and where

2:15 do I put a colon where I want you to

2:17 read this as do something five times

2:20 okay that's how I want you to read it do

2:24 something five times that's what this is

2:26 saying and here I'm going to say print

2:28 hello so if I told you do do print hello

2:33 five times that's how you would think of

2:35 it you would go okay print hello five

2:38 times you'll say do something five times

2:40 and that something is print hello

2:43 okay I want you to get in an intuitive

2:46 sense of this so then you can reproduce

2:48 these and do them on your own away from

2:51 the screen okay so for example now let's

2:54 run this code and let's see what it does

2:56 and you can see print it out hello

2:58 multiple times now you might be asking

3:00 what is I if you don't already know you

3:03 can call it whatever you want you can

3:05 call it banana the code will still run

3:07 what we need to do is break this one

3:10 break this down one line at a time if we

3:13 need to look at it more introspectively

3:15 and see what's going on so what is range

3:18 of five well let's check it out let's

3:20 see what range of five is so I'm going

3:22 to print this out range of five is

3:25 really a list of numbers from zero

3:27 through four okay so to use loops you

3:31 need to know what a list is since we've

3:33 covered what a list is that's there you

3:36 go so range of five really evaluates to

3:40 this okay when you write the reason why

3:43 you should write lists here is because

3:45 python hides it it secretly writes it

3:47 and you can't tell what it is and

3:50 especially since you're starting out

3:52 this is going to confuse you when it

3:53 says that okay when you get more

3:55 advanced you'll know why it says this

3:57 but for now I'm gonna not hide it unhide

4:00 it and take out the actual form of what

4:03 it looks like and I know that if I call

4:05 list it'll show you like the actual type

4:07 of what what it's doing under the hood

4:09 so essentially whenever you see a range

4:13 of five

4:13 you should think oh that's just this

4:17 it's a list containing five elements

4:19 starting from zero all the way up to

4:21 four okay so now we can try we can start

4:25 to break all of this

4:26 down we can go okay if range of five is

4:30 really this hmm so range of five is

4:32 actually that zero one two three for the

4:36 first time right let me write this code

4:39 here let me let me change this hello and

4:41 let me change it with an eye okay and

4:44 let's let's do that

4:45 you see zero one two three four hmm what

4:48 happened there so let's see the first

4:51 time we ran the code I is zero so it

4:54 prints out zero the second time around

4:55 the code I went and became the second

4:58 element of the list so I became one then

5:00 where I was here it became one it

5:03 printed out the one then I became two it

5:05 put two here printed out two then I

5:07 became three three four and printed out

5:10 four and there you go you see all of

5:12 that on the right hand side zero got

5:14 printed then one got printed and two

5:16 that's why it's all separated by a line

5:18 okay so that's really what that I there

5:22 does could we call this number yes we

5:27 can and that's actually a better

5:28 variable name so my suggestion to you

5:31 especially if you're starting out with

5:32 programming never ever use one letter

5:36 variables never okay so even if you

5:41 think you have you can use an I here or

5:42 an X or Y don't do it take your time and

5:46 pick up pick better variable names so

5:50 I'm going to change this with number

5:51 because number is that variable this

5:55 variable we created on the fly and I'm

5:58 going to say from zero through four now

6:00 let me tell you something about the

6:02 range function in Python the range

6:04 function take start and stop okay but

6:08 it's kind of like this if you have to

6:11 think about it mathematically it

6:12 includes this point which is why I put

6:15 this here and it goes up to but not does

6:18 not include the stop okay so the range

6:21 function takes in two arguments start

6:23 and stop so if I gave it one or let's

6:26 say I gave it zero and let's say I gave

6:28 it ten you get back let's just do two

6:31 because I'm lazy oh you get back

6:34 zero one and two well what if I wanted

6:37 to print up to from

6:39 2:40 right what if I wanted to do that

6:42 well I can certainly write a list from

6:45 20 to 40 right I can go 20 21 22 23 24

6:51 all the way up to 40 but that would take

6:53 a long time let's use range to generate

6:55 that on the fly let's start with 20 and

6:57 go up to 41 but not including 41 it

7:01 excludes the 41 so this actually

7:05 translates to a list starting from 20

7:07 going up to 40 and if we print it out

7:09 you can see goes from 20 all the way up

7:12 to 40 so loops are great now in the next

7:19 video I want to talk about while loops

7:20 before I get to that I want to tell you

7:22 something so up until now we have done a

7:24 lot of Python syntax stuff learn the

7:27 basics of Python and how it works

7:31 programming specific syntax where do you

7:34 put the colon or did he do this now

7:37 after this point especially after the

7:39 while loops we're gonna get into

7:41 exercises and we're gonna kind of drill

7:43 and kill exercises I'm going to make

7:45 this the best Python course there is for

7:49 learning Python hands down what's

7:51 lacking from a lot of the online video

7:53 courses or exercises and I'm going to

7:56 give you guys so many exercises is going

7:58 to hurt your brain but in a good way

8:00 where as it makes you smarter and you

8:02 get better because without doing

8:04 exercises you will not get better you

8:06 can look at me write code all day long

8:08 you're not going to get better because

8:11 of that you need to do it on your own no

8:13 matter how simple it is

8:14 and while code Academy and all those

8:16 things are great the hand holds you

8:18 quite a bit which means that you're not

8:22 really learning a lot of those things

8:25 okay so we're gonna take a break from a

8:28 lot of these tutorials and do exercises

8:31 really get these ideas down okay what do

8:35 I mean by an exercise let's do an

8:36 exercise right here right now okay live

8:39 I'm gonna leave this here for you guys

8:42 because I want to leave this link into

8:45 the youtube description so you can

8:46 actually go look at these notes okay

8:48 so I'm gonna save this and I'm going to

8:50 leave this as notes

8:52 print numbers from 20 through 40

8:58 including 40 ha right or rather I can

9:03 say through to 41 and I can say

9:08 excluding 41 that's probably a better

9:10 way for me to write it because that's

9:12 how Python thinks a bit okay now let's

9:15 do an exercise let's say that we take

9:18 numbers from a list sum them up and

9:20 return them what do I mean if I give you

9:23 a list containing 1 2 3 you should give

9:26 me back 6 because 1 plus 2 plus 3 is 6

9:29 so if I gave you this list you should

9:34 give me back a 6 but now let's also

9:37 start adding together everything we've

9:40 learned so far

9:41 let's create a function okay that takes

9:44 in something and then does it so we're

9:46 going to build up to that

9:47 ok so first let's just write something

9:50 using a for loop that sums up all the

9:52 numbers in the list and then returns

9:54 them back to us okay or prints them out

9:56 back to us so let's do this let's say

9:59 count is equal to 0 so we're going to

10:02 use some variable to keep track of every

10:06 number so essentially then we can say

10:08 that if when we go through the loop we

10:11 can say okay I want you to add 1 to

10:15 count then when I want to go through the

10:17 loop again I wanna I want to say add 2

10:20 to count then when I go through the loop

10:21 again I want to say add 3 to count okay

10:25 and so in the start you'll have a count

10:28 being 1 then you'll have it being 1 plus

10:30 2 so 3 and then you'll have it being 2

10:33 plus 3 so 6 so you should get you know

10:36 arm back 6 so let's try it so I will say

10:41 for number in range let's say range what

10:45 will I say to range to create a list

10:47 from 0 to 3 or 1 to 3 basically I would

10:51 say 1 2 4 ok this creates a list of 1 2

10:57 & 3

10:57 ok

10:58 so I'm going to say range one through

11:01 four okay now let's do how can we

11:07 increment count okay I know that number

11:11 is going to be one the first time then

11:12 two then three that's not a problem so

11:14 number is good what we're getting with

11:16 number but how can I increment it to

11:18 count every single time here's what I

11:20 will do also count is equal to count

11:22 plus number okay what does this mean

11:26 here's what this means this says I want

11:31 the new count to be so the new count to

11:34 be here new count is equal to old count

11:38 plus number okay but so this is saying

11:43 my new variable count is going to be

11:46 what my old variable count was plus

11:50 number okay so the first time we go

11:52 through the loop the number is going to

11:53 be one old count is going to be zero

11:56 okay and so it's going to say now count

12:00 is one okay then when we go through the

12:03 loop the second time number is going to

12:05 be two oh by the way this count is going

12:07 to turn to one right because we

12:09 redefined what count is then what's

12:13 going to happen is that a number is

12:16 going to be two so essentially you're

12:18 going to get back half plus number which

12:21 is going to evaluate to count plus two

12:23 and number is two and count was one so

12:27 this is going to evaluate to three store

12:29 count as three then you're going to get

12:31 back count plus number here again right

12:33 and then you're going to get so the

12:36 number is now this time going to be

12:38 three and so you get back count which is

12:42 three plus three this evaluates to a six

12:45 and our loop ends okay so let's just

12:48 undo this damage that we have done and

12:52 let's check it out let's print count so

12:56 we run through this entire loop right

12:59 and once we're done running through this

13:00 entire loop we print out count it should

13:04 be

13:05 in our in this case six so let's check

13:08 it okay and you can see that count does

13:12 indeed give us back a six well that's

13:15 great that we wrote a four loop that

13:17 runs for the numbers one two and three

13:19 what if we want to write this for loop

13:22 and we want it to run right we want it

13:26 to run and sum up lists for any list

13:29 given to it any size list it could be a

13:31 size one list size 30 list size a

13:35 bajillion lists whatever okay so let's

13:38 see how we do it okay because practice

13:41 is key right how do you go from

13:43 conscious competence to in conscious

13:46 competence

13:47 that's how Jordan Belfort says it right

13:50 the guy in walls wolf of Wall Street the

13:53 movie oh-hoo-hoo wolf of Wall Street was

13:55 bait the person who wolf of Wall Street

13:57 was based on he defines two concepts

14:00 conscious competence and unconscious

14:02 competence what is conscious competence

14:04 that's you thinking about something and

14:08 then being good at it what's in

14:09 conscious competence you get so damn

14:11 good at what you do that you don't even

14:14 have to think about it and you do it

14:15 okay so how do we go from conscious

14:18 competence you're looking at your notes

14:19 he's really thinking about it and how

14:21 does a function work to like you just

14:24 writing code is practice okay there's no

14:27 there's there's no substitution for that

14:30 the only thing that you can do is hard

14:33 work okay there's no getting around it

14:35 there's no you can't watch somebody

14:37 else's videos or my videos and just sit

14:40 there and try to synthesize information

14:41 be like why am I not getting it you

14:43 can't just take notes all day from your

14:45 professor you have to try to do this

14:48 stuff on your own end of rant let's

14:52 continue so let's write this function

14:53 that can take in any size list and sum

14:57 it up okay so I'm going to say define

15:00 function okay so let's just write a

15:02 little note at the top so so we know

15:04 what we're doing right a function that

15:09 sums sums all elements of a list and

15:13 returns them okay that's what this is

15:16 doing that's what returns them let's

15:20 just put a little thing here like this

15:24 all right so let's write this function

15:27 I'm going to say define that's the first

15:29 step to defining a function and I'm

15:31 going to say a sum list okay so the

15:33 function is called sum list it takes in

15:35 one argument let's make that argument be

15:38 whatever my list okay that's you can

15:42 call it whatever you want you can call

15:43 ABC X Z but remember my rule try not to

15:47 think the name things as one bit of one

15:50 letter variable try to make it more

15:51 descriptive so I'm assuming I'm going to

15:54 get a my list or a list as input so I'm

15:57 going to call it my list so I'm going to

16:00 hit enter now what's the next part okay

16:03 hmm

16:04 let's think about it so the next part I

16:06 want to put here is I want to have a

16:11 counter variable right that's what we

16:13 had when we were writing this for loop

16:15 and I want to say for number in well

16:20 what's the list we're working with here

16:22 the list we were working with is one

16:24 through four right kind of like this

16:26 list so imagine if we had that list as

16:29 my list well we know it's called my list

16:31 whatever the list we get is called my

16:34 list and so just so you guys know what

16:38 we are kind of doing is eventually what

16:40 we want to be able to do is it's a sum

16:43 list and pass it to list one two three

16:46 and this should give us back a six okay

16:50 so we want to say assert that sum list

16:54 is this is equal to six

16:59 okay so this is a nice little test for

17:01 you guys which asserts things for

17:02 example if I write an assertion that's

17:04 false it's going to give me back false

17:06 so if I said five is equal to six my

17:09 code is going to yell at me hold on

17:11 I'm obviously having an error here

17:13 because I'm writing funky business but

17:15 you but you can say yes you can see I

17:17 get an assertion error it goes hey five

17:19 is not equal to six but if I go six is

17:22 equal to

17:22 six you can see that assertion gives me

17:25 a nice little three here right which is

17:28 which is or sorry does this give me

17:32 three there no I'm sorry yeah what so

17:35 assertion basically just doesn't give

17:37 you an error okay

17:38 when assertion works it just doesn't

17:40 give you an error okay so certian

17:44 doesn't give me back anything it just

17:46 gives me back no error when it works and

17:48 it gives me a big red angry

17:50 scary-looking error when it when it's

17:53 wrong so I want to have this assertion

17:56 here and why this assertion will be

17:58 helpful to me is it's going to test my

18:00 code okay

18:02 let's have another assertion and let's

18:07 say for and this should give me back 10

18:10 so I'm also teaching you guys how to

18:12 test your code so once I write this

18:14 function you should be able to call the

18:16 function by saying some list passing it

18:19 any list in this case this list 1 2 3

18:23 and it should give you back a 6 and it

18:25 should give you back a 10 when you run

18:26 this code if it doesn't give you an

18:28 error that means it's passed all of

18:30 these test cases if it fails it means it

18:32 failed one of these test cases and your

18:35 code is wrong all right let's keep going

18:37 so I want to say for a number in my list

18:40 right that's the list I get so remember

18:44 this list is going to be if we pass this

18:47 test case right if we give it this test

18:49 case and list is going to be 1 2 3

18:50 really right so let's just say my list

18:54 and what I want to do is what do I want

18:57 to do now I want to go count is equal to

18:59 count plus 1 ok so even though we're

19:02 doing it this exercise right now I'm

19:03 gonna have you guys do it again in the

19:05 exercises portion just so you can do it

19:07 from scratch for now just watch along

19:09 and get the concepts and how it works

19:11 ok so I increment count by what should

19:15 increment count by I don't want to

19:17 increment it by 1 that would be wrong I

19:19 want to increment it by the actual

19:24 number right so I want to say number

19:26 here oh I'm sorry I wrote this wrong I'm

19:28 silly I meant to

19:30 I meant incrementing by number up there

19:32 hopefully that didn't throw you off too

19:34 much okay

19:35 so but for you guys when you guys look

19:38 at the notes it's going to be fixed okay

19:40 I change it to number sorry about that

19:42 all right

19:43 so whatever count was plus the number

19:47 from list okay great and now at the end

19:52 we have to say return count okay just

19:57 format it a little bit nicer let's

19:59 remove some of these extra spaces and

20:02 let's save it again

20:04 and now I'm going to run it and if I

20:06 don't get back an assertion error which

20:08 I didn't that means that runs perfectly

20:11 imagine if okay so if I said one plus

20:13 two plus three should not give you a six

20:15 let's say I said give it it should give

20:17 me a seven you'll see that it gives me

20:19 an assertion error it says hey calling

20:21 some list on one two three does not give

20:23 me a seven so it should give you back a

20:26 six okay that's good

20:28 and just for sanity check let's run it

20:31 and print something let's say one two

20:33 what should that give us that should

20:35 give us a four right or sorry three one

20:38 plus two


Signup or Login to Join the Discussion


Related Stories