Just the easy one which I could not find out why it works like that:
cities = ['Madrid', 'Barcelona', 'Valencia', 'Murcia']
for city in range(len(cities)):
if cities[city] == 'Madrid':
cities[city] = 1
elif cities[city] == 'Barcelona':
cities[city] = 2
elif cities[city] == 'Valencia':
cities[city] = 3
else:
cities[city] = 4
print(cities)
and this one gives [1, 2, 3, 4]
as it should.
But if I do this:
cities = ['Madrid', 'Barcelona', 'Valencia', 'Murcia']
for city in cities:
if city == 'Madrid':
print('hey')
city = 'Kiev'
print(cities)
Result is the following:
hey
['Madrid', 'Barcelona', 'Valencia', 'Murcia']
So, it recognizes 'Madrid'
, therefore prints hey
, but then does not assign 'Kiev'
to it. However in the first example, using index range, the reassignment is completed perfectly.