students_period_A = ["Alex", "Briana", "Cheri", "Daniele"]
students_period_B = ["Dora", "Minerva", "Alexa", "Obie"]
INFINITE LOOP CODE:
for student in students_period_A:
students_period_A.append(student)
print(student)
First Iteration of loop: student will be first element “Alex” and
students_period_A will become [“Alex”, “Briana”, “Cheri”, “Daniele”, “Alex”]
Second Iteration of loop: student will be second element “Briana” and
students_period_A will become [“Alex”, “Briana”, “Cheri”, “Daniele”, “Alex”, “Briana”]
Third Iteration of loop: student will be third element “Cheri” and
students_period_A will become
[“Alex”, “Briana”, “Cheri”, “Daniele”, “Alex”, “Briana”, “Cheri”]
Fourth Iteration of loop: student will be fourth element “Daniele” and
students_period_A will become
[“Alex”, “Briana”, “Cheri”, “Daniele”, “Alex”, “Briana”, “Cheri”, “Daniele”]
Fifth Iteration of loop: student will be fifth element “Alex” and
students_period_A will become
[“Alex”, “Briana”, “Cheri”, “Daniele”, “Alex”, “Briana”, “Cheri”, “Daniele”, “Alex”]
and this will continue forever. In every iteration, we are moving one step forward, but we are appending a new element/student in every iteration. We will never get to the end of a list which keeps growing. We are iterating over students_period_A
and yet we keep appending to the same list. We will never get to the last element of this list because we keep adding a new element.
CORRECT LOOP CODE:
for student in students_period_A:
students_period_B.append(student)
print(student)
First Iteration of loop: student will be first element of list A “Alex” and
students_period_B will become [“Dora”, “Minerva”, “Alexa”, “Obie”, “Alex”]
Second Iteration of loop: student will be second element of list A “Briana” and
students_period_B will become [“Dora”, “Minerva”, “Alexa”, “Obie”, “Alex”, “Briana”]
Third Iteration of loop: student will be third element of list A “Cheri” and
students_period_B will become
[“Dora”, “Minerva”, “Alexa”, “Obie”, “Alex”, “Briana”, “Cheri”]
Fourth Iteration of loop: student will be fourth element of list A “Daniele” and
students_period_B will become
[“Dora”, “Minerva”, “Alexa”, “Obie”, “Alex”, “Briana”, “Cheri”, “Daniele”]
And that’s it.
We have looped over every single student in students_period_A and so the for loop will finish. The for loop doesn’t keep going in circles. The for loop iterates over every element of a list from the first to the last element.
students_period_A
had 4 students. The for loop will iterate over all four of them and then stop because it has gotten to the end of the list,
At this point after the loop has finished, if we use print statements, we will see
for student in students_period_A:
students_period_B.append(student)
print(student)
print(students_period_A)
print(students_period_B)
print(students_period_A)
Output: [“Alex”, “Briana”, “Cheri”, “Daniele”]
Unchanged list
print(students_period_B)
Output: [“Dora”, “Minerva”, “Alexa”, “Obie”,“Alex”, “Briana”, “Cheri”, “Daniele”]
Mutated list with four new elements