[Tutor] Making a Primary Number List generator

Alan Gauld alan.gauld at btinternet.com
Tue May 14 01:55:55 CEST 2013


On 14/05/13 00:01, Daniel Magruder wrote:

> I am still confused as what return does.
 > What does it mean if a function returns True to the caller?
 > What is the caller?

The caller is the program code that calls the function.
For example if I write a function

def square(x):
     return x*x

and write another function to display the first 5 squares:

def square5():
     for n in range(1:6):
         result = square(n)
         print result

square5() is the caller of square() and the return value (x*x)
is stored in result.

return always terminates a function and the value of
the expression following return is the value of the function.

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

> The meaning of this I am still confused about.
 > What does it mean if the value is returned as false
> if it has 0 remainder,
> what does it mean if it is returned as true?

The function is answering the question "is candidate odd?"
It returns True if candidate is dd and False if candidate
is even. It determines whether candidate is even by dividing candidate 
by 2 and checking to see if there is a zero remainder. If the remainder 
is zero then candidate is even and isodd returns(evaluates to) False.

We can then use it in an if statement like

x = int(raw_input('number: '))
if isodd(x):
    print x,'is odd'
else:
    print x, 'is even'

> Is this like a dictionary with keys and values?

No, it's just the value of the function.

> def isprime(x):
>      test=2
>      numberinquestion = x
>      while test < numberinquestion:
>          if numberinquestion % test == 0:
>              test += 1
>              return False

Down to here is fine. All you need now is to
return True if you get to the end of the while loop.
(actually you probably don't want/need to increment
test here either but that's a nicety)

>          elif numberinquestion / test == int:

But this doesn't work because int is a type not a value.
I'm not quite sure what you think it might be doing but in practice it 
won't work because you are comparing a numeric value to a type.
(type is an object in Python) You could do it like:

          elif type(numberinquestion / test) == int:

But for various reasons isinstance() is the preferred method.

>              test += 1
>              return False


>      if test == numberinquestion:
>          return True

You don't really need the if test here since you only exit the while 
loop if test == numberinquestion

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

That seems OK but it would be simpler with a for loop:

def counting_primes():
    primelist = []
    for prime in range(2,1000):
       if isprime(prime):
          primelist.append(prime)
    return primelist

And even shorter with a list comprehension

def counting_primes():
     return [n for n in range(2,1000) if isprime(n)]

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list