0
Can someone please help me understand using outside variables in for loops
fruits = ["apples","cherries"] for fruits[-1] in fruits: print(fruits[-1], end ="|") Why is the output apples | apples |? Doesn’t fruits[-1] mean it will give an output of cherries | cherries |
1 Odpowiedź
+ 2
on first iteration, apples is assigned to fruits[-1] .
List looks like this now : ["apples", "apples"]
that is why fruits[-1] prints apples.
________________________________
on second iteration , apples is assigned to fruits[-1], and again fruits[-1] prints apples .