{article Examples__Python 3.4 }{title} {text}{/article}

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.

class range(stop)

class range(start, stop[, step])

The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method). If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. If step is zero, ValueError is raised.

For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.

For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.

A range object will be empty if r[0] does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices.

Ranges containing absolute values larger than sys.maxsize are permitted but some features (such as len()) may raise OverflowError.

class range(stop)

class range(start, stop[, step])


class range(stop)

>>> for i in range(5):
print(i)


0
1
2
3
4


class range(start, stop)

>>> range(5, 10)
[5, 6, 7, 8, 9]


class range(start, stop[, step])

>>> range(0, 10, 3)
[0, 3, 6, 9]



>>> range(-10, -100, -30)
[-10, -40, -70]



>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
print(i, a[i])

(0, 'Mary')
(1, 'had')
(2, 'a')
(3, 'little')
(4, 'lamb')



>>> print(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]



>>> list(range(5))
[0, 1, 2, 3, 4]



>>>

{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
class range(stop)

>>> for i in range(5):
    print(i)


0
1
2
3
4
class range(start, stop)

>>> range(5, 10)
[5, 6, 7, 8, 9]
class range(start, stop[, step])

>>> range(0, 10, 3)
[0, 3, 6, 9]


>>> range(-10, -100, -30)
[-10, -40, -70]


>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
        print(i, a[i])

    (0, 'Mary')
(1, 'had')
(2, 'a')
(3, 'little')
(4, 'lamb')


>>> print(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


>>> list(range(5))
[0, 1, 2, 3, 4]

</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}