0
I haven't declared x anywhere. How does it know to reference the index of myarray? Python3, sample code provided.
From the following code: myarray = [20,10,44,928,200] for x in myarray: print(x) I haven't declared x anywhere. How does it know to reference the index of myarray? Is it because of the "in" clause? I know it works, but I'm trying to sort out HOW its interpreted. I cant settle for "it just works" I need the concept behind it. thanks all!
3 Answers
+ 8
The for statement performs the assignments to x. It is equivalent to
x=myarray[0]
print(x)
x=myarray[1]
print(x)
x=myarray[2]
print(x)
x=myarray[3]
print(x)
x=myarray[4]
print(x)
+ 8
Zachary Flynn each value is separated by a comma in myarray so x is the individual values within myarray ...
Yes -> The "in" keyword is what triggers the iteration process. It signifies that you want to iterate over the elements of the iterable.
0
For each element in myarray, assign that element's value to x and then execute the code inside the loop.