473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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?

http://www.python.org/doc/current/tu...00000000000000

Thanks.
Apr 9 '08 #1
7 1927
jmDesktop schrieb:
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?
print out what range(2, n) for n == 2 is.

And if you didn't know - 2 *IS* a prime.
Diez
Apr 9 '08 #2
On Apr 9, 4:58*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
jmDesktop schrieb:


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?

print out what range(2, n) for n == 2 is.

And if you didn't know - 2 *IS* a prime.

Diez- Hide quoted text -

- Show quoted text -
I do not understand.
Apr 9 '08 #3
jmDesktop wrote:
>>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?
>>range(2, 2)
[]
>>>
The loop body executes zero times.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Apr 9 '08 #4
jmDesktop schrieb:
On Apr 9, 4:58 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>jmDesktop schrieb:


>>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?
print out what range(2, n) for n == 2 is.

And if you didn't know - 2 *IS* a prime.

Diez- Hide quoted text -

- Show quoted text -

I do not understand.
for <variablein <sequenceloops over a sequence. And of course it
doens't if the sequence is empty, because you can't loop over something
that is empty, can't you?

and range(2,2) is the empty sequence.

Diez
Apr 9 '08 #5
|So what is n and x in the first iteration? Sorry. I'm trying.

When n == 2, the inner loop executes 0 times (the length of range(2,n)) and
then falls thru to the else clause, printing the correct answer.

Apr 9 '08 #6

"jmDesktop" <ne***********@gmail.comwrote in message
news:a6**********************************@e39g2000 hsf.googlegroups.com...
So what is n and x in the first iteration? Sorry. I'm trying.
Remember how Python's range operator works. range(n, x) constructs a list
that consists of all elements starting with n and up to, but /not
including/, x. For example, range(3, 7) constructs the list [3, 4, 5, 6].

So what happens if you try to construct the list range(2, 2)? In this case,
Python will construct an empty list -- this is its interpretation of "up to,
but not including n" for an argument like range(n, n). This is precisely
what is happening in the first iteration of the first enclosing for loop.
Trace through the code; see that 2 is bound to n in the first iteration; see
that the inner loop then tries to construct the list range(2, n), which is
range(2, 2), which is an empty list. And a "for x in []" statement will not
execute even once.

As it happens, your loop is perfectly correct. You are testing for divisors
for a number excluding 1 and the number itself. For the number 2, the
number of possible divisors satisfying this condition is an empty set. So 2
is, quite correctly, adjudged to be prime.
Jun 27 '08 #7
jmDesktop wrote:
[...]
So what is n and x in the first iteration? Sorry. I'm trying.
Somewhat feebly, if you don't mind my saying so, but don't worry.

The usual way to proceed in the face of such ignorance is to insert some
form of output that will tell you the answer to your question.

So:
>>for n in range(2, 20):
.... print range(2, n)
.... for x in range(2, n):
.... if n % x == 0:
.... print n, 'equals', x, '*', n/x
.... break
.... else:
.... print n, "is prime"
....
[]
2 is prime
[2]
3 is prime
[2, 3]
4 equals 2 * 2
[2, 3, 4]
5 is prime
[2, 3, 4, 5]
6 equals 2 * 3
[2, 3, 4, 5, 6]
7 is prime
[2, 3, 4, 5, 6, 7]
8 equals 2 * 4
[2, 3, 4, 5, 6, 7, 8]
9 equals 3 * 3

and so on! This is the value of the interactive interpreter.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
3
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello I am teaching myself python, and I have gotten a long way, it's quite a decent language and the syntax is great :) However I am having a...
15
by: Guyon Morée | last post by:
Hi all, I am working on a Huffman encoding exercise, but it is kinda slow. This is not a big problem, I do this to educate myself :) So I started profiling the code and the slowdown was...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
18
by: Marko.Cain.23 | last post by:
Hi, I create a dictionary like this myDict = {} and I add entry like this: myDict = 1 but how can I empty the whole dictionary? Thank you.
22
by: Cesar G. Miguel | last post by:
I've been studying python for 2 weeks now and got stucked in the following problem: for j in range(10): print j if(True): j=j+2 print 'interno',j What happens is that "j=j+2" inside IF...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.