[Tutor] How is the return statement working in this function?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] How is the return statement working in this function?

Greg Christian
I am just wondering if anyone can explain how the return statement in this function is working (the code is from activestate.com)? Where does x come from – it is not initialized anywhere else and then just appears in the return statement. Any help would be appreciated.
 
 
def primes(n):
    """Prime number generator up to n - (generates a list)"""
    if n == 2: return [2]
    elif n < 2: return []
    s = range(3, n + 1, 2)
    mroot = n ** 0.5
    half = (n + 1)/2 - 1
    i = 0
    m = 3
    while m <= mroot:
        if s[i]:
            j = (m * m - 3)/2
            s[j] = 0
            while j < half:
                s[j] = 0
                j += m
        i = i + 1
        m = 2 * i + 3
    return [2]+[x for x in s if x]

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] How is the return statement working in this function?

Dave Angel-3
On 04/05/2012 08:39 PM, Greg Christian wrote:

> I am just wondering if anyone can explain how the return statement in this function is working (the code is from activestate.com)? Where does x come from – it is not initialized anywhere else and then just appears in the return statement. Any help would be appreciated.
>
>
> def primes(n):
>     """Prime number generator up to n - (generates a list)"""
>     ## {{{ http://code.activestate.com/recipes/366178/ (r5)
>     if n == 2: return [2]
>     elif n < 2: return []
>     s = range(3, n + 1, 2)
>     mroot = n ** 0.5
>     half = (n + 1)/2 - 1
>     i = 0
>     m = 3
>     while m <= mroot:
>         if s[i]:
>             j = (m * m - 3)/2
>             s[j] = 0
>             while j < half:
>                 s[j] = 0
>                 j += m
>         i = i + 1
>         m = 2 * i + 3
>     return [2]+[x for x in s if x]
>

The expression [x for x in s if x] is called a list comprehension, and
it defines x as it needs it.   The results of that expression is a list,
which is concatenated to the end of the list [2], and the combined list
is returned.

For example, try the one-liner:


    print    [i for i in xrange(5)]



--

DaveA

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] How is the return statement working in this function?

Steven D'Aprano-8
In reply to this post by Greg Christian
Greg Christian wrote:
> I am just wondering if anyone can explain how the return statement in this
> function is working (the code is from activestate.com)? Where does x come
> from – it is not initialized anywhere else and then just appears in the
> return statement. Any help would be appreciated.

> return [2]+[x for x in s if x]

It's called a list comprehension, and is like a one-liner specialist for loop.

[x for x in s if x] builds a list using x as the loop variable. So the above
line can be considered as the equivalent of this:

result = []
for x in s:
    if x:
        result.append(x)

return [2] + result



--
Steven
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] How is the return statement working in this function?

Brian van den Broek-2
In reply to this post by Greg Christian


On 6 Apr 2012 02:43, "Greg Christian" <[hidden email]> wrote:
>
> I am just wondering if anyone can explain how the return statement in this function is working (the code is from activestate.com)? Where does x come from – it is not initialized anywhere else and then just appears in the return statement. Any help would be appreciated.
>  
>  
> def primes(n):
>     """Prime number generator up to n - (generates a list)"""
>     ## {{{ http://code.activestate.com/recipes/366178/ (r5)
>     if n == 2: return [2]
>     elif n < 2: return []
>     s = range(3, n + 1, 2)

<snip>

>     return [2]+[x for x in s if x]

Hi Greg,

That it appears is a return isn't relevant. The bit '[x for x in s if x]' is a list comprehension. They build lists in an economical way. This one is equivalent to:

result = []
for x in s:
    if x:
        result.append(x)

Informally, you can think of the 'for x' as working kind of like "for every student" when a teacher reminds him or herself to praise students by repeating softly "for every student in my class praise that student".

HTH,

Brian vdB


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor