Join Club ShowMeDo to Learn Python!

This series is a part of Club ShowMeDo. Click to learn why you should join our club with a simple monthly subscription.

  • We teach Python and all the cool modules
  • Topics include beginner Python, GUIs, Web and Desktop Apps
  • Long, specially crafted video tutorials just for you
  • Club video tutorials extend our normal Free content
  • Save your time - we've done the research for you
  • Keep your skills up-to-date
  • Learn at your own pace, everything you need is shown to you
  • Over 12 hours of archived material are waiting for you in the club

Name: [002] Ian Ozsvald
Member: 128 months
Authored: 181 videos
Description: I am the co-founder of ShowMeDo (see, author of `The Screencasting Handbook <>`_ and the founder of the professional screencast production company `ProCasts <>`_: .. image:: ...

Averaging the data [ID:254] (13/17)

in series: Python 101 - easygui and csv

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

To calculate an average we need to introduce a new 'average' function - you'll need to write a test for it. To call the function we need to have our numerical data stored in a list, so we introduce 'measured1' and show how to populate the list.

Task: Write the unit-test for the average function and replicate measured1 to create measured2 (both solved in the next episode)

Created May 2007, running time 5 minutes

import csv
import sys
import easygui

def getFileAndPath():
    "Get fully-qualified path to the csv file"
    fileAndPath = easygui.fileopenbox(title="Select .CSV file")
    print "Using:",fileAndPath 
    return fileAndPath

def getLinesInCSVFile(fileAndPath):
    "read lines in CSV file, return a list of these lines"
    linesInCSV = []
    reader = csv.reader(open(fileAndPath, "rb"))
    for row in reader:
    return linesInCSV

def justNumbers(listOfStrings):
    "True if the list contains just numbers represented as strings"
    # e.g. ['22.4', '23.9']
    isJustNumbers = True
    for item in listOfStrings:        
            nbr = float(item)            
        except ValueError:
            isJustNumbers = False
    return isJustNumbers

def getNumbers(listOfStrings):
    "Convert a list of strings-of-numbers to a list of numbers, e.g. ['22.4', '23.9'] -> [22.4, 23.9]"
    numbers = []
    for item in listOfStrings:
        nbr = float(item)            
    return numbers

def average(values):
    """Computes the arithmetic mean of a list of numbers"""
    return sum(values, 0.0) / len(values)

if __name__ == "__main__":
    # get the file-name
    #fileAndPath = getFileAndPath()
    # NOTE quick hack to make our test/development process quicker
    fileAndPath = "C:/LearningPython_CSV/measured.csv"
    print fileAndPath
    # read the CSV file
    linesInCSV = getLinesInCSVFile(fileAndPath)
    print linesInCSV
    # print out the headings and the contents
    print "Headings:",linesInCSV[0]
    measured1 = []
    for n in range(1,4):
        line = linesInCSV[n]
        isJustNumbers = justNumbers(line)
        if not isJustNumbers:
            print "ERROR!  Expected a line of numbers, instead we got:",line
        # we only get here if justNumbers reports that we only have numbers
        # so we can extract the list of floating-point numbers
        numbers = getNumbers(line)
        #print numbers
        print measured1        
    averaged1 = average(measured1)
    print averaged1

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 0 (since July 30th)
  • Plays in last week: 0
  • Published: 111 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>

All comments excluding tick-boxed quick-comments

hi Ian,

I've noted that in the sum function, the 2nd variable is supposed to be an integer that is added to the resulting sum, not the default sum as mentioned in the video. It might be because I'm using python 3 or something, but I thought I should point this out.


I took this a step further, looking up how to initialize a list of length N with zeros using [0.0] * N, so that I could build up my sums and counts for N columns, not just two:

print "Headings:",linesInCSV[0]

cols = len(linesInCSV[0]) # use number of headings to tell number of columns

sums = [0.0] * cols

counts = [0.0] * cols

averages = [0.0] * cols

for n in range(1,len(linesInCSV)): # NOTE: runs length of CSV

line = linesInCSV[n]

isJustNumbers = justNumbers(line)

if not isJustNumbers:

print "ERROR: Expected line of numbers. Instead: ", line


numbers = getNumbers(line)

print numbers

for c in range(0,cols):

sum = sums[c] + numbers[c]

sums[c] = sum

count = counts[c] + 1

counts[c] = count

for c in range(0,cols):

averages[c] = sums[c] / counts[c] # no average function needed here

print 'Averages:'

print averages





gives OUTPUT:

Headings: ['a', 'b', 'c']

[1.0, 2.0, 3.0]

[3.0, 4.0, 5.0]


[2.0, 3.0, 4.0]

Averaging the data (13/17)

2:16 def average() as sum(,0.0) / len()

3:20 measured1 = [] before appending

Hi Ryan. Near the end of the series:

I write out a new CSV file which contains the averages, which is 'adding' new data to the CSV file. Is that what you mean?

The averages are stored in-memory.


Great challenge - that was very difficult. Do you also go over putting information dirrectly back into the CSV file or is it stored into the temporary list?

Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for Ian

3 Minute Oveview (What Does Python Look Like?)

simple and informative. you draw me in
70 months ago


Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.