[Tutor] Making a Primary Number List generator

Dave Angel davea at davea.name
Sun May 12 13:43:52 CEST 2013


On 05/11/2013 09:58 PM, Daniel Magruder wrote:

Please respond to the list, not the individual.  Otherwise you're 
robbing yourself and others of the possibility of learning from and 
helping multiple people.  I don't mind if you ALSO reply to me (which is 
what reply-all does by default), but many people do object.  In 
Thunderbird, you normally just do Reply-list, and it does the right 
thing.  But with other email programs, you might do reply-all, and 
remove whichever recipients you don't need.

> Dear Dave,
> I can't tell you how much I appreciate your assistance, however as a novice I doubt my ability to crawl. I sincerely envy your depth of understanding about this and in trying to learning would really benefit if you could please expound on a few notions.
> Why must I top-level call? What is that actually? Several of the sources I have seen like learnpython.org suggest that def function(): is sufficient...

A function is defined by a def, but a program that never calls any of 
them will do nothing at all (except look for syntax errors and the 
like).  There are probably tens of thousands of defined functions in the 
library, but running python will only execute the ones you call.

Typically at the bottom of every script there needs to be at least one 
function call that gets things started.  (Unless the script is so 
trivial that it's written without using any functions).  But in either 
case, some top-level code is needed.

That top-level code can be as simple as a single call, or it might 
define a few variables, and then make the call(s).

> I thought I had to define prime as f(0) so that it would be a float, not an integer division. Am I wrong? I am so utterly confused.

I'm afraid you're wrong in a couple of points.  There's no such function 
as f() in the standard library.  If you wanted to define a literal 
float, you could either use 0.0, or use float(0).  But you do not want a 
float there, everything can be done with ints.

> In many sample codes I have seen num or int seem to be short hand for anything that is a number, because I though I was dividing and would receive a float remainder for non-prime numbers, I wan't to use that as a test.

int and float are types (and act like functions), and if you needed to 
check the type of an object, you'd use  isinstance(myvar, int) or 
isinstance(myvar, float).  You can't just do a comparison with int.  But 
dividing two ints will give an int in Python 2.x and will give a float 
in Python 3.x.   And that's regardless of whether it comes out even or 
not.  Which version of Python are you using?

Fortunately, there's a much better way of telling if an int can be 
divided by another int:  The modulo operator, represented by a "%" symbol.

     prime%n   will give the remainder, and if it's zero, you know that 
prime is evenly divisible by n  (and therefore isn't prime).


> For your suggestion:
>> def testprime(candidate)
>>     check all ints between 2 and candidate, and return true if any of them divide the candidate evenly
> I don't mean to be so dependent, but how would I do this? Also what is the difference between return and print?
>>     Otherwise return false
>>

The return statement is the way a function returns a value to its 
caller.  Let's suppose you wanted a function that returned true if its 
int argument was odd.  You could do it like this:

def isodd(candidate):
     if candidate%2 ==0:
         return False
     else:
         return True

Now, the body of that function could be done in one line, but I wanted 
to illustrate the returning of useful values, and also the possibility 
that the return might not be at the end, and that there might be  more 
than one place the function returns.

Why not get the other function to call this one temporarily, and see if 
you can make the program find the first 1000 odd numbers instead.  Then 
you can come back to the testprime() function and see if you can create 
one that returns True for primes, and False for anything else.  Clearly 
that function will need a loop.

>> def counting_primes()
>>     build a list of primes, where a number is put in the list if testprime() confirms that it is prime.
> Again, how would I do this, so if I had something that told me a number was prime, how would I take that prime and put it in a list if the function is on loop?
>>     return the list once it's big enough

You're already doing that in the code you showed.  You initialized the 
primelist, then started a while loop.  The difference I'm suggesting is 
that instead of dealing with x in this function, you defer that to the 
testprime() function.  So your loop would call testprime() and 
conditionally append the value prime to the primelist.

def counting_primes():
     primelist=[]
     prime = 2
     while len(primelist) < 1000:
         if isodd(prime):
              primelist.append(prime)
         prime += 1
     return primelist

> Despite going to many sources I can not seem to find a single source that adequately explains Python 100% without any technical jargon or assumptions of prior knowledge. What I mean by this, is that I now have the gist of the utmost basics, but I would say my understanding is befuddled. Furthermore if I were to attempt to write code (such as this exercise) I am completely lost. What do you recommend for a good source to learn Python? I have tried MIT open course ware, but again even their intro explains the obvious, but not how to use it collectively.
> Sincerely,
> Dan
>
>

With no experience in programming other languages, you'd need a 
different kind of tutorial than I sought when I was learning Python. 
And you absolutely need to match your tutorial against the version of 
Python you're running on your machine.  2.x and 3.x aren't VERY 
different, but there were enough changes that you don't want to be 
dealing with that while learning initially.

Anybody else want to recommend a tutorial for someone who has no 
programming experience in other languages?

--
DaveA



More information about the Tutor mailing list