Python For Loops - Programming for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everybody today we will talk about for loops a for loop is a control instruction which we use for two main purposes iteration and repetition let me show you what i mean with an example iteration means performing the same action on different items so if we have a list of guitar strings for example and we would like to print it to the console item after item we can of course manually type six print statements in six separate lines of code but why would we do that if python has a much easier and much more intuitive alternative the for loop so let's consider this code example to achieve the exact same results so as you guys can probably guess for loop statements begin with a keyword for right after we include a special variable which represents each of our list items one at a time we often call it the loop control variable or iteration variable and it is very common to use the letter i when declaring it and we of course don't have to use the letter i we can use any other valid variable name such as item for example so the naming part is entirely up to us next we include the in keyword followed by the name of the list we would like to loop over the list of guitar strings and we will finish this declaration with a colon to indicate we're entering the body of the for loop now you guys may have noticed that the body of our for loop is indented meaning we added a tab character or maybe three space characters in front of our commands and we absolutely have to include this indentation otherwise our for loop will fail lastly we include the iteration variable i inside a single indented print statement and then each time we pass through the loop a different list item will be printed one after the other let's look at another example where we recreate a very famous destiny's child song so this time we have a list of names and we concatenate or add the exact same sentence to each of the names since we have three names in our list our for loop will repeat itself three times and each time our iteration variable n will have a different value in the first pass through the loop n equals kelly in the second pass through the loop and equals michelle and in the last pass it of course equals beyonce note that we have an additional print statement which we did not indent there are no tabs or spaces in front of it this statement will not be repeated because it's not inside the body of our for loop it's outside of our for loop and therefore on a different scope cool so this is how you use for loops to iterate over lists later on this course i'll show you how to do it with other data structures another purpose of for loops is repetition performing the same action more than once we often combine it with a range function which we use to quickly create lists of integers for example range 10 will create a list that starts with 0 and ends with 9 while range 3 will create a list that also starts with 0 but ends in 2. i won't go into too many details here as i'm planning to cover range much more extensively in future lessons the important thing to know is we can combine range when declaring a for loop so let's say we want to recreate a famous nirvana song in this example in the first pass through the loop our iteration variable i will equal 0. in a second pass it will equal 1 and in the last pass it will equal 2. now because we didn't include i inside the body of our for loop we cannot really see how it changes from past to pass what we can see however is our indented print statement repeats itself three times which is an exact match to the argument we specified in the range function so this is a cool trick we can use to quickly repeat lines of code and later on this course i'll show you how to do the exact same thing with while loops and thank you so much for watching this python basics tutorial please let me know if you found this video helpful or if you're looking for something a little bit more advanced i always appreciate your feedback and i plan my lessons accordingly if you guys like this video you already know the drill please leave it a like maybe a comment maybe subscribe to my channel and turn on the notification bell if you want to be extra awesome please share this video with your friends with your family with all the pythoneers you know and they'll be very very happy now thanks again and i'll see you soon
Info
Channel: Python Simplified
Views: 12,902
Rating: undefined out of 5
Keywords: python, python programming, python for beginners, pythin basics, python baisc, python for loop, for loop, for loop statement, programming, coding, python coding, learn python, python course, loop, iteration variable, control flow, control instruction, loop control, loop control variable, indentation, indent, iteration, repetition, python repetition, list iteration, code example, code lesson
Id: dHANJ4l6fwA
Channel Id: undefined
Length: 5min 19sec (319 seconds)
Published: Mon Jun 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.