0

Is there a way to do this efficiently

for loops allow you to easily iterate through lists. Given a list of numbers, output their sum. Sample Input 1 3 7 5 Sample Output 16

26th Aug 2021, 2:10 PM
Ervan
Ervan - avatar
9 Respuestas
+ 3
Ervan sorry, I can't take your post serious. I laugh with you.
26th Aug 2021, 2:15 PM
Oma Falk
Oma Falk - avatar
+ 1
s = sum(map(int, input().split())) print(s) This is a SHORTER WAY
26th Aug 2021, 2:13 PM
Obloev Komronbek
Obloev Komronbek - avatar
+ 1
Ervan , it can be done shorter by using built-in sum function. Look at the example. https://code.sololearn.com/czceW839hkxG/?ref=app
26th Aug 2021, 2:15 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
With "for i in list" the "i" already is the element of the list – just add it to sum – no need to check it with "if"
26th Aug 2021, 2:29 PM
Lisa
Lisa - avatar
+ 1
Delicate Cat These codes perform the same function, but it is preferable that you do not use sum as a variable. Because python uses it as a function
26th Aug 2021, 2:30 PM
Obloev Komronbek
Obloev Komronbek - avatar
+ 1
l=[1,2,3,4,5,4,2,5] print(sum(l)) this is simplest way
27th Aug 2021, 5:47 PM
CHINNI KRISHNA KONGALA
0
#btw this is how i do it list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sum = 0 for i in list: if i == 1: sum+=1 elif i == 2: sum+=2 elif i == 3: sum+=3 elif i == 4: sum+=4 elif i == 5: sum+=5 elif i == 6: sum+=6 elif i == 7: sum+=7 elif i == 8: sum+=8 elif i == 9: sum+=9 print(sum)
26th Aug 2021, 2:11 PM
Ervan
Ervan - avatar
0
Surely there is a SHORTER WAY TO DO IT than that
26th Aug 2021, 2:11 PM
Ervan
Ervan - avatar
0
Open the code below to view how I solved your problem all the explanation is within the code each statement that requires explanation I add comment before it. https://code.sololearn.com/c0YL9jKd8Cxm/?ref=app
27th Aug 2021, 2:42 PM
Rufai Limantawa