MIME-Version: 1.0 Content-Location: file:///C:/F48430D3/Week3.htm Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="us-ascii" Week 3

Week 3

List, Files, and Functions

Indiana U= niversity

Com= puter Science A202 / A598

and Informatics I211

 

This week’s success strategy

<= ![if !supportLists]>n   Don’= t wait until the last minute to do assignments

<= ![if !supportLists]>q   you may need help, which may n= ot be possible at the last minute

<= ![if !supportLists]>q   it may take longer than you th= ink

<= ![if !supportLists]>q   there may be last minute techn= ical problems, which are seldom excuses for not submitting your assignment on ti= me

<= ![if !supportLists]>q   doing assignments is the best = preparation for quizzes and the final

<= ![if !supportLists]>n   Of course = the strategies highlighted each week apply to the whole course!

Review: sequences

<= ![if !supportLists]>n    There are three types of se= quences: string, list, and tuple

<= ![if !supportLists]>q&nb= sp;  Strings are homogeneous, all elements are of the same type (characters) and = are immutable, they can’t be modified

<= ![if !supportLists]>n    Sequence operators<= /span>

<= ![if !supportLists]>q   s1 + s2 concatenates sequences s1 and s2

<= ![if !supportLists]>q&nb= sp;  s * n r= epeats sequence s n times

<= ![if !supportLists]>q   <sequence> [ <index> ] returns the indicated sequence element

<= ![if !supportLists]>q&nb= sp;  <sequence> [ [<start>] : [<stop>] ] returns a sequenc= e slice

<= ![if !supportLists]>n    Iteration over elements of a sequence

<= ![if !supportLists]>q   for <variable> in <sequence> : = <body>

<= ![if !supportLists]>n    Function = len(<sequence>) returns the sequence length

<= ![if !supportLists]>n    Function range(…<= b>) returns a list of numbers

<= ![if !supportLists]>q   a list of numbers might look li= ke [0, 1, 2, 3, 4]

 

This week

<= ![if !supportLists]>n   More fun w= ith sequences

<= ![if !supportLists]>n   Objects and method calls

<= ![if !supportLists]>n   More on li= sts

<= ![if !supportLists]>n   Simple fil= e I/O

<= ![if !supportLists]>n   Writing and using Python applications

<= ![if !supportLists]>q   not covered by your text<= /o:p>

<= ![if !supportLists]>n   More on functions

 

 

Parallel assignment

<= ![if !supportLists]>n    Parallel assignment= allows several variables to be assigned in one statement

<= ![if !supportLists]>q   convenient for naming elements = of a short fixed-length sequence

<= ![if !supportLists]>q   syntax: <variable_0>,…,<variable_n> <= b>=3D <sequence exp>

<= ![if !supportLists]>n     sequence length must equal the = number of variables

<= ![if !supportLists]>n     also works in for statem= ents

<= ![if !supportLists]>q   examples

>>> i, j =3D [= 1, 2]

>>> print i, j

1 2

>>> a, b, c = =3D 'efg'

>>> print a, b, c

e f g

>>> for x, y i= n ['ab', 'cd']:

   =    print x, y

   =   

a b

c d

Objects and method calls

<= ![if !supportLists]>n   String, lists, open files, and many other Python types are represented as objects

<= ![if !supportLists]>n   Method = calls may be used to invoke object behavior

<= ![if !supportLists]>q&nb= sp;  syntax: <object exp>.<method name>(<argument exps><= b>)

<= ![if !supportLists]>q   may be thought of as sending a message with consisting of the method name and arguments to the object, whi= ch responds with behavior determined by the method

<= ![if !supportLists]>q   a method is a kind of function associated with an object class

<= ![if !supportLists]>n   String mod= ule functions may be invoked as methods

<= ![if !supportLists]>q   the first argument moves to be= come the object expression

<= ![if !supportLists]>q&nb= sp;  example: s.replace(old, new)
instead of string.replace(s, old new)=

<= ![if !supportLists]>q   may avoid the need to import t= he string module

 

Lists

<= ![if !supportLists]>n   (transcrip= t)

<= ![if !supportLists]>n   List liter= al and printed representation

<= ![if !supportLists]>q&nb= sp;  syntax: [ <elt_0>,…, <e_(length-1)> ]

<= ![if !supportLists]>q   empty list with no elements: <= b>[ ]

<= ![if !supportLists]>n   Lists are = heterogeneous

<= ![if !supportLists]>q   lists elements may be of any t= ype (even other lists)

<= ![if !supportLists]>n     example: [1, [‘a’= ;, True]]

<= ![if !supportLists]>n   Lists are = mutable sequences: a list can be modified

<= ![if !supportLists]>q   list elements may be assigned = new values (like arrays elements)

<= ![if !supportLists]>n     <list exp> [ <i= ndex exp> ] =3D <value exp>

<= ![if !supportLists]>q   lists may change in length (no= t in your text)

<= ![if !supportLists]>n&nb= sp;    remove an element from a list: de= l <list exp> [ <index exp> ] =

<= ![if !supportLists]>n     add<= /span> an element at the end of a lis= t: <list exp> . append(<value exp>)

Handin exercise

<= ![if !supportLists]>n      Write a function swap(list, i, j) that exchanges the elements indicate by i  and j

>>&= gt; lst =3D [1, 2, 3]

>>&= gt; swap(lst, 0, 2)

>>&= gt; lst

[3, 2, 1]=

<= ![if !supportLists]>n      Answer

def swap(lst= , i, j):

   = ;  temp =3D lst[i]=

   = ;  lst[i] =3D lst[j]

   = ;  lst[j] =3D temp

What's a file, and why have them?

<= ![if !supportLists]>n    A file is a just a sequence of= bytes

<= ![if !supportLists]>q   file input does not involve dat= a type checking

<= ![if !supportLists]>n     file name extensions= often suggest the type of data= in the file

<= ![if !supportLists]>q&nb= sp;    examples: paper.doc, notes.txt, and program.exe

<= ![if !supportLists]>q   meaningless results if files ar= e not read in a way that is consistent with the way they were written<= /span>

<= ![if !supportLists]>n    Files are usually stored on di= sk

<= ![if !supportLists]>q   disk storage is persistent

<= ![if !supportLists]>n     data on disk is not lost when t= he power is turned off

<= ![if !supportLists]>n     data on disk is usually not los= t when a system "crashes"

<= ![if !supportLists]>n     most computer "main" = memory is volatile (not persistent)

<= ![if !supportLists]>q   disk storage is much less expen= sive per byte than computer memory

<= ![if !supportLists]>n     disk storage is much larger tha= n main memory

<= ![if !supportLists]>n&nb= sp;    example: 512MB memory for $60 vs 200GB disk for $90, so this memory is about 250 times cheaper

<= ![if !supportLists]>q   disks are slower than memory. How mu= ch?

How fast, or slow?

<= ![if !supportLists]>n    Disk random access is about a = million times slower than memory !!

<= ![if !supportLists]>q   random access: reaching any (randomly chosen) element in the same amount of time

<= ![if !supportLists]>q   disks are not perfectly random = access

<= ![if !supportLists]>n     depends on the track it is on, = and even where the disk is in rotation

<= ![if !supportLists]>q   disk data transfer can be pretty fast, so data is often moved in blocks of a few thousand bytes at a time

<= ![if !supportLists]>q   disk "pseudo-random" = access times are 10's of milliseconds

<= ![if !supportLists]>q   memory access times are 10's of nanoseconds

<= ![if !supportLists]>n    Digression: small units of tim= e

<= ![if !supportLists]>q   millisecond: a thousandth (10**-3) of a se= cond

<= ![if !supportLists]>q   microsecond: a millionth (10**-6) of a sec= ond

<= ![if !supportLists]>q   nanosecond: a billionth (10**-9) of a sec= ond

<= ![if !supportLists]>n     light travels about a foot in a nanosecond

File objects

<= ![if !supportLists]>n   Before rea= ding or writing a file is necessary to open it

<= ![if !supportLists]>q   this creates a file object<= /b> that is used for file I/O

<= ![if !supportLists]>q   the file object keeps track su= ch things as

<= ![if !supportLists]>n     your position in the file =

<= ![if !supportLists]>n     whether you are allowed to read= or write the file

<= ![if !supportLists]>n     data you may have recently writ= ten to the file

<= ![if !supportLists]>q   it also locks the file = on disk so other programs cannot use it while it is open

<= ![if !supportLists]>n   When you a= re done with a file, you should close it

<= ![if !supportLists]>q   writes any unsaved data to dis= k

<= ![if !supportLists]>q   unlocks it so others can use i= t

<= ![if !supportLists]>q   releases a limited operating s= ystem resource associated with each open file in the whole system

<= ![if !supportLists]>q   supposedly done automatically = when your program terminates, but don't count on it, and that may not be soon en= ough

Opening and closing files

<= ![if !supportLists]>n   Function open(<file name> [= , <mode>])

<= ![if !supportLists]>q   returns a file object

<= ![if !supportLists]>q   both arguments are strings

<= ![if !supportLists]>q   the optional mode is a string indicating how the file will be used

<= ![if !supportLists]>n     ‘r’ for reading the file (the defa= ult)

<= ![if !supportLists]>n     ‘w’ for writing the file (first re= moving any existing file of the same name)

<= ![if !supportLists]>n     ‘a’ for appending (writing, starti= ng at the end of an existing file)

<= ![if !supportLists]>n     a few other possibilities we wo= n't go into

<= ![if !supportLists]>n   When done = with an open file object, close it using a close() method call=

 

 

"File variable" terminology in your text is misleading

<= ![if !supportLists]>n   File method calls are messages to a file object, not a variable

<= ![if !supportLists]>q   even though file objects are u= sually stored in variables

<= ![if !supportLists]>n   Sometimes = it is important to know the difference between a variable and the value it contai= ns

<= ![if !supportLists]>q   for example all variables are = mutable, but they may contain objects that are not mutable

<= ![if !supportLists]>n     you can assign to a variable containing a string, but not change a string

<= ![if !supportLists]>q   multiple variables may contain= the same object

<= ![if !supportLists]>n     this is called aliasing:= more on this in chapter 6

Some file object methods

<= ![if !supportLists]>n   read()<= /span> returns the entire remaining contents of the f= ile in a string

<= ![if !supportLists]>q   "the file" is of cou= rse the object (or "target") of the method call

<= ![if !supportLists]>n   readline() returns the next line of the file in a string

<= ![if !supportLists]>q   newline character is included = at the end of the string

<= ![if !supportLists]>n     on Microsoft operating systems,= if a return character just precedes a newline character in a file, it is ignored (not placed in the string)

<= ![if !supportLists]>q   it is not named readLine

<= ![if !supportLists]>n   readlines() returns a list of the remaining lines of the f= ile

<= ![if !supportLists]>n   write(s= tring) writes the contents of the str= ing to the file

<= ![if !supportLists]>q   to write a line there must be a newline character at the end of the string

<= ![if !supportLists]>n   close()= closes the file and returns nothing<= /span>

<= ![if !supportLists]>q&nb= sp;  it is an error to read or write a closed file

Digression: tsv and csv form= ats

<= ![if !supportLists]>n    Data in tab-separated-value (<= span class=3DSpellE>tsv) format is common

<= ![if !supportLists]>q&nb= sp;  each line is a data record

<= ![if !supportLists]>q   fields (values) in each record= are separated by tabs

<= ![if !supportLists]>q   example: address.tsv

Sue Smith&= nbsp;            10 Arbor Way  Pleasant View NY

John Jones=       1934 Main St. Summerville   TX

<= ![if !supportLists]>n    What is the advantage of this format?

<= ![if !supportLists]>n    Answer: data appears in nice c= olumns if tabs are set appropriately

<= ![if !supportLists]>n    What is a disadvantage of this format?

<= ![if !supportLists]>n    Answer: data appearance is a m= ess if tabs are not set appropriately

<= ![if !supportLists]>q   tabs may even look like spaces<= o:p>

<= ![if !supportLists]>n    Common alternative: comma-separated-value (csv) format

<= ![if !supportLists]>q   uses commas instead of tabs

<= ![if !supportLists]>n    What if commas, or tabs, can b= e part of field values?

File programming example

<= ![if !supportLists]>n   tsvToCsv1.py converts = files from tsv to csv format

<= ![if !supportLists]>n   Problem: w= hat if the file is too big to fit in memory (twice!) ?<= o:p>

<= ![if !supportLists]>q   solution: read and write a lin= e, or even a character, at a time

<= ![if !supportLists]>n     a bit more complicated to progr= am and requires conditional tests

<= ![if !supportLists]>n   Problem: w= hat if a name contains a comma?

<= ![if !supportLists]>q   fairly common, for example: Martin L= uther King, Jr.

<= ![if !supportLists]>q   common solution: put double qu= otes around field values

<= ![if !supportLists]>n     assumes field values do not con= tain double quotes

<= ![if !supportLists]>q   exercise: modify tsvToCsv1.= py to quote fields

<= ![if !supportLists]>n&nb= sp;    example: "Sue Smith","10 Arbor Way","Pleasant View"= ,"NY"

<= ![if !supportLists]>n     solution tsvToCsv.p= y

Operating system command shells

<= ![if !supportLists]>n     Most operating systems provide = a command line interface

<= ![if !supportLists]>q    they are commonly called a s= hell, since it may be thought of as containing much of the operating system power=

<= ![if !supportLists]>q    shells  provides an alternative to a GUI (= Graphic User Interface) for many operations

<= ![if !supportLists]>q    some things can be done only vi= a a shell, or more conveniently than via a GUI

<= ![if !supportLists]>q    shells make it much easier to automate many operations, especially when dealing with files

<= ![if !supportLists]>n     The shell repeatedly prints a p= rompt, reads command line text, interprets certain special characters, and invokes= the indicated command with the given arguments

<= ![if !supportLists]>q&nb= sp;   a Windows CoMmanD shell may = be started with Start > Run > cmd

<= ![if !supportLists]>q    command line syntax: <command name> <argument> …

<= ![if !supportLists]>n      here <argument> …= ;  means zero or more arguments separa= ted by spaces

<= ![if !supportLists]>n     Some common Windows commands

<= ![if !supportLists]>q    change directory: cd <= ;directory name>

<= ![if !supportLists]>q&nb= sp;   output contents of file: type <file name>

<= ![if !supportLists]>q    list information about files: <= b>dir <file name> …

<= ![if !supportLists]>n      list information about all (non-hidden) files in the current directory if no file name is given

<= ![if !supportLists]>n     Applications are programs designed to be run= as shell commands

Python applications

<= ![if !supportLists]>n    (this is not in your text)

<= ![if !supportLists]>n    Python programs can be invoked= as applications

<= ![if !supportLists]>q   command line syntax: python = <Python file> <argument> …

<= ![if !supportLists]>n    Arguments are passed to the pr= ogram as a list of strings in sys.argv (variab= le argv of module sys)

<= ![if !supportLists]>q   this often eliminates the need = to prompt for information

<= ![if !supportLists]>q   sys.arg= v[0] is the Python file name from the command line

<= ![if !supportLists]>q   v in argv stands= for vector, another name for a list or array

<= ![if !supportLists]>q   module sys contains a lo= t of other "system" related stuff

<= ![if !supportLists]>n    Python applications, like Pyth= on commands, do not return anything

<= ![if !supportLists]>q   actually, an error code may be returned, but it is usually ignored

<= ![if !supportLists]>n    Example: = tsvToCsvApp.py takes file names as arguments, instead of prompti= ng for them

<= ![if !supportLists]>q&nb= sp;  try the command: python tsvToCsvApp.py address.tsv address.csv

Exercise

<= ![if !supportLists]>n   Write a Py= thon application that prints the sum of its arguments

C:\home\202\src>python sum.py 1 2 3.3

6.3

<= ![if !supportLists]>n   Answer

import sys<= /b>

def main():

    su= m =3D 0

    fo= r arg in sys.argv[1:]:

        sum +=3D float(arg)

    pr= int sum

main()<= /p>

Review: functions

Tuples and returning multiple values

Parameter passing is by-value

<= ![if !supportLists]>n   Assigning parameters

<= ![if !supportLists]>n   Aliasing

 

Using functions for better program structure<= /span>

The End

Some os module functions

<= ![if !supportLists]>n   The os (Operating System) module pro= vides a number of functions for manipulating files on disk

<= ![if !supportLists]>n   os.remove(fil= ename) deletes the file indicated by = the string filename

<= ![if !supportLists]>q   be<= /span> careful with this one!

<= ![if !supportLists]>n   os.rename(oldName, newName) changes the name of file oldName to newName

<= ![if !supportLists]>q   under Windows, if newName file already exists, it must be removed first (not so under Unix)

This week’s success strategy

<= ![if !supportLists]>n    A leading scientific study exa= mined a great many factors to see what most contributed to student success in college. The three leading factors contributing to success were:

<= ![if !supportLists]>q   Time on task: Ok, so this one is obvious. Bu= t it's hard to put in time studying if you're not enjoying it, because you're not succeeding, because you're not studying effectively and taking advantage of= the other factors contributing to success.

<= ![if !supportLists]>q   Studying with other students: <= /span>Many students rate social life = as the most valuable parts of their college experience, but they often fail to ext= end this to their studies. Studying together can be more fu= n, and more productive too. You can really relate to each others learning need= s. And when you help a friend, the material becomes more l= ively in your own mind.

<= ![if !supportLists]>q   Involvement with faculty: Most faculty wish students woul= d seek their help more often. They have a deeper and broader perspective on the material and learning process, and an enthusiasm for the material, which th= ey are eager to share with students. Naturally you learn more, and faculty enj= oy working with you more, if you make a good effort to learn what you reasonab= ly can by reading and attempting exercises on your own. Then please do come wi= th any questions you are stuck on.

<= ![if !supportLists]>n    This and other strategies on t= he course web strategies page!