Python ForLoop 语法 [英] Python ForLoop syntax

查看:42
本文介绍了Python ForLoop 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

for i in range(0, 3):
    print "hi"

这不起作用:

for range(0, 3):
    print "hi"

但我根本不需要'i'.有没有办法编写一个没有 'i' 的 'for' 语句或一个承担相同角色的不同字符?

but I don't need the 'i' for anything at all. Is there a way to write a 'for' statement without the 'i' or a different character which assumes the same role?

(通常,它会像

for i in range(0, someReturnValue())
    someFunction()

但是这个问题可以推广到我的第一个例子.)

but the question is generalizable to my first examples.)

推荐答案

如果您不需要 lopping 变量(索引),最好的做法是使用 _(实际上,只是另一个变量):

If you don't need a lopping variable(index), the best practice is to use _ (which is, really, just another variable):

for _ in range(0, 3):
    print "hi"

另见:

这篇关于Python ForLoop 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆