[Tutor] for: how to skip items

Neil Cerutti neilc at norwich.edu
Tue Feb 18 15:53:02 CET 2014


On 2014-02-17, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
> On 17 February 2014 16:17, Gabriele Brambilla
><gb.gabrielebrambilla at gmail.com> wrote:
>> Doesn't exist a way in Python to do like in C
>>
>> for i=0, i<100, i=i+10
>>
>> ? without creating a list of index?
>
> You haven't said which Python version you're using. In Python 2
> the range function returns a list but the xrange function
> returns an iterator. In Python 3 the range function returns an
> iterator.

In Python 3 it range returns a range object, which is a sequence
type.

>>> x = range(100)
>>> x
range(0, 100)

You can use slice notation on range objects.

>>> x[::10]
range(0, 100, 10)

So we've got *that* going for us.

-- 
Neil Cerutti



More information about the Tutor mailing list