## A community for students. Sign up today

Here's the question you clicked on:

## frankdpnw 3 years ago First time user of this group. I have a quick question about the python prime number program listed below. In the first loop of ... for x in range (2,n): .... is n = 2 in this case? If yes, why does the program correctly say it is a prime number? I can follow the rest of the logic but and struggling with the first iteration Thanks for n in range (2,15) : for x in range(2, n): if n % x == 0: print n, 'equals', x, '*', n/x break else: print n, 'is a prime number'

• This Question is Closed
1. Spitfire96

Yes that's exactly what it does, it starts at 2 and increases by one every iteration. Basically the start value is always included and the stop value is always excluded in the 'range()' function ( and in many other functions too ) . And if I may make a suggestion, to make the program run faster ( in this case it wouldn't make a difference but it's something to think about for every program you write ) you should make the nested loop only iterate to half of 'n' because anything above that would be equal to a smaller number. Ask if your not sure how to write that. And also you can add user interaction with the program. Maybe you should allow the user to pick the value he/she wishes to iterate from and to using the 'raw_input'.

2. frankdpnw

Thanks .. I am just trying to understand the logic of what was written and I know that it can be improved. Before doing so, I want to make sure I understand the existing logic. However, I am still confused. If n=2, and x=2 then n % x == 0 is true and it should fall thru to the 'print n ='s line and not go to the else statement. I am definitely not grasping this one.

3. Spitfire96

well no it wouldn't because the 'range()' function would exclude the stop value 2, so basically what would happen is that your iteration is starting at 2 and going back 1. Since this can't happen ( if it can someone please correct me ) the inner loop is never entered and it automatically goes to the else statement. That's something I don't understand, that else statement seems to be a branch of the loop ...does someone know whats happening there?

4. shandelman

There actually is such a thing as a for else loop...if the loop terminates normally without a break, it will do the else clause. If it does break, it will skip the else clause. Spitfire is right: since the range is basically from 2 to 1 when n = 2 that first time, it never even enters the inner for loop and jumps right to the else clause.

5. frankdpnw

Thank you both ... After single stepping through the code multiple times and trying a number of other tests, it can only work the way you describe it and it confirms what I thought but was not sure enough about it.

6. shandelman

If you're learning your first programming language, know that for-else loops are, well, kinda weird. They don't appear in a lot of programming languages, and I don't know that I've ever used one. For every for-else loop, there's a way one can write it without using a for-else loop.

7. Spitfire96

Ah that explains why I didn't know about it, the only other language I know is Java and I don't think it has a for-else loop. And you're welcome @frankdpwn I'm glad I can help.

8. bwCA

http://docs.python.org/reference/compound_stmts.html#the-for-statement http://docs.python.org/library/functions.html#range the docs installed on your computer are easier to use than the online docs (F1 from IDLE - windows), the index tab is very useful. The tutorial in the docs is a must - http://docs.python.org/tutorial/index.html

#### Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy