# Code Example: How To Add Numbers In A List Python

Are you looking for a program in Python on how to add numbers to a list? This article will show you different methods and their runtime that you can use to add the numbers to a list.

In python, you can simply use the sum method if a list of numbers is passed as an argument to the function. Though there are other methods as well that you can use one such is using the for loop or while loop.

## Add Numbers In a List Using Sum Method in Python

Let us see in the below example code the usage of the sum method that you can use to get the sum of a list instantly.

```#initializing the List of Number

#Adding the Numbers using Sum Method

#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(sumOfList))```

Output:

``Total Sum of the List is = 135``

## Using For Loop to Add Numbers of a List in Python

You can use the For loop as well to add all the numbers in the Python list. This method can be really handy when you have a list containing Numbers and non-numbers and you only want to add the numbers found in the list.

Though you can use the for loop to find the sum for the normal list of numbers as well. Let us see in the below two code examples for For Loop.

```#initializing the List of Number

#Adding the Numbers using For Loop
totalSum = 0

#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(totalSum))```

Output:

``Total Sum of the List is = 135``

Now modify the above code when you have a list containing alphanumeric characters and you want to add only numbers.

```#initializing the List of Number

#Adding the Numbers using For Loop
totalSum = 0

#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(totalSum))```

Output:

``Total Sum of the List is = 135``

## Using While Loop To Add Numbers Of A List

You can use the while loop as well to add the numbers. Let us see in the below example code the usage of while to add numbers from a list in Python.

```#initializing the List of Number

#Adding the Numbers using While Loop
totalSum = 0
while index>=0:
index = index - 1

#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(totalSum))```

Output:

``Total Sum of the List is = 135``

In the while loop, you need to create a variable to indicate the index of the list. And we are decreasing the index till we reach the start of the index in the above code.

## Using Recursion in Python to Add the Numbers of A List

You can use the recursion as well to add the list of a number given. This method will be called recursively till the length of the given array of numbers or list of numbers in Python. Let us see in the below example code the usage of recursion.

```#initializing the List of Number

def recursiveSum(giveNumberList, len):
#Return 0 when Reaches End of List
if len<0:
return 0
return giveNumberList[len] + recursiveSum(giveNumberList, len-1)

#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(totalSum))```

Output:

``Total Sum of the List is = 135``

## Adding Numbers From User Input as A List in Python

If you want to take the list of numbers as input from your user then you need to follow the below code example do to so.

```#Taking List of Number as Input from User
inputNumbers = int(input("How Many Numbers to be Added.\n"))

#Initializing the Empty Number List
numberList = []

#Appending Numbers from User
for i in range(0, inputNumbers):
numberList.append(int(input("Enter the number.\n")))

totalSum = sum(numberList)

#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(totalSum))```

Output:

``````How Many Numbers to be Added.
5
Enter the number.
1
Enter the number.
2
Enter the number.
3
Enter the number.
4
Enter the number.
5
Total Sum of the List is = 15``````

## Wrap Up

That’s it for this article. I hope you were able to get the answer related to how to add a list of numbers in Python. Let me know if you have any better method than the one discussed above. I will be happy to add here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.