[Tutor] Leap years
ahimsa
ahimsa@onetel.net.uk
Tue Jan 14 17:59:02 2003
Hello Tim :)
On Tue, 2003-01-14 at 22:21, Tim Wilson wrote:
<snip>
> The first version is certainly more explicit. If you haven't run across it
> yet, the % operator in Python is called "mod" and is the same as the
> remainder concept you probably recall from elementary school math class. In
> this case, any number that has 0 remainder when divided by 2 is event and
> anything else is odd.
>
'mod' as in 'modulus' - where it only yields the remainder or balance of
the division rather than the number of times it is divisible?
> For a given value of "num," the statement
>
> num % 2 == 0:
>
> is either true or false. By using the construct
>
> return num % 2 == 0
>
> I'm simply evaluating the "trueness" :-) of the num % 2 == 0 expression and
> returning that straight away. As always, this idiom is optional, but quite
> elegant I think.
I agree - I reckon that that *is* a really elegant way of doing this: it
appeals to my appreciation of minimalism. Would you mind explaining - or
referring me to an explanation of - the mechanics of this process where
you are doing away with the the if-else test. I note that 'return' is a
keyword/reserved word in Python, but I haven't found out what it does
precisely. I am surmising that it 'returns' the value of an evaluation,
but from the example you give above, it appears as if somehow it is also
encapsulates the truth/falsity of an evaluation (does that make sense?)
- i.e. 'squashes' or 'compresses' the 'if-else' test into a single
'function' (I'm not sure of the correct terminology here, sorry). If you
could, I would appreciate it if you could unpack how 'return' works and
also the data types or evaluation types it works with.
Nevertheless, I do agree that it is an elegant substitution, and
functionally/operationally seems exactly the same as the 'if-else' test.
Thanks for the illustration.
Andrew
--
ahimsa <ahimsa@onetel.net.uk>