sum of digits in list python code example
Example 1: python sum of digits
def sum_digits(n):
s = 0
while n:
s += n % 10
n //= 10
return s
Example 2: how to calculate the sum of a list in python
numbers = [1,2,3,4,5,1,4,5]
Sum = sum(numbers)
print(Sum)
Sum = sum(numbers, 10)
print(Sum)
Example 3: how to sum digits of a number in python
number = 123
sum_of_digits = sum(int(digit) for digit in str(number))
print(sum_of_digits)
Example 4: how to add list numbers in python
lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Sum of elements in given list is :", sum(lst))