Problem in understanding the for loop

I am unable to understand how the following loops are working. Please explain me the logic behind the outputs.

Code1

friends=[‘gh’,‘ty’,‘py’]
for x in friends:
print(‘hello’,x)
print(‘finished’)

Output:
hello gh
hello ty
hello py
finished

Code 2

friends=[‘gh’,‘ty’,‘py’]
for x in friends:
print(‘hello’,friend)
print(‘finished’)

Output:
hello py
hello py
hello py
finished

Code 3

friends=[‘gh’,‘ty’,‘py’]
for x in friends:
print(‘hello’,friends)
print(‘finished’)

Output
hello [‘gh’, ‘ty’, ‘py’]
hello [‘gh’, ‘ty’, ‘py’]
hello [‘gh’, ‘ty’, ‘py’]
finished

First loop:

You iterate over friends variable, keeping every element from it in x variable. Then you just print it.

Second loop:

You iterate over friends variable, keeping every element from it in x variable. But you nowhere use it inside the loop. Instead you just print(‘hello’,friend). I suspect the friend variable is defined somewhere else, and has a value of py.

Third loop

You iterate over friends variable, keeping every element from it in x variable. But you also don’t use it. Instead you print friends list (3 times).

Thanks. I as running these codes in google colab. Now when I ran again, In the second code, it says ‘friend’ not defined.

Day before, I think colab had automatically assigned some value ‘py’ to ‘friend’.