How does Python know what to access in “for student in students” loop if the “student” variable hasn’t been defined?
students = [blah, blahh, blahhh] def function(list): for student in students: # will run loop for blah, then blahh, then blahhh and assign the neccesary variable
The for student is you clairifying a new variable to attach whatever
happens inside of the students list
student is a local variable (function scope) defined in the
for loop. The loop is a readonly, lookup operation that goes through each item in the list, in turn. The order is not important.
students = [lloyd, alice, tyler] for student in students:
student will be one object reference (dictionary) at a time, going through the entire list. These references are then passed to a function that can unpack the object attributes.
Left the part out that it stays within the function. Better said then I.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.