StephanieS
0
Q:

how to calculate the sum of a list in python

# Python code to demonstrate the working of  
# sum() 
numbers = [1,2,3,4,5,1,4,5] 
  
# start parameter is not provided 
Sum = sum(numbers) 
print(Sum) # result is 25  
# start = 10 
Sum = sum(numbers, 10) 
print(Sum) # result is 10 +25 = 35
12
# Python code to demonstrate the working of  
# sum() 
   
numbers = [1,2,3,4,5,1,4,5] 
  
# start parameter is not provided 
Sum = sum(numbers) 
print(Sum) 
  
# start = 10 
Sum = sum(numbers, 10) 
print(Sum) 
6
# 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 variale total 
for ele in range(0, len(list1)): 
    total = total + list1[ele] 
  
# printing total value 
print("Sum of all elements in given list: ", total) 
-1

New to Communities?

Join the community