I'll start explaining how this code works and then we can look at the other one.
Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']
for genres in Genres:
print(genres)
At the first iteration, you assign the first item of Genres ('rock') to the variable genres, then you print it.
In the second iteration, you assign the second item of Genres ('R&B') to the variable genres, then you print it.
This is repeated for the lenght of the list (with every item). When you get to the end of the list, the for loop ends.
Let's now look at the other code
Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']
for Genres in Genres:
print(Genres)
Here, you're taking the variable Genres and reasigning it. Now in each iteration it will be the next item in the list. Python is intelligent and has the original list "stored", so it can keep iterating, but if we do after this code print(Genres)
, It will return 'pop'
, while the other code will return the whole list.
You should be careful when you reasign variables in python, because you will lose access to the information that was there before.
The difference is that in the first code, you create a new variable that changes every loop, genres, and change that variable, while in the second, you destroy the list to assign the reference of Genres to the corresponding item in the list.
To check it, you can compare
Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']
for genres in Genres:
print(genres)
print(Genres)
which will print each genre and then the full list,
Compared to
Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']
for Genres in Genres:
print(Genres)
print(Genres)
that will print each genre and then the last one again