iTechtalk

Quick Search

Go Advanced

Member Login

Not registered? | Forgot Password
 
Register
Welcome
 
iTechtalk > Tutorials > Programming » Totaling the Values in a List in Python
Reply
Old 11-04-2008, 09:34 AM   #1 (permalink)
 
achiever's Avatar
 
Junior Member
Join Date: Nov 2008
Posts: 15
Post Totaling the Values in a List in Python

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
achiever is offline   Reply With Quote
 
Reply

Bookmarks

Tags
list, python, totaling the values

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boundary and typical values in Programming kelvin Programming 0 12-01-2008 07:44 AM
Values and variables in a C++ Program volt Programming 0 11-29-2008 04:22 AM
Getting Date and Time Values in Flash prem Graphics 0 11-13-2008 10:46 AM
Passing a List as an Argument to a Function in Python achiever Programming 0 11-04-2008 09:37 AM
Averaging the Values in a List in Python achiever Programming 0 11-04-2008 09:35 AM


 

Content Relevant URLs by vBSEO 3.3.0