[Solved] Sum of Elements Problem with Python

Sum of Elements: Write a program to get size of list and  elements of the list and to find the sum of all the numbers inside the list, with the help of Lambda Expression and display the sum of elements.

Input Format:
The first line of input corresponds to the size of list ‘n’.
The next ‘n’ line of input corresponds to the elements in the list .

Refer sample input for formatting specifications.

Output Format:
The output consists of sum of elements in the list.

Refer sample output for formatting specifications.
 

Problem Constraints:
n > 0 print the sum of the elements in the list, else display as “Invalid Input“.
 

Sample Input/Output 1:

Enter the number of elements in list
6
Enter the elements
5
8
10
20
50
100

The sum of of elements in list is 193

Sample Input/Output 2:

Enter the number of elements in list
0
Invalid Input


Additional Sample TestCases

Sample Input and Output 1 :

Enter the number of elements in list6
Enter the elements
5
8
10
20
50
100
The sum of of elements in list is 193

Sample Input and Output 2 :

Enter the number of elements in list0
Invalid Input

Solution

n = int(input("Enter the number of elements in list\n"))

if n > 0:
    list = []
    print("Enter the elements")
    for i in range(n):
        list.append(int(input()))
        # cal sum
        l = lambda x: sum(x)
    print("The sum of of elements in list is",l(list))
else:
    print("Invalid Input")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *