n To understand the use of lists (arrays) to represent a collection of related data.

n To be familiar with the functions and methods available for manipulating Python lists.

n To be able to write programs that use lists to manage a collection of information.

Python Programming, 2/e 3

Objectives

n To be able to write programs that use lists and classes to structure complex data.

n To understand the use of Python dictionaries for storing nonsequential collections.

Python Programming, 2/e 4

Example Problem: Simple Statistics

n Many programs deal with large collections of similar information. n Words in a document n Students in a course n Data from an experiment n Customers of a business n Graphics objects drawn on the screen n Cards in a deck

n Statistical analysis routines might come in handy some time, so lets add the capability to use this code as a module by adding: if __name__ == '__main__': main()

Python Programming, 2/e 40

Lists of Objects

n All of the list examples weve looked at so far have involved simple data types like numbers and strings.

n We can also use lists to store more complex data types, like our student information from chapter ten.

Python Programming, 2/e 41

Lists of Objects

n Our grade processing program read through a file of student grade information and then printed out information about the student with the highest GPA.

n A common operation on data like this is to sort it, perhaps alphabetically, perhaps by credit-hours, or even by GPA.

Python Programming, 2/e 42

Lists of Objects

n Lets write a program that sorts students according to GPA using our Sutdent class from the last chapter.

n Get the name of the input file from the user Read student information into a list Sort the list by GPA Get the name of the output file from the user Write the student information from the list into a file

Python Programming, 2/e 43

Lists of Objects n Lets begin with the file processing. The

following code reads through the data file and creates a list of students.