Use range() or xrange() to Track Loop Index
In the last lesson, you saw the sort of loop you might expect a developer coming from a C-style language to write in Python. In this lesson, you’ll see how you can use the
xrange() built-ins to make your loops more Pythonic.
The original loop keeps track of the loop index manually, which isn’t very Pythonic. You can instead keep track automatically if you use
range() in Python 3 and
xrange() in Python 2. A
range object is a generator in Python.
Become a Member to join the conversation.