我了解“for”和“range”在 for 回圈中的作用,但無法得到“i”在它們中的作用。你可以解釋嗎?
uj5u.com熱心網友回復:
i是一個在 for 回圈中使用的可迭代變數(在本例中為 range(x))
i可以用任何其他字符或單詞更改,我建議您將i其用作標準練習或與您正在回圈的內容相關的內容。
range(x)也可以更改為任何可迭代的。例如:
names = ['janet', ' bob', 'charlie']
for name in names:
print(name)
# Output: janet bob charlie
更多 for 回圈示例:
grid = [[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]
for row in grid:
print(row)
# Outputs:
# [0, 1, 2]
# [3, 4, 5]
# [6, 7, 8]
for row in range(3):
for col in range(3):
print(grid[col][row])
# Outputs: 0 3 6 1 4 7 2 5 8
# Prints a single digit at a time going from the first of each array to the last
# Instead of printing out an array at a time
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/488468.html
上一篇:隨時間步數可變的屬性變化
