Learn Python Programming - 20 - For Loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's up in this video I want to talk about for loops and how they work okay so we're going to get into loops you saw that we did a little bit a lot earlier in when we were using the turtle module so hopefully there are a little intuitive to you by now but now let's use them for more and guess realistic purposes right things to do with numbers and manipulation of data so not only can you use it for visual graphics but you can use it for anything now that's what I'm going to teach you so if you're making your own game you'll be able to use it after learning these things or if you were creating your own script maybe you're at a job whatever is the case you're going to become more familiar with loops and it's going to help you do a lot of powerful things okay so let's get started how loops work essentially it's a way of doing something multiple times whenever you think hmmm I need this done a lot of times what can I do for example if somebody told you print out numbers from 1 to 2 you'll be like ok I'll just write print 1 in print too good but now what if that same person told you print numbers from 1 through 10 you'll say okay I'll just write print 1 print to print 3.4 print 5 all up to print 10 whoo a lot of typing but hey it wasn't so bad now let's say I tell you to print up to 1000 could you type that of course you can would it take you maybe 2 hours 3 hours maybe you'll copy paste you'll do tricks it'll be harder now if I tell you type in a million is going to get much much much harder okay so for loop helps us overcome the burden of doing repetitive tasks so let's try something okay let's say I want to do something five times that's how I would say it okay I want you to look at it less from Python syntax specific perspective of I want to zoom in microscopically and what what is I it's hurting my eyes and what is range and what does it do and I'm getting a headache over here and where do I put a colon where I want you to read this as do something five times okay that's how I want you to read it do something five times that's what this is saying and here I'm going to say print hello so if I told you do do print hello five times that's how you would think of it you would go okay print hello five times you'll say do something five times and that something is print hello okay I want you to get in an intuitive sense of this so then you can reproduce these and do them on your own away from the screen okay so for example now let's run this code and let's see what it does and you can see print it out hello multiple times now you might be asking what is I if you don't already know you can call it whatever you want you can call it banana the code will still run what we need to do is break this one break this down one line at a time if we need to look at it more introspectively and see what's going on so what is range of five well let's check it out let's see what range of five is so I'm going to print this out range of five is really a list of numbers from zero through four okay so to use loops you need to know what a list is since we've covered what a list is that's there you go so range of five really evaluates to this okay when you write the reason why you should write lists here is because python hides it it secretly writes it and you can't tell what it is and especially since you're starting out this is going to confuse you when it says that okay when you get more advanced you'll know why it says this but for now I'm gonna not hide it unhide it and take out the actual form of what it looks like and I know that if I call list it'll show you like the actual type of what what it's doing under the hood so essentially whenever you see a range of five you should think oh that's just this it's a list containing five elements starting from zero all the way up to four okay so now we can try we can start to break all of this down we can go okay if range of five is really this hmm so range of five is actually that zero one two three for the first time right let me write this code here let me let me change this hello and let me change it with an eye okay and let's let's do that you see zero one two three four hmm what happened there so let's see the first time we ran the code I is zero so it prints out zero the second time around the code I went and became the second element of the list so I became one then where I was here it became one it printed out the one then I became two it put two here printed out two then I became three three four and printed out four and there you go you see all of that on the right hand side zero got printed then one got printed and two that's why it's all separated by a line okay so that's really what that I there does could we call this number yes we can and that's actually a better variable name so my suggestion to you especially if you're starting out with programming never ever use one letter variables never okay so even if you think you have you can use an I here or an X or Y don't do it take your time and pick up pick better variable names so I'm going to change this with number because number is that variable this variable we created on the fly and I'm going to say from zero through four now let me tell you something about the range function in Python the range function take start and stop okay but it's kind of like this if you have to think about it mathematically it includes this point which is why I put this here and it goes up to but not does not include the stop okay so the range function takes in two arguments start and stop so if I gave it one or let's say I gave it zero and let's say I gave it ten you get back let's just do two because I'm lazy oh you get back zero one and two well what if I wanted to print up to from 2:40 right what if I wanted to do that well I can certainly write a list from 20 to 40 right I can go 20 21 22 23 24 all the way up to 40 but that would take a long time let's use range to generate that on the fly let's start with 20 and go up to 41 but not including 41 it excludes the 41 so this actually translates to a list starting from 20 going up to 40 and if we print it out you can see goes from 20 all the way up to 40 so loops are great now in the next video I want to talk about while loops before I get to that I want to tell you something so up until now we have done a lot of Python syntax stuff learn the basics of Python and how it works programming specific syntax where do you put the colon or did he do this now after this point especially after the while loops we're gonna get into exercises and we're gonna kind of drill and kill exercises I'm going to make this the best Python course there is for learning Python hands down what's lacking from a lot of the online video courses or exercises and I'm going to give you guys so many exercises is going to hurt your brain but in a good way where as it makes you smarter and you get better because without doing exercises you will not get better you can look at me write code all day long you're not going to get better because of that you need to do it on your own no matter how simple it is and while code Academy and all those things are great the hand holds you quite a bit which means that you're not really learning a lot of those things okay so we're gonna take a break from a lot of these tutorials and do exercises really get these ideas down okay what do I mean by an exercise let's do an exercise right here right now okay live I'm gonna leave this here for you guys because I want to leave this link into the youtube description so you can actually go look at these notes okay so I'm gonna save this and I'm going to leave this as notes print numbers from 20 through 40 including 40 ha right or rather I can say through to 41 and I can say excluding 41 that's probably a better way for me to write it because that's how Python thinks a bit okay now let's do an exercise let's say that we take numbers from a list sum them up and return them what do I mean if I give you a list containing 1 2 3 you should give me back 6 because 1 plus 2 plus 3 is 6 so if I gave you this list you should give me back a 6 but now let's also start adding together everything we've learned so far let's create a function okay that takes in something and then does it so we're going to build up to that ok so first let's just write something using a for loop that sums up all the numbers in the list and then returns them back to us okay or prints them out back to us so let's do this let's say count is equal to 0 so we're going to use some variable to keep track of every number so essentially then we can say that if when we go through the loop we can say okay I want you to add 1 to count then when I want to go through the loop again I wanna I want to say add 2 to count then when I go through the loop again I want to say add 3 to count okay and so in the start you'll have a count being 1 then you'll have it being 1 plus 2 so 3 and then you'll have it being 2 plus 3 so 6 so you should get you know arm back 6 so let's try it so I will say for number in range let's say range what will I say to range to create a list from 0 to 3 or 1 to 3 basically I would say 1 2 4 ok this creates a list of 1 2 & 3 ok so I'm going to say range one through four okay now let's do how can we increment count okay I know that number is going to be one the first time then two then three that's not a problem so number is good what we're getting with number but how can I increment it to count every single time here's what I will do also count is equal to count plus number okay what does this mean here's what this means this says I want the new count to be so the new count to be here new count is equal to old count plus number okay but so this is saying my new variable count is going to be what my old variable count was plus number okay so the first time we go through the loop the number is going to be one old count is going to be zero okay and so it's going to say now count is one okay then when we go through the loop the second time number is going to be two oh by the way this count is going to turn to one right because we redefined what count is then what's going to happen is that a number is going to be two so essentially you're going to get back half plus number which is going to evaluate to count plus two and number is two and count was one so this is going to evaluate to three store count as three then you're going to get back count plus number here again right and then you're going to get so the number is now this time going to be three and so you get back count which is three plus three this evaluates to a six and our loop ends okay so let's just undo this damage that we have done and let's check it out let's print count so we run through this entire loop right and once we're done running through this entire loop we print out count it should be in our in this case six so let's check it okay and you can see that count does indeed give us back a six well that's great that we wrote a four loop that runs for the numbers one two and three what if we want to write this for loop and we want it to run right we want it to run and sum up lists for any list given to it any size list it could be a size one list size 30 list size a bajillion lists whatever okay so let's see how we do it okay because practice is key right how do you go from conscious competence to in conscious competence that's how jordan belfort says it right the guy in walls wolf of Wall Street the movie oh-hoo-hoo wolf of Wall Street was bait the person who wolf of Wall Street was based on he defines two concepts conscious competence and unconscious competence what is conscious competence that's you thinking about something and then being good at it what's in conscious competence you get so damn good at what you do that you don't even have to think about it and you do it okay so how do we go from conscious competence you're looking at your notes he's really thinking about it and how does a function work to like you just writing code is practice okay there's no there's there's no substitution for that the only thing that you can do is hard work okay there's no getting around it there's no you can't watch somebody else's videos or my videos and just sit there and try to synthesize information be like why am I not getting it you can't just take notes all day from your professor you have to try to do this stuff on your own end of rant let's continue so let's write this function that can take in any size list and sum it up okay so I'm going to say define function okay so let's just write a little note at the top so so we know what we're doing right a function that sums sums all elements of a list and returns them okay that's what this is doing that's what returns them let's just put a little thing here like this all right so let's write this function I'm going to say define that's the first step to defining a function and I'm going to say a sum list okay so the function is called sum list it takes in one argument let's make that argument be whatever my list okay that's you can call it whatever you want you can call ABC X Z but remember my rule try not to think the name things as one bit of one letter variable try to make it more descriptive so I'm assuming I'm going to get a my list or a list as input so I'm going to call it my list so I'm going to hit enter now what's the next part okay hmm let's think about it so the next part I want to put here is I want to have a counter variable right that's what we had when we were writing this for loop and I want to say for number in well what's the list we're working with here the list we were working with is one through four right kind of like this list so imagine if we had that list as my list well we know it's called my list whatever the list we get is called my list and so just so you guys know what we are kind of doing is eventually what we want to be able to do is it's a sum list and pass it to list one two three and this should give us back a six okay so we want to say assert that sum list is this is equal to six okay so this is a nice little test for you guys which asserts things for example if I write an assertion that's false it's going to give me back false so if I said five is equal to six my code is going to yell at me hold on I'm obviously having an error here because I'm writing funky business but you but you can say yes you can see I get an assertion error it goes hey five is not equal to six but if I go six is equal to six you can see that assertion gives me a nice little three here right which is which is or sorry does this give me three there no I'm sorry yeah what so assertion basically just doesn't give you an error okay when assertion works it just doesn't give you an error okay so certian doesn't give me back anything it just gives me back no error when it works and it gives me a big red angry scary-looking error when it when it's wrong so I want to have this assertion here and why this assertion will be helpful to me is it's going to test my code okay let's have another assertion and let's say for and this should give me back 10 so I'm also teaching you guys how to test your code so once I write this function you should be able to call the function by saying some list passing it any list in this case this list 1 2 3 and it should give you back a 6 and it should give you back a 10 when you run this code if it doesn't give you an error that means it's passed all of these test cases if it fails it means it failed one of these test cases and your code is wrong all right let's keep going so I want to say for a number in my list right that's the list I get so remember this list is going to be if we pass this test case right if we give it this test case and list is going to be 1 2 3 really right so let's just say my list and what I want to do is what do I want to do now I want to go count is equal to count plus 1 ok so even though we're doing it this exercise right now I'm gonna have you guys do it again in the exercises portion just so you can do it from scratch for now just watch along and get the concepts and how it works ok so I increment count by what should increment count by I don't want to increment it by 1 that would be wrong I want to increment it by the actual number right so I want to say number here oh I'm sorry I wrote this wrong I'm silly I meant to I meant incrementing by number up there hopefully that didn't throw you off too much okay so but for you guys when you guys look at the notes it's going to be fixed okay I change it to number sorry about that all right so whatever count was plus the number from list okay great and now at the end we have to say return count okay just format it a little bit nicer let's remove some of these extra spaces and let's save it again and now I'm going to run it and if I don't get back an assertion error which I didn't that means that runs perfectly imagine if okay so if I said one plus two plus three should not give you a six let's say I said give it it should give me a seven you'll see that it gives me an assertion error it says hey calling some list on one two three does not give me a seven so it should give you back a six okay that's good and just for sanity check let's run it and print something let's say one two what should that give us that should give us a four right or sorry three one plus two
Info
Channel: Clever Programmer
Views: 123,739
Rating: 4.8315134 out of 5
Keywords: python, python3, programming, for loops, loops, coding, walkthrough, guide, tutorial, python class, learn python, beginners, part 20, functions, looping, conditionals, control flow
Id: fBIvTEOTzvs
Channel Id: undefined
Length: 20min 39sec (1239 seconds)
Published: Thu Oct 20 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.