How to calculate the sum of elements in a list in Python

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Thank you very much man
I wrote 80 lines of code to do what you did in 3 lines

ijnvnadm
Автор

Sometimes sum(list) will not work. In those cases, import numpy and use numpy.sum(list)

taiftahmid
Автор

Just type

Print("sum is", sum(list))

BRUH-sklp
Автор

# Without Buil-in Function :

number = int(input("How Many Numbers? "))
lst = []

for _ in range(number) :
numbers = int(input("Enter Your Numbers: "))
lst.append(numbers)

result = 0

for num in lst :
new_num = num + result
result = new_num

print(f"Sum Of Your List Is: {result}")


#

# With Buil-in Function :

number = int(input("How Many Numbers? "))
lst = []

for _ in range(number) :
numbers = int(input("Enter Your Numbers: "))
lst.append(numbers)

print(f"Sum Of Your Numbers Is: {sum(lst)}")

adxmreal
Автор

num1=int(input("How many numbers want to insert ? :"))
lst=[]

for i in range(num1):
num2=int(input(" Enter your number :"))
lst.append(num2)
print("Here is list ", lst)
result=0
for i in lst:
result=result+i


print("Sum of all number is : ", result)

outpt:
How many numbers want to insert ? :4
Enter your number :30
Enter your number :25
Enter your number :35
Enter your number :50
Here is list [30, 25, 35, 50]
Sum of all number is : 140

mukundbiradar
Автор

Is there a bird chirping in the background?

qq