|
|
#1 (permalink) |
|
Status: Junior Member
Join Date: Nov 2008
Posts: 15
|
Totaling the Values in a List in Python:-
Assuming a list contains numeric values, to calculate the total of those values you use a loop with an accumulator variable. The loop steps through the list, adding the value of each element to the accumulator. Program (specified below) demonstrates the algorithm with a list named numbers. (total-list.py) # This program calculates the total of the values in a list #. def main ( ): # Create a list. numbers = [2, 4, 6, 8, 10] # Create a variable to use as an accumulator. total = 0 # Calculate the total of the list elements. for value in numbers: total += value # Display the total of the list elements. print 'The total of the elements is', total # Call the main function. main ( ) Output The total of the elements is 30 |
|
|
|
![]() |
| Bookmarks |
| Tags |
| list, python, totaling the values |
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Boundary and typical values in Programming | kelvin | Programming | 0 | 12-01-2008 08:44 AM |
| Values and variables in a C++ Program | volt | Programming | 0 | 11-29-2008 05:22 AM |
| Getting Date and Time Values in Flash | prem | Graphics | 0 | 11-13-2008 11:46 AM |
| Passing a List as an Argument to a Function in Python | achiever | Programming | 0 | 11-04-2008 10:37 AM |
| Averaging the Values in a List in Python | achiever | Programming | 0 | 11-04-2008 10:35 AM |