The Importance of Understanding Loops in Programming
When it comes to programming, there are several concepts that are essential to master. One of the most important ones is loops. Loops are used to execute a set of instructions repeatedly, and they come in different types, such as for loops, while loops, and list comprehensions.
One type of loop that is commonly used is the for loop. A for loop allows us to go through a list right, we've done a lot of that but they're going to give us more practice cool on each iteration the variable num will be the next value in the list so the first time through it'll be seven then it'll be N9 then it'll be 12. This variable here right we've done plenty of practice with this in our previous videos so if you have but just to recall if this is the list right numbers is actually that list.
The for loop starts by iterating over a range object which can generate numbers from a given start value to a given stop value, or to a given step value. In the case of the list provided, we want to iterate over the elements in the list and print each one on its own line. The variable num will take the next value from the list on each iteration, so when it's 7 the first time through, num will be 7. Then when it's 9, num will be 9, then when it's 12, num will be 12.
Once we've iterated over all the values in the list, the loop will exit automatically because it detects that there are no more values to process. This is an important feature of loops that allows us to write efficient and concise code.
To demonstrate the use of for loops, let's take a closer look at how they can be used to iterate over a list and print each element squared on its own line. We want to see what 99 squared looks like so we're going to say for num in numbers: print(num squar. The square function will take an argument, which is num, and return the result of the multiplication operation.
When we run this code, it will print out each element in the list on its own line, with its corresponding value squared. For example, when num is 99, num squared will be 9801, which is printed to the console. If there are no more values in the list, the loop will exit automatically and we'll see "None" printed to the console.
For Loop Practice
One of the best ways to practice using for loops is through exercises. There are many resources available online that provide practice exercises in programming languages such as Python.
W3 Resource.com is a great place to start. It offers a range of exercises in different areas, including data types and list comprehensions. For example, we can choose to work on basic exercises with lists. The exercises will challenge us to write code that solves specific problems, and provide the solution for us to check.
The benefits of practicing with exercises are numerous. Not only does it help us learn new concepts, but it also helps us develop problem-solving skills and think critically about how to approach a problem. By working through these exercises, we'll be able to identify areas where we need more practice and improve our overall coding skills.
Conclusion
In conclusion, for loops are an essential concept in programming that allow us to execute a set of instructions repeatedly. They come in different types, including for loops, while loops, and list comprehensions. Understanding how to use for loops will help you write efficient and concise code, and will be essential for any career in computer science or software development.
By practicing with exercises and working through examples like the ones provided here, you'll become more comfortable using for loops and improve your overall coding skills. Remember that practice is key when it comes to mastering new concepts, so take the time to work through these exercises and you'll see improvement over time.
"WEBVTTKind: captionsLanguage: enhey guys what is up this is kazy from clever program.com welcome to another episode of Code Academy Python tutorial Series this is part 21 so I'm really bad with this numbers thing it's actually part 22 so where did we leave off last time we left off covering uh while Loops okay and we covered all of this all the way up to your own else while and we finished all of that so let's go just take a little peek at our course page just uh check where we are oh they changed that on us while we were in the middle of working on it they Chang the entire interface on us so here it is here are the loops and we are 42% of this and we're past the 60% barrier nice let's click on this loops and let's keep going so now we're going to work with for Loops I enjoy for Loops much more than wild Loops I did use S like wild Loops a lot till I started using four Loops a lot and now I just enjoyed them most of the times you can use four loops and they they're easier to work with and they will do what you want to do also a lot of the time stimes with wild Loops you have to be careful and make a condition that stops for for Loops that's never the case you have to give it that thing to stop and here we just say run 10 times this is pretty much saying that cuz range 10 gives you this list right we've talked about how range is a function that generates a list on the fly so it gives you this I is zero the first time right I is zero the first time then it's one then it's two and if it prints out 0 1 2 3 4 5 6 7 8 uh each time so let's go back and then as soon as it runs out of the list it stops right so they want us to make sure that we print out 10 so here what do they want us to do make the loop print the numbers from 0 to 19 instead of 0 to 9 so I should do 0 to 19 which means that I should really do 20 because zero is in included because we start from zero and 20 is excluded but 19 is included right we go up to 20 but not including 20 that's a general convention and here we have counting 0 to 19 perfect for your hobbies this is this kind of loop is useful when you want to do something a certain number of times such as aend something to a list so again for Loops are really useful we just keep looping through keep appending the thing to the list and that way our list keeps getting bigger so we're going to keep appending things so I can do create a for Loop that prompts the user for hobby three times and appends each one of those Hobbies okay they want us to get our hands dirty now so they want us to do something so we're going to say um all right so hobby is equal to Raw input what is uh your favorite right what is or what is one of your what is your hobby what are your hobbies sorry I'm I'm uh really caring about the user interface of this what is your hobby so let's just make it simple and we got that so that'll give us the string and then what we want to do is we want to ask this three times so it'll be a part of a for Loop fry range three CU we already know we need to run three times are we going to use this I no so I'm just going to make it that underscore cuz I don't think we're going to be using that variable we just want to run this loop three times and I'm going to say Hobbies do append hobby okay and then ultimately I can say print Hobbies what is your hobby hobby is um soccer another hobby Is Lifting and what is another hobby let's see I I went skydiving but that's not a hobby cuz I did it once and it was pretty fun scary at the same time that was two months ago that was amazing H let me think of another good hobby that I like to do so I have also started jiujitsu that's been amazing I love it let's put that and here you got that list soccer lifting Jiu-Jitsu this U is ignored by python it's just there to show you for some reason python 2 does it Python 3 does not do this okay so just ignore that you like it doesn't exist create a for Loop okay cool for your strings using a for Loop you can print out each individual character in a string you've already seen this so thing is equal to spam for C and thing print C so better way to say it for letter right so now they want us to do it add a second for Loop so that each character in word is printed out so I would say for character in word print character or I would say letter in word letter is simpler I will usually use letter letter in Word print letter let's run it let's check it out let's see what happens and you got exactly what we expected that you would get right spam exclamation mark um the first Loop right here and then the second Loop ran and we got exit exclamation mark right that's what we expected to happen Okay for your a string manipulation is useful in for Loops if you want to modify some content in a string so sometimes we want to change a string okay let's see so here they're just simply teaching showing you how to access each character in a word and print it out the example prints out M the first time a r b l e okay and here they show you the comma character after our print State means that our next print statement keeps printing on the same line so this is a nice little trick you can do to print everything on the same line okay so if I go here paste it does this actually work in Python 3 doesn't look like it but apparently in Python two it works right so let me check that same Loop in Python 2 what when it okay word is equal to Marble for a car in word print car comma okay so in um python 2 you can just put a comma at the end of it and then it'll print it all in the same line okay what would I do to print it out separately well one thing that coming to my mind is I would turn it into a list of list of a word so that separates it out like this and then I'd call uh join uh but I would say join it by one space and then I would go like this that's just how I would do it cuz I'm cool like that and then I can write print in front of it and you can do it too when you're that cool all right um our next print statement keeps printing on the same line let's filter out the letter A from our sting do the following for each character in the phrase hm filter out the letter A from the string so it should a bird in the hand not a bird in the hand do the following for each car character in the phrase if the character is an A or character is an a print X instead of character make sure to include the trailing comma okay so for character oops in phrase if character equal equal an a here I'll do a little trick but let's not get fancy let's just do it simple if the character is capital A or character is small a the trick that I did is just make the character in small or lower case and then just check it that way or make it all in uppercase and check it that way so it would be simpler so if the character is either capital little a so big a or small a uh replace it with an X so print X um otherwise instead of character make sure to include the trailing comma right so here we want to use it like Python 3 and include the trailing comma and then what we want to say is print character otherwise so or else right if that's not the case see X bird in the X HX and d right everywhere it's is oh actually in this case they do want uh also for us to change it to a small uh if it was a small a so this is then the correct way to do it okay cool we did it nice all right we just had to put the trailing commas to make sure everything was on the same line okay cool next lesson for your list perhaps the most useful and most common use of for Loops is to go through a list right we've done a lot of that but they're going to give us more practice cool on each iteration the variable num will be the next value in the list so the first time through it'll be seven then it'll be N9 then it'll be 12 so this variable here right we've done plenty of practice with this in our previous videos so if you have but just to recall if this is the list right numbers is actually that list first time num is going to be seven it's going to print out seven shoot it up into the screen into the terminal then num the variable num will be nine it'll say print n it shoot it out to the screen then it'll be 12 this will be 12 it shoot out to the screen I think you get the idea then it gets to 99 it'll print it out and after that it says the loop is over because it automatically detects it has uh reach the end of the loop or end end of the list write a second here it says and then the loop will exit when there are no more values in the list write a second for Loop that goes through the numbers list and prints each element squared on its own line so again I thought we've done this but okay cool let's it's good to do more practice oh they want us to do it on a separate line so we're going to say for Num in numbers print num squar okay all of it on its own line okay that's what they want so let's see what is 99 squar it is 9,81 which is what we get here and then after that we get none which means everything is over let's see this for your list okay which part are we at step up your fours nice so we just finished our video on the entire uh on the loops on all our four Loops okay so again that's a great one practice the for Loops more okay use that to practice the for Loops take this time to practice your for Loops just like in the last section I told you to practice to while Loops I would make this video longer about the usual 30 minutes however these concepts are so key the loops while loops and for Loops that I think that you need to go and practice them one good place you can go to is W3 resource.com for python exercises okay and you can simply choose the data type like kinds of exercises you want right so for example do you want basic exercises string the ones with list so here I would choose these on python data types list 34 exercises with Solutions so you can then check the solution simply click on the exercise and go okay write a program to sum all the items in the list can you do that if you can great write out the program if you can't great also go ahead and write out the program then it's perfect cuz now you get to practice and learn it only if you fail after trying at least for 1 hour 30 minutes or 1 hour then go and look up the answer for me when I was starting out programming it would take me I would be stuck on a problem for weeks okay and I Tred to never almost look at the answer which is probably not the best way to do it because you can be like pulling your hair out and going crazy however I get obsessive when I fall in love with something so I just truly enjoy but you know if you start getting you know your own frustration levels if you start to get frustrated go and look at the solution it'll help you learn it so here it is test out your for Loop knowledge here I'm going to stop the video right here okay this is kazy from clever program.com you just finished watching code academy Python tutorial number 21 we covered for Loops next video we will be talking about dictionary and how to use for Loops in dictionaries okay that's going to be another important key concept key value pair remember that all right enough of the lame jokes this is the end of the video please like it or subscribe I love you guys I will see you in the next video code academy Python tutorial number 22 number 22 I mean number 23 that's the one you guys should get excited forhey guys what is up this is kazy from clever program.com welcome to another episode of Code Academy Python tutorial Series this is part 21 so I'm really bad with this numbers thing it's actually part 22 so where did we leave off last time we left off covering uh while Loops okay and we covered all of this all the way up to your own else while and we finished all of that so let's go just take a little peek at our course page just uh check where we are oh they changed that on us while we were in the middle of working on it they Chang the entire interface on us so here it is here are the loops and we are 42% of this and we're past the 60% barrier nice let's click on this loops and let's keep going so now we're going to work with for Loops I enjoy for Loops much more than wild Loops I did use S like wild Loops a lot till I started using four Loops a lot and now I just enjoyed them most of the times you can use four loops and they they're easier to work with and they will do what you want to do also a lot of the time stimes with wild Loops you have to be careful and make a condition that stops for for Loops that's never the case you have to give it that thing to stop and here we just say run 10 times this is pretty much saying that cuz range 10 gives you this list right we've talked about how range is a function that generates a list on the fly so it gives you this I is zero the first time right I is zero the first time then it's one then it's two and if it prints out 0 1 2 3 4 5 6 7 8 uh each time so let's go back and then as soon as it runs out of the list it stops right so they want us to make sure that we print out 10 so here what do they want us to do make the loop print the numbers from 0 to 19 instead of 0 to 9 so I should do 0 to 19 which means that I should really do 20 because zero is in included because we start from zero and 20 is excluded but 19 is included right we go up to 20 but not including 20 that's a general convention and here we have counting 0 to 19 perfect for your hobbies this is this kind of loop is useful when you want to do something a certain number of times such as aend something to a list so again for Loops are really useful we just keep looping through keep appending the thing to the list and that way our list keeps getting bigger so we're going to keep appending things so I can do create a for Loop that prompts the user for hobby three times and appends each one of those Hobbies okay they want us to get our hands dirty now so they want us to do something so we're going to say um all right so hobby is equal to Raw input what is uh your favorite right what is or what is one of your what is your hobby what are your hobbies sorry I'm I'm uh really caring about the user interface of this what is your hobby so let's just make it simple and we got that so that'll give us the string and then what we want to do is we want to ask this three times so it'll be a part of a for Loop fry range three CU we already know we need to run three times are we going to use this I no so I'm just going to make it that underscore cuz I don't think we're going to be using that variable we just want to run this loop three times and I'm going to say Hobbies do append hobby okay and then ultimately I can say print Hobbies what is your hobby hobby is um soccer another hobby Is Lifting and what is another hobby let's see I I went skydiving but that's not a hobby cuz I did it once and it was pretty fun scary at the same time that was two months ago that was amazing H let me think of another good hobby that I like to do so I have also started jiujitsu that's been amazing I love it let's put that and here you got that list soccer lifting Jiu-Jitsu this U is ignored by python it's just there to show you for some reason python 2 does it Python 3 does not do this okay so just ignore that you like it doesn't exist create a for Loop okay cool for your strings using a for Loop you can print out each individual character in a string you've already seen this so thing is equal to spam for C and thing print C so better way to say it for letter right so now they want us to do it add a second for Loop so that each character in word is printed out so I would say for character in word print character or I would say letter in word letter is simpler I will usually use letter letter in Word print letter let's run it let's check it out let's see what happens and you got exactly what we expected that you would get right spam exclamation mark um the first Loop right here and then the second Loop ran and we got exit exclamation mark right that's what we expected to happen Okay for your a string manipulation is useful in for Loops if you want to modify some content in a string so sometimes we want to change a string okay let's see so here they're just simply teaching showing you how to access each character in a word and print it out the example prints out M the first time a r b l e okay and here they show you the comma character after our print State means that our next print statement keeps printing on the same line so this is a nice little trick you can do to print everything on the same line okay so if I go here paste it does this actually work in Python 3 doesn't look like it but apparently in Python two it works right so let me check that same Loop in Python 2 what when it okay word is equal to Marble for a car in word print car comma okay so in um python 2 you can just put a comma at the end of it and then it'll print it all in the same line okay what would I do to print it out separately well one thing that coming to my mind is I would turn it into a list of list of a word so that separates it out like this and then I'd call uh join uh but I would say join it by one space and then I would go like this that's just how I would do it cuz I'm cool like that and then I can write print in front of it and you can do it too when you're that cool all right um our next print statement keeps printing on the same line let's filter out the letter A from our sting do the following for each character in the phrase hm filter out the letter A from the string so it should a bird in the hand not a bird in the hand do the following for each car character in the phrase if the character is an A or character is an a print X instead of character make sure to include the trailing comma okay so for character oops in phrase if character equal equal an a here I'll do a little trick but let's not get fancy let's just do it simple if the character is capital A or character is small a the trick that I did is just make the character in small or lower case and then just check it that way or make it all in uppercase and check it that way so it would be simpler so if the character is either capital little a so big a or small a uh replace it with an X so print X um otherwise instead of character make sure to include the trailing comma right so here we want to use it like Python 3 and include the trailing comma and then what we want to say is print character otherwise so or else right if that's not the case see X bird in the X HX and d right everywhere it's is oh actually in this case they do want uh also for us to change it to a small uh if it was a small a so this is then the correct way to do it okay cool we did it nice all right we just had to put the trailing commas to make sure everything was on the same line okay cool next lesson for your list perhaps the most useful and most common use of for Loops is to go through a list right we've done a lot of that but they're going to give us more practice cool on each iteration the variable num will be the next value in the list so the first time through it'll be seven then it'll be N9 then it'll be 12 so this variable here right we've done plenty of practice with this in our previous videos so if you have but just to recall if this is the list right numbers is actually that list first time num is going to be seven it's going to print out seven shoot it up into the screen into the terminal then num the variable num will be nine it'll say print n it shoot it out to the screen then it'll be 12 this will be 12 it shoot out to the screen I think you get the idea then it gets to 99 it'll print it out and after that it says the loop is over because it automatically detects it has uh reach the end of the loop or end end of the list write a second here it says and then the loop will exit when there are no more values in the list write a second for Loop that goes through the numbers list and prints each element squared on its own line so again I thought we've done this but okay cool let's it's good to do more practice oh they want us to do it on a separate line so we're going to say for Num in numbers print num squar okay all of it on its own line okay that's what they want so let's see what is 99 squar it is 9,81 which is what we get here and then after that we get none which means everything is over let's see this for your list okay which part are we at step up your fours nice so we just finished our video on the entire uh on the loops on all our four Loops okay so again that's a great one practice the for Loops more okay use that to practice the for Loops take this time to practice your for Loops just like in the last section I told you to practice to while Loops I would make this video longer about the usual 30 minutes however these concepts are so key the loops while loops and for Loops that I think that you need to go and practice them one good place you can go to is W3 resource.com for python exercises okay and you can simply choose the data type like kinds of exercises you want right so for example do you want basic exercises string the ones with list so here I would choose these on python data types list 34 exercises with Solutions so you can then check the solution simply click on the exercise and go okay write a program to sum all the items in the list can you do that if you can great write out the program if you can't great also go ahead and write out the program then it's perfect cuz now you get to practice and learn it only if you fail after trying at least for 1 hour 30 minutes or 1 hour then go and look up the answer for me when I was starting out programming it would take me I would be stuck on a problem for weeks okay and I Tred to never almost look at the answer which is probably not the best way to do it because you can be like pulling your hair out and going crazy however I get obsessive when I fall in love with something so I just truly enjoy but you know if you start getting you know your own frustration levels if you start to get frustrated go and look at the solution it'll help you learn it so here it is test out your for Loop knowledge here I'm going to stop the video right here okay this is kazy from clever program.com you just finished watching code academy Python tutorial number 21 we covered for Loops next video we will be talking about dictionary and how to use for Loops in dictionaries okay that's going to be another important key concept key value pair remember that all right enough of the lame jokes this is the end of the video please like it or subscribe I love you guys I will see you in the next video code academy Python tutorial number 22 number 22 I mean number 23 that's the one you guys should get excited for\n"