How to Calculate the Sum of Numbers in a Given List in Python


In this example we will show how to calculate the sum of all numbers in a given list.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

n = input("Please input a number: ")
a = list(n)
sum = 0
for i in range(len(a)):
    sum += int(a[i])
print("The sum is: ", sum)

Output:

Please input a number: 45623
The sum is: 20
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments