for or while loop to do something n times

前端 未结 3 1005
失恋的感觉
失恋的感觉 2020-12-16 09:12

In Python you have two fine ways to repeat some action more than once. One of them is while loop and the other - for loop. So let\'s have a look on

3条回答
  •  佛祖请我去吃肉
    2020-12-16 09:53

    This is lighter weight than xrange (and the while loop) since it doesn't even need to create the int objects. It also works equally well in Python2 and Python3

    from itertools import repeat
    for i in repeat(None, 10):
        do_sth()
    

提交回复
热议问题