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, items 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 same results
so as you guys can probably guess
"For" loop statements begin with the 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 though the loop
a different list item will be printed, one after the other
let's look at another example
where we recreate a 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 throuigh the loop: n= "Kelly"
in the second pass through the loop: n= "Michelle"
and in the last pass, it of course equals "Beyounce"
note, that we have an additional "Print" statement which we did not indent
there are no "TAB"s or "SPACE"s 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 zero and ends with nine
while "range(3)" will create a list that
also starts with zero but ends in two
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 recrate a famous Nirvana song
in this example, in the first pass through the loop
our iteration variable "i" will equal zero
in the second pass, it will equal one
and in the last pass, it will equal two
now, because we didn't include "i" inside the body of our "For" loop
we cannot really see how it changes from pass 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 - pleased share this video
with your friends, with your family, with all the Pythoneers you know!
- and then I'll be very very happy!
now, thanks again! and I'll see you soon!