Screen Link:
https://app.dataquest.io/m/312/lists-and-for-loops/9/for-loops
Hi, for more times I read and re-read the explanation, I can’t get my head around for loops. Anyone can give some advice and how to learn them?
Thanks
Screen Link:
https://app.dataquest.io/m/312/lists-and-for-loops/9/for-loops
Hi, for more times I read and re-read the explanation, I can’t get my head around for loops. Anyone can give some advice and how to learn them?
Thanks
If I give you this sentence -
Hi. My name is A
and ask you to write down all the words in the above sentence as they appear, what would you write down?
Simple enough -
Hi
My
Name
Is
A
Those are all the words in order. You read one word, and you wrote it down in that order.
That’s essentially what for
loops do.
They iterate over some data container and give us whatever is inside that container, one-by-one.
By now, you have covered at least one such container in Python. That is a list. For example,
a = [1, 2, 3, 4]
a
is a list in Python. And the list contains 4 items in it. Using a for
loop, we can obtain the items in that list -
for item in a:
print(item)
The above for
loop, iterates over each item
in the list a
and then prints out that item
.
Think of an arrow underneath the first item (that is 1
) in the list. The arrow is just there pointing towards 1
.
The for
loop essentially moves that arrow around and returns whatever value the arrow is pointing at. This value is stored in the variable item
.
So, in the above loop I shared, the arrow is pointing at the first item
, which is 1
. So, the first iteration of the loop will have the arrow pointing at the first item
and it will then run the code inside the loop.
The code inside the loop is print(item)
. So, the loop prints out 1
.
After the code inside the loop is finished running, it will move onto the next iteration. The arrow now moves by one step and is now pointing to the second item in the list, that is 2
.
So, now item
is 2
and the code inside the loop runs again. The loop now prints out 2
.
And the next iteration happens, the arrow moves again, the value in the arrow is stored inside of item
and the code inside the loop runs. The process keeps repeating till we reach the end of the data container, our list a
.
Let’s go back to the first example I shared -
Hi. My name is A
Let’s say, we have a list that stores the words in the above sentence.
So, we have
sentence = ["Hi", "My", "name", "is", "A"]
And now you have to use a for
loop to print out each word in the list sentence
in order.
You will have -
for word in sentence:
print(word)
The above is the same as the example with the numbers in a
. Everything happens the same way -
sentence
.word
. So, word
currently stores the value "Hi"
word
which has the value "Hi"
in it.sentence
.sentence
.The reason I am sharing this example again is to point out that previously we used for item
and now we are using for word
. The item
and word
are just variable names that store whatever is inside of your data container for that iteration. This is helpful because you can then do whatever you want to that value using that variable name.
This should, hopefully, clarify this for you. Take your time. Go through the above multiple times.
IMPORTANT
Over time, you will learn about variations to the above as well. But understanding the basics above is important. So don’t be worried when those variations start showing up.
Thank you for the explanation.
How do you understand what iteration variable to use at the beginning of a for loop. In some examples I see Value, and in the solution I saw row. My brain wanted to use rating because thats what I was looking for but am not sure if there are defined parameters to be using when building the loop.
Or am I thinking about this incorrectly and you literally are defining the variable name directly after “for”?
Yup, you got it! Since you are defining the variable within the loop, you can use any variable name you’d like but always try to use something that is descriptive over generic. I like your way of thinking in wanting to use rating
rather than row
.