How to Add Every Element of a List in Python
Using Python as well as other programs, you often encounter cases in which you will need to find the sum of an entire list of numbers within the course of a program you are writing. Python's simple "for-loop" syntax makes this process simple and painless; it allows you to add every element within a list quickly by following only a few steps.
Instructions
-
-
1
Open the "IDLE" Python debugger.
-
2
Type the following command:
alist = [1,2,3,4,5,6,7]
sum = 0
for x in alist:
sum += xprint (sum)
-
-
3
Press "F5" on your keyboard to test the program.
-
1