Change language

Python For Loops – Programming for Beginners

Python For Loops - Programming for Beginners

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 mean s 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 lets 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 dont 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 were entering the body of the for loop now you guys may have noticed that the body of our for loop is indented mean ing 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 lets look at another example where we recreate a very famous destinys 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 its not inside the body of our for loop its 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 ill 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 wont go into too many details here as im 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 lets 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 didnt 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 ill 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 youre 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 theyll be very very happy now thanks again and ill see you soon

Python For Loops - Programming for Beginners Python For dummies: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method