[Tutor] sum of a list
Bill Mill
bill.mill at gmail.com
Wed Aug 11 05:07:26 CEST 2004
> sum() works with any sequence, not just lists.
Not true. Witness:
>>> sum('123')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> sum('123', '')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sum() can't sum strings [use ''.join(seq) instead]
It works for any sequence composed of numeric elements, AFAICT. The
documentation is unclear on this point, saying only that the sequences
are *normally* numbers, although it explicitly disallows strings. I
assume that it works on anything which can be added to start and which
is not a string or a class derived from a string.
Peace
Bill Mill
More information about the Tutor
mailing list