[Tutor] List comprehension question
Alan Gauld
alan.gauld at btinternet.com
Mon Nov 8 17:28:07 CET 2010
"Steven D'Aprano" <steve at pearwood.info> wrote
> I'm going to be pedantic here... but to quote the Middleman,
> specificity is the soul of all good communication.
Be pedantic! :-)
I really liked the explanation although I already sort of knew most of
it.
But there were a few nuggets in there I'd missed, like range() being
a type all of its own.
But one, slightly off-topic, question:
> def proper_divisors(n):
> return sum(x for x in range(1, int(math.sqrt(n))) if n%x == 0)
>
Why use math.sqrt() instead of just using the ** operator?
return sum(x for x in range(1, int(n**0.5)) if n%x == 0)
I'd have expected ** to be significantly faster than calling the
function, and given this is a performance tweak...?
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list