# basic python question about for loop

Reedick, Andrew jr9445 at ATT.COM
Wed Apr 9 23:23:30 CEST 2008

```
> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of jmDesktop
> Sent: Wednesday, April 09, 2008 5:04 PM
> To: python-list at python.org
> Subject: Re: basic python question about for loop
>
> >
> > > >>> for n in range(2, 10):
> > > ...     for x in range(2, n):
> > > ...         if n % x == 0:
> > > ...             print n, 'equals', x, '*', n/x
> > > ...             break
> > > ...     else:
> > > ...         # loop fell through without finding a factor
> > > ...         print n, 'is a prime number'
> > > ...
> >
> > > first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
> > > Why did it fall through?
> >

>
> So what is n and x in the first iteration?  Sorry.  I'm trying.

You're never getting to n and x in the first iteration, because the 'for x in range(2, n)' loop isn't looping.

This:
for x in range(2, n)
is equivalent in C/Perl/etc. to:
for(x=2; x<n; x++)
which for the first iteration is:
for(x=2; x<2; x++)

Since (2 < 2) is false, you never call 'if n %x == 0:' in the first iteration.

Or to put it another way:
Range(2, n) starts at 2, and stops _before_ n.
Range(2, n) starts at 2, and stops _before_ 2.

*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622

```