[Tutor] "if n % 2 == 0" vs. "if not n % 2" compared for speed:aesthetics lose

ALAN GAULD alan.gauld at btinternet.com
Tue Nov 23 17:35:39 CET 2010



> >  Did you try:
> >
> > if n % 2: pass
> > else:  do it  here?
> >
> > ie inverting the logic and omitting the equality  test.
> 
> Alan, since I'm doing comparison's, why do you suggest omitting  the
> equality test?

Because the equality test with 0 might be slower than the implicit test of 
trueness.

> Also, what do you mean by "do it  here?"

I mean whatever you wanted to do inside the n%2==0 block.

Alan G.



More information about the Tutor mailing list