How to Get the Sum of Number Digits in List using Python
In this tutorial, we will program 'How to Get the Sum of Number Digits in a List using Python.' We will learn how to calculate the sum of the digits of numbers in a given list. The objective is to find the sum of the digits of the numbers in the list. I will provide a sample program to demonstrate the actual coding process in this tutorial.
This topic is very easy to understand. Just follow the instructions I provide, and you can do it yourself with ease. The program I will show you covers the basics of programming to find the sum of the digits of numbers in a list. I will do my best to provide you with a simple method for getting the sum. So, let's start with the coding.
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- test_list = [7, 12, 16, 28]
- print("\n\n================ Get the Sum of Number Digits in List ================\n\n")
- print("The original number is : " + str(test_list))
- res = []
- for ele in test_list:
- sum = 0
- for digit in str(ele):
- sum += int(digit)
- res.append(sum)
- print ("The sum : " + str(res))
This script calculates the sum of the digits for each number in the list [7, 12, 16, 28]. It iterates through each number, converts it to a string to access each digit, sums the digits, and appends the result to a new list. Finally, it prints the original list and the list containing the sum of digits for each number.
Output:

There you have it we successfully created How to Get the Sum of Number Digits in List using Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language