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 range()
and 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.
jayantsing on Dec. 13, 2023
Hello, Is it possible to see an example of pre creation of list using range() in Python 2. It will be helpful to understand the difference from scratch.
Appreciate the explanation.