Sum of List Elements

Tags: List, Loop

Write a Python program that finds the sum of all numbers in a list. The list will be provided as a single line input with space-separated numbers.

Example 1:

Input: 1 2 3 4 5
Output: 15

Example 2:

Input: 10 20 30 40
Output: 100

You can split the input string into a list of numbers. Then use a for loop to iterate through the list and calculate the sum.

nums = list(map(int, input().split()))
total = 0
for num in nums:
    total += num

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!