Python 'for' loop is memory inefficient

Benjamin Kaplan benjamin.kaplan at
Sun Aug 16 17:41:21 CEST 2009

On Sun, Aug 16, 2009 at 2:30 AM, Emmanuel Surleau
<emmanuel.surleau at> wrote:
> I don't see what's particularly un-Pythonic with this code. Not using xrange()
> is a mistake, certainly, but it remains clear, easily understandable code
> which correctly demonstrates the naive algorithm for detecting whether n is a
> prime. It doesn't call for condescension

It's not that the code is bad, but too many people coming from Java
and C keep thinking of for loops like they're using Java or C and
therefore that "for i in range(a,b)" is identical to "for(int i = a; i
< b; i++)". It's not and, for the most part, you shouldn't code like
that. Since you're using numbers, range/xrange is the appropriate
idiom but you still have to remember that a for loop in python doesn't
loop until a condition is met, it loops through an iterator until the
interator says it's done.

More information about the Python-list mailing list