Which version of this code is better?
Ver 1.
a = 0
for i in xrange( 1000 ):
if i % 3 == 0 or i % 5 == 0:
a += i
print a
Ver 2.
from operator import add
print reduce( add,
( i for i in xrange( 1000 )
if i % 3 == 0 or i % 5 == 0 ) )
Things I like about version one is that it is more readable, but it creates requires a variable. If you had to choose, which one would you argue is better?
sum()
function with the generator used in version 2. \$\endgroup\$list descriptor
a correct name for that ? Should not befor-loops
vsgenerator comprehensions
? \$\endgroup\$