Q. Write a program that prints the sum of the even-indexed elements of L, minus the sum of the odd-indexed elements of L.
For example:
For list[1, 2, 3, 4, 5, 6] it should print -3 (that is, (1 + 3 + 5) - (2 + 4 + 6))
For list([1, 2, 3]) it should print 2 (that is, 1 - 2 + 3)
Answer =
L = eval (input("Enter the number list :- ")) even = odd = 0 for i in range (len(L)) : if i % 2 == 0 : even += L[ i ] else : odd += L[ i ] print ("(sum of even-indexed elements) - (sum of odd-indexed elements) :-", even - odd)
can you explain b+=a[i]
ReplyDeleteIt will add element of List a into b. It can also be written as :-
Deleteb = b + a[i]
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )