Hi am new to python
denismfmcmahon at gmail.com
Thu Sep 10 00:35:49 CEST 2015
On Tue, 08 Sep 2015 17:44:26 -0500, Nassim Gannoun wrote:
> My question is in a while loop; how do l sum all the numbers in the
> given list (list_a)?
You don't normally use a while loop or a counter to iterate over a list.
Such a question should only be used as a precursor to discussing better
methods of achieving the required result.
> sum_a = 0
> i = 0
> while i < (len(list_a)):
> sum_a += list_a[i]
> i += 1
Better, use a for loop:
s = 0
for i in list_a:
s += i
Even better, use sum():
s = sum(list_a)
And if you only want to display the answer:
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list