[Tutor] tutorial 5.1.3

Lloyd Kvam pythontutor@venix.com
Thu May 22 15:14:02 2003


filter returns the list element if f returns true.  filter doesn't return f.

So the elements from range(2,25) are what get returned by filter.
If f(element) returns 0, then the element is omitted from the result list.

David Scott wrote:
> In the Python Tutorial (the one that comes with Python), in section 
> 5.1.3, there is something that I do not understand:
> 
>>>> def f(x): return x % 2 != 0 and x % 3 != 0
>>>
> ...
> 
>>>> filter(f, range(2, 25))
>>>
> [5, 7, 11, 13, 17, 19, 23]
> 
> That example is used to compute primes.
> 
>> From what I understand, the "return" statement is returning the tested 
>> value 
> 
> (passed in from the range) if it passes both tests. However, it seems to 
> me that the return statement would return either a 1 or a 0 and nothing 
> else, because of the "and".
> 
> return (test expression) and (test expression). If both are true, then 
> the "and" is true. else, the entire statement is false.
> 
> What am I missing or misunderstanding here?
> 
> 
> 
> --------------------------------------
> dedoleo@hotmail.com
> http://groups.msn.com/WalledCity
> 
> _________________________________________________________________
> Help STOP SPAM with the new MSN 8 and get 2 months FREE*  
> http://join.msn.com/?page=features/junkmail
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582