How to add up a list of items?


#1

Question

As part of this exercise, the function has to add up the values for 4 parameters (food_bill, electricity_bill, internet_bill, rent) and compare against the budget parameter. If a list of expenses was provided instead of individual parameters, how could those be added easily?

Answer

The sum() function is provided by Python to easily add up a list of items. If given a list of numeric values, the sum() function will add them up and return the value of the summation.

expenses = (42, 17, 99, 34, 10)
print(sum(expenses))
# OUTPUTS: 202