Paul Melvin wrote:
If I have a set of numbers, or strings etc. which have been generated
and I then want to do something with them, for example a sum function
call. Is the best way to put those items in a list, or similar
container, before applying the function.
Not only "best" but "necessary". the sum () builtin takes an
iterator -- often a list, but needn't be -- of numbers. Generally
you'll have the numbers in some container already. If you've used
range, then range already generates a list:
<code>
nums = range (5, 10) # the numbers 5, 6, 7, 8, 9 in a list
print sum (nums)
</code>
If you're getting a user to enter them (say) by raw_input, you'll
need to create your own list:
<code>
nums = []
while True:
snum = raw_input ("Enter number (0 to finish): ")
num = int (snum)
if num == 0:
break
else:
nums.append (num)
print nums, "->", sum (nums)
</code>
(Obviously tons of assumptions in that code, which is just
a Noddy example)
TJG
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor