Skip to content Skip to sidebar Skip to footer

Python Forloop Syntax

This works: for i in range(0, 3): print 'hi' This doesn't work: for range(0, 3): print 'hi' but I don't need the 'i' for anything at all. Is there a way to write a 'for'

Solution 1:

As mentioned elsewhere, this is an interesting and possibly faster alternative:

import itertools

for _ in itertools.repeat(None, 3):
    print'hi'

Solution 2:

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"

Also see:

Post a Comment for "Python Forloop Syntax"