write a python program, where list 'L' passed as an
argument and sum of all
values which are ending
ending with 3 from a list.
fast please
Answers
Answered by
3
Solution.
There are many approaches for solving this problem.
1. Using for loop.
def Calculate(list):
sum=0
for i in list:
if i%10==3:
sum+=i
return sum
2. Using list comprehension.
def Calculate(list):
return sum(i for i in list if i%10==3)
Here, we have to transverse through array elements. If the number has 3 at its unit place, then we will add the number to the sum variable. The sum variable is then returned by the function.
Sample Output.
list=[1,2,33,3,63,85,44,23,94,93]
print("Sum of elements in the list ending with 3 is:",Calculate(list))
> Sum of elements in the list ending with 3 is: 215
See attachment for output.
Attachments:
Similar questions