How To find the sum of all the elements in a list.
li = []
n = int(input("Enter the number of elements: "))
for i in range(n):
elem = int(input("Enter the elements: "))
li.append(elem)
print("The list:",li)
print("The sum of the list = {}".format(sum(li)))
li = []
n = int(input("Enter the number of elements: "))
for i in range(n):
elem = int(input("Enter the elements: "))
li.append(elem)
print("The list:",li)
print("The sum of the list = {}".format(sum(li)))
Enter the number of elements: 4
Enter the elements: 30
Enter the elements: 30
Enter the elements: 30
Enter the elements: 30
The list: [30, 30, 30, 30]
The sum of the list = 120
li = []
n = int(input("Enter the number of elements: "))
for i in range(n):
elem = int(input("Enter the elements: "))
li.append(elem)
print("The list:",li)
print("The sum of the list = {}".format(sum(li)))
Enter the number of elements: 10
Enter the elements: 4
Enter the elements: 5
Enter the elements: -10
Enter the elements: 20
Enter the elements: -50
Enter the elements: 30
Enter the elements: -9
Enter the elements: -2
Enter the elements: 0
Enter the elements: 7
The list: [4, 5, -10, 20, -50, 30, -9, -2, 0, 7]
The sum of the list = -5
numbers = []
n = int(input("Enter the elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
def findSum(numbers):
total = 0
for j in numbers:
total += j
return total
if __name__ == '__main__':
temp = findSum(numbers)
print("The created list: ",numbers)
print("The Sum = ",temp)
numbers = []
n = int(input("Enter the elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
def findSum(numbers):
total = 0
for j in numbers:
total += j
return total
if __name__ == '__main__':
temp = findSum(numbers)
print("The created list: ",numbers)
print("The Sum = ",temp)
Enter the elements: 4
Enter the elements: 30
Enter the elements: 40
Enter the elements: 10
Enter the elements: 20
The created list: [30, 40, 10, 20]
The Sum = 100
numbers = []
n = int(input("Enter the elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
def findSum(numbers):
total = 0
for j in numbers:
total += j
return total
if __name__ == '__main__':
temp = findSum(numbers)
print("The created list: ",numbers)
print("The Sum = ",temp)
Jubayer Hossain
Please share your tips or tricks for solving this problem in a better way! Happy Coding!