osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Hi am new to python


On Mon, Aug 31, 2015 at 5:27 PM, Chubasco Diranga <alpharulz at gmail.com> wrote:
> Can anyone please help me with the following please?
>
> My question is in a while loop; how do l sum all the numbers in the given list (list_a)?
>
> list_a = [8, 5, 2, 4]
> sum_a = 0 # for storing the sum of list_a
> i = 0 # for looping through the list_a# Use a while loop to sum all numbers in list_a# If you store the sums into sum_a
> print(sum_a) # should print 19

You don't need a loop at all. Take a look at the builtin sum function:
https://docs.python.org/3.4/library/functions.html#sum