On Sun, Apr 19, 2009 at 2:48 PM, Gregor Lingl <span dir="ltr"><<a href="mailto:gregor.lingl@aon.at">gregor.lingl@aon.at</a>></span> wrote:<br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">
>How do you explain the nature of range to beginners? <br></blockquote>
<br>How about using list(range())? Something like:<br><br>>>> # Here's how you can create a list of integers:<br>>>> list(range(10))<br>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<br>>>> list(range(1, 10))<br>
[1, 2, 3, 4, 5, 6, 7, 8, 9]<br>>>> list(range(1, 10, 2))<br>[1, 3, 5, 7, 9]<br>>>> list(range(-10, 10, 2))<br>[-10, -8, -6, -4, -2, 0, 2, 4, 6, 8]<br><br>>>> # 'list' creates a list, and 'range' specifies its starting point, ending point, and interval between points.<br>
>>> # a range object produces these values when called upon to do so.<br>>>> # for example, in a loop:<br>>>> for x in range(10): (x, x**2)<br><br>(0, 0)<br>(1, 1)<br>(2, 4)<br>(3, 9)<br>(4, 16)<br>
(5, 25)<br>(6, 36)<br>(7, 49)<br>(8, 64)<br>(9, 81)<br><br>Now, interesting, here I've stumbled on a question that I need some clarification on:<br><br>>>> a = range(10)<br>>>> type(a)<br><class 'range'><br>
>>> next(a)<br>Traceback (most recent call last):<br> File "<pyshell#49>", line 1, in <module><br> next(a)<br>TypeError: range object is not an iterator<br><br>>>> help(range)<br>
Help on class range in module builtins:<br><br>class range(object)<br> | range([start,] stop[, step]) -> range object<br> | <br> | Returns an iterator that generates the numbers in the range on demand.<br><br>So is range an iterator?<br>
<br>- Michel<br><br>