basic python question about for loop

Reedick, Andrew jr9445 at ATT.COM
Wed Apr 9 16:59:55 EDT 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 4:51 PM
> To: python-list at python.org
> Subject: basic python question about for loop
> 
> >From the Python.org tutorial:
> 
> >>> 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'
> ...
> 2 is a prime number
> 3 is a prime number
> 4 equals 2 * 2
> 5 is a prime number
> 6 equals 2 * 3
> 7 is a prime number
> 8 equals 2 * 4
> 9 equals 3 * 3
> 
> 
> 
> first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
> Why did it fall through?
> 

a) 2 is prime, so nothing is wrong.

b) Range isn't doing what you think it's doing:

>>> print range(2,2)
[]
>>> print range(2,3)
[2]
>>> print range(2,4)
[2, 3]
>>> print range(2,5)
[2, 3, 4]

>>> print range(1,1)
[]
>>> print range(1,2)
[1]
>>> print range(1,3)
[1, 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





More information about the Python-list mailing list