otal= 0 for i in [1,2,3,4,5] : total=total+1 print(total) | Sololearn: Learn to code for FREE!

0

otal= 0 for i in [1,2,3,4,5] : total=total+1 print(total)

It give output 5.is it counting total elements of list

6/23/2020 4:13:43 PM

SAADAT ALI

3 Answers

New Answer

+2

Yes, total = total + 1 0=0+1 2=1+1 3=2+1 4=3+1 5=4+1 if you want to sum the elements, try: total=total+i

+1

Yes, since you increase the value of total by 1 and not by the value. To get the sum you'll need to sum up: list = [ 1, 2, 3, 4, 5 ]; ... loop ... total += list[index]

+1

SAADAT ALI Yes it is counting the total number of elements in the list If you want total number of elements in the list , you can use len() function Here's its syntax πŸ‘‡ len(list) If you want total sum of elements present in the list, you can use sum() function Here's its syntax πŸ‘‡ sum(list) I hope you understood πŸ™‚