[Tutor] checking for a condition across a list

Tim Peters tutor@python.org
Mon, 14 May 2001 02:25:27 -0400


[Pijus Virketis]
> Thanks so much! Just one small note: the way you've written the
> function, the usage should be:
> >>> alltrue([1,1,1,1], isone) <-- inputs the other way around...

Wish I could say I was testing you, but, na, I screwed up -- cut 'n paste
error.  Good eye!

> Oh, and one more thing. I've never before encountered the
> "return x ==1" statement before. What exactly does it do?

Hmm.

    return expression

returns the value of expression from a function, where "expression" is any
expression.  "x == y" is a specific expression that yields 1 if x == y, else
0.  All the comparison operators work that way:

>>> 2 == 3
0
>>> 2 == 2
1
>>> 2 < 3
1
>>> 2 > 3
0
>>> 2 <= 3
1
>>> 2 >= 3
0
>>> 2 is 3
0
>>> 2 is not 3
1
>>> 'a' in 'abc'
1
>>> 'a' not in 'abc'
0
>>>

So

    return x == 1

returns 1 as the value of the function if x equals 1, and returns 0 as the
value of the function if x does not equal 1.  Clear?  Clear!