Python question:
print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))
9
10
What is logical behind it? Thank you
numpy.sum() signature is as follows (with some arguments omitted):
numpy.sum(a, axis=None, dtype=None, out=None, ...)
Python's sum signature:
sum(iterable, start=0)
sum iterates over supplied iterable, sums the values, and then adds -1 (i.e. substracts 1).
numpy.sum just sums all the values from supplied iterable, and receives an axis parameter as 1, which in your case doesn't change the behaviour.
range, and it has different boundry rules.0+1+2+3+4+(-1). The third line is equivalent to0+1+2+3+4onaxis=-1. Useimport numpy as npand then rewrite the third line asnp.sum(range(5),-1). And read the documentation for np.sum()range. The second argument innp.sum()isaxis. It will only sum the items in the first argument.