[Tutor] tutorial 5.1.3

Alan Gauld alan.gauld@blueyonder.co.uk
Fri May 23 15:43:15 2003


> >>>def f(x): return x % 2 != 0 and x % 3 != 0
> ...
> >>>filter(f, range(2, 25))
> [5, 7, 11, 13, 17, 19, 23]
> 
> From what I understand, the "return" statement is returning 
> the tested value (passed in from the range) if it passes both 
> tests. 

That is correct. It returns the value from the range() if 
both conditions are true.

> However, it seems to me that the return statement would 
> return either a 1 or a 0 and nothing else

As Gerrit points out Python actually returns either the value 
of the first expression if false or the value of the second 
if true. But it doesn't matter because both get passed back 
to filter which uses them to decide wjether to add the range 
value parameter to the output list.

> What am I missing or misunderstanding here?

You are getting the return value from the function confused 
with the return value from filter. They are not the same.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld