[Tutor] tutorial 5.1.3

Gerrit Holl gerrit@nl.linux.org
Thu May 22 15:17:12 2003


Hi David,

David Scott schreef op donderdag 22 mei om 20:51:42 +0000:
> >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?

The 'and' operator in Python has the feature that is returns the true
operand if either is true. This can be useful in some occasions: for
example:

m = "%d apple%s" % (n, (n!=1 and 's' or ''))

This is documented in the reference manual:

"""The expression x and y first evaluates x; if x is false, its value is
returned; otherwise, y is evaluated and the resulting value is returned.  """

Source: http://www.python.org/dev/doc/devel/ref/Booleans.html

yours,
Gerrit.

-- 
157. If any one be guilty of incest with his mother after his father,
both shall be burned. 
        -- Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/