试试这个。希望对你有帮助
languages = ["C", "C++", "Perl", "Python"]
for k in range(0,len(languages)):
print(languages[k-2])
输出是:
Perl
Python
C
C++
在 python 中,for 循环函数是 (for k in x(start,stop,step))。这里 x 是您的列表或字符串变量,并且
start : Indicates where the for loop should start and the index starts from zero.
stop : Indicates where the for loop ends. It takes up to (n-1) elements.
例如:
for k in range(0,100):
它提供从零到 99 的输出,如果你想要输出 100。你应该这样提及:
n = 100
for k in range(n+1):
print k
输出从 0 到 100。在这种情况下,for 循环将从零获取索引(开始)。
step : Indicates how many steps should do in for loop. By default step is one in for loop.
例如:
for k in range(0,10,1):
print k
#output is 0,1,2,3,4,5,6,7,8,9
for k in range(0,10,2):
print k
#output is 0,2,4,6,8
for k in range(0,10,3):
print k
#output is 0,3,6,9