[Tutor] for loop for long numbers

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Aug 3 23:02:40 CEST 2015

On 03/08/2015 19:15, Dima Kulik wrote:
>   Hi to all.
> Can you help me plz.
> I want to make a for loop with a huge numbers.
> for example:
> for i in range (0,9000000000):
>   make_some_code
> but range and xrange cant operate with such big numbers.
> Can some on help me?
> Thanks.

You cannot do anything about it directly with Python 2 but this works 
fine on Python 3.  Work arounds from 
http://stackoverflow.com/questions/9816603/range-is-too-large-python are:-

import itertools
range = lambda stop: iter(itertools.count().next, stop)


def range(stop):
    i = 0
    while i < stop:
        yield i
        i += 1

Having said that what are you trying to achieve?  If you tell us we may 
be able to suggest a cleaner solution.

My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

More information about the Tutor mailing list