Re: [Tutor] Prime Numbers

2013-12-16 Thread Steven D'Aprano
On Mon, Dec 16, 2013 at 09:49:23AM +0100, Rafael Knuth wrote: > That's actually a good example. Let me explain what I feel confused about. > Print actually runs through the entire loop. No it doesn't. Print has nothing to do with loops. It just happens that the body of the loop -- the thing that

Re: [Tutor] Prime Numbers

2013-12-16 Thread spir
On 12/16/2013 09:49 AM, Rafael Knuth wrote: Hej there, number = 9 for element in range(2,9): 3 % 2 != 0: My assumption is that the program should end the loop after the first iteration again and it then should return True. No. If it did that, it wouldn't be a *loop* at all, would it? The whol

Re: [Tutor] Prime Numbers

2013-12-16 Thread spir
On 12/16/2013 11:34 AM, spir wrote: On 12/15/2013 05:54 PM, Rafael Knuth wrote: PS: using "print" as Mark proposes is indeed you best friend to unsertand loops (and also recursion). Denis ___ Tutor maillist - Tutor@python.org To unsubscribe or ch

Re: [Tutor] Prime Numbers

2013-12-16 Thread spir
On 12/15/2013 05:54 PM, Rafael Knuth wrote: Hej, I stumbled upon this program here (Python 3.3.0) and I don't quite understand how the for loop plays with the return True statement: def is_prime(number): for element in range(2, number): if number % element == 0: retur

Re: [Tutor] Prime Numbers

2013-12-16 Thread Andreas Perstinger
On 16.12.2013 09:49, Rafael Knuth wrote: That's the tiny little detail I am confused about: What does return exactly do? Does it return only the first value within a loop or does it iterate through all values within a loop? (unless a given condition is met) The return statement has nothing to d

Re: [Tutor] Prime Numbers

2013-12-16 Thread Rafael Knuth
Hej there, >> number = 9 >> for element in range(2,9): >> 3 % 2 != 0: >> My assumption is that the program should end the loop after the first >> iteration again and it then should return True. > > No. If it did that, it wouldn't be a *loop* at all, would it? The whole > reason loops (for and whil

Re: [Tutor] Prime Numbers

2013-12-15 Thread Alan Gauld
On 15/12/13 16:54, Rafael Knuth wrote: I stumbled upon this program here (Python 3.3.0) and I don't quite understand how the for loop plays with the return True statement: It doesn't. Remember that indentation is all important in Python. The return true statement is outside the loop so only g

Re: [Tutor] Prime Numbers

2013-12-15 Thread Mark Lawrence
On 15/12/2013 16:54, Rafael Knuth wrote: Hej, I stumbled upon this program here (Python 3.3.0) and I don't quite understand how the for loop plays with the return True statement: def is_prime(number): for element in range(2, number): if number % element == 0: return F

Re: [Tutor] Prime Numbers

2013-12-15 Thread Steven D'Aprano
On Sun, Dec 15, 2013 at 05:54:10PM +0100, Rafael Knuth wrote: > Hej, > > I stumbled upon this program here (Python 3.3.0) and I don't quite > understand how the for loop plays with the return True statement: > > def is_prime(number): > for element in range(2, number): > if number % el

Re: [Tutor] Prime Numbers

2013-12-15 Thread Joel Goldstick
On Sun, Dec 15, 2013 at 11:54 AM, Rafael Knuth wrote: > Hej, > > I stumbled upon this program here (Python 3.3.0) and I don't quite > understand how the for loop plays with the return True statement: > > def is_prime(number): > for element in range(2, number): > if number % element ==

Re: [Tutor] Prime Numbers

2010-10-07 Thread Steven D'Aprano
On Thu, 7 Oct 2010 12:41:07 pm Ryan Bridges wrote: > Hi, > I'm using Python 2.6.2 and I need to write a code so that when I use > an input, it will say if the number is prime or not. How do I do > this? Using the keyboard is probably the best way, although at a pinch you could copy and paste ind

Re: [Tutor] Prime Numbers

2010-10-07 Thread Shantanoo
On Thu, Oct 7, 2010 at 07:11, Ryan Bridges wrote: > Hi, > I'm using Python 2.6.2 and I need to write a code so that when I use an > input, it will say if the number is prime or not. How do I do this? > > Following links would be useful: http://en.wikipedia.org/wiki/Prime_number http://en.wikiped

Re: [Tutor] Prime numbers

2010-03-28 Thread Lie Ryan
On 03/28/2010 09:57 PM, yd wrote: > It's not homework i just want to be able to convert my algorithm into > good code, and the only way to do that is by actually writing it. I'm > just writing it to learn how it's done. In most cases, when: 1) the code is effective (i.e. it always gives correct an

Re: [Tutor] Prime numbers

2010-03-28 Thread yd
> > > > What 'problem' are you trying to solve? > In general, anytime you can use a premade solution, you are at an > advantage, > not cheating. > That's one of the marks of a truly good programmer - being able to reuse as > much code as possible. > Unless it's a homework problem and he said "don't

Re: [Tutor] Prime numbers

2010-03-28 Thread Luke Paireepinart
On Sat, Mar 27, 2010 at 5:08 PM, yd wrote: > > Having a problem finding the first 1000 prime numbers, here is my code:- > > print(2) > n =3 > counter =1 > while counter <=1000: > for x in range(3,int((n**0.5)),2): > if n%x != 0: > print(n) > n+=1 > counter+=1 > else: >

Re: [Tutor] Prime numbers

2010-03-28 Thread Rafik Ouerchefani
On Sat, Mar 27, 2010 at 11:08 PM, yd wrote: > > Having a problem finding the first 1000 prime numbers, here is my code:- > > print(2) > n =3 > counter =1 > while counter <=1000: >   for x in range(3,int((n**0.5)),2): >     if n%x != 0: >   print(n) >   n+=1 >   counter+=1 >     else: >

Re: [Tutor] Prime numbers

2010-03-27 Thread Shashwat Anand
>>> [x for x in range(3,int((n**0.5)),2)] [] your while loop is an infinite loop. Had you read the range documentations ? >>> range(3,int((n**0.5)),2) [] >>> n**0.5 1.7320508075688772 >>> n = 3 >>> n ** 0.5 1.7320508075688772 >>> int ( n ** 0.5) 1 >>> range ( 3, 1, 2) [] On Sun, Mar 28, 2010 at