Can you sum all of the elements in the list, how about to multiply them and get the result?



# the basic way
s = 0
for x in range(10):
    s += x

# the right way
s = sum(range(10))


# the basic way
s = 1
for x in range(1, 10):
    s = s * x

# the other way
from operator import mul
reduce(mul, range(1, 10))

No comments:

Post a Comment