带索引遍历列表

列表和集合 / 列表

numbers = [2, 3, 5, 7, 11, 13, 17]
string = ""
# enumerate gives the index and the element
for i, number in enumerate(numbers):
    if i > 0:
        string += "; "
    string += str(number)
    # The index lets you change the element in place
    numbers[i] = number * 10

# string is '2; 3; 5; 7; 11; 13; 17'
print(f"{string = }")
# numbers is [20, 30, 50, 70, 110, 130, 170]
print(f"{numbers = }")