xxxxxxxxxx
def list_sum(num_list):
the_sum = 0
for i in num_list:
the_sum = the_sum + i
return the_sum
print(list_sum([1, 3, 5, 7, 9]))
xxxxxxxxxx
"""If the numbers inside a list are all integers,
floats, or a combination of both, you can use the sum() function.
"""
number_list=[1, 2, 3, 4, 5, 7, 8, 5.5, 7.987, 7845.12454, 100, 200, 300]
print(sum(number_list))
# Now that I have helped you, don't you want to like this post?
xxxxxxxxxx
# to sum all the numbers we use python's sum() function
a = [4,5,89,5,33,2244,56]
a_total = sum(a)
xxxxxxxxxx
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3
>>> sum(num for num in myList if not num%2)
60
xxxxxxxxxx
# Python program to find sum of elements in list
total = 0
# creating a list
list1 = [11, 5, 17, 18, 23]
# Iterate each element in list
# and add them in variable total
for ele in range(0, len(list1)):
total = total + list1[ele]
# printing total value
print("Sum of all elements in given list: ", total)
xxxxxxxxxx
int knapsackRec(int[] w, int[] v, int n, int W) {
if (n <= 0) {
return 0;
} else if (w[n - 1] > W) {
return knapsackRec(w, v, n - 1, W);
} else {
return Math.max(knapsackRec(w, v, n - 1, W), v[n - 1]
+ knapsackRec(w, v, n - 1, W - w[n - 1]));
}
}
xxxxxxxxxx
def calculate_sum(numbers):
total = sum(numbers)
return total
numbers = [1, 2, 3, 4, 5]
result = calculate_sum(numbers)
print("Sum:", result)