Why I can't re-assign a variable in a for loop in python? [duplicate]
问题 This question already has answers here : python assign values to list elements in loop (4 answers) Closed 5 years ago . Today I find an interesting fact, that I can't re-assign variable in a 'for loop', but why? Here is my code: >>> my_list = [1,2,3,4,5] >>> for i in my_list: >>> if i > 3: >>> i = 'my_value' >>> my_list [1, 2, 3, 4, 5] 回答1: When you loop over a list with for i in my_list , i isn't bound to a list cell on each iteration. It's bound to the object the list cell referred to.