CS 1004 Aug 12 2014
We learn by doing
Aristotle
1 Getting Ready For Final Exam
Several students have asked about an existing function in the random module that allows you to shuffle a list.
>>> list = range(10) >>> list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> import random >>> random.shuffle(list) >>> list [7, 6, 5, 2, 1, 4, 9, 8, 3, 0]
This might be helpful when working with randomized trials, for example.
1.1 List Operations
Yesterday we saw the basic operations for sequences. Lists go further by adding operations to manipulate a list.
list.append(value) – Append a value to become final element in list
list.sort() – Sort list
list.reverse() – Sort list in reverse order
list.index(value) – locate index of first occurence of value in list
list.count(value) – Return number of times value appears in list
list.remove(value) – Delete first occurence of value in list
These same operations apply to lists, because lists can be treated as a sequence. Lists are special, however, and have additional operations, which we will summarize tomorrow.
1.2 Skills Review Beginning
1.2.1 Python Programming Syntax
PS-1. Assign value to variable
PS-2. Know that in Python the type of the value determines the type of the variable
PS-3. Know how to insert comments into code fragments and modules
PS-4. Understand syntax errors
PS-5. Understand concept of NameError
Trying to access a variable that doesn’t exist.
>>> x Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> x NameError: name ’x’ is not definedPS-6. Understand concept of TypeError
Trying to carry out wrong action on a value.
>>> x=4 >>> len(x) Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> len(x) TypeError: object of type ’int’ has no len()PS-7. Understand concept of IndentationError
Indentation identifies block statements.
def someFunction(): """ This function has Bad indentation """ return -1
IndentationError: unindent does not match any outer indentation levelPS-8. Understand context in which variables are defined
PS-9. Understand how indentation identifies body of statements
PS-10. Understand nested indentation
PS-11. Understand operator precedence and use of parentheses for clarity
1.2.2 Python Functions
PF-1. Know how to define a function
PF-2. Know how to define a function with parameters
PF-3. Know how to use return statement within a function
PF-4. Understand concept of AttributeError
Invoking a non-existent function on a value:
>>> list [1, 2, 3, 4] >>> list.shuffle() Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> list.shuffle() AttributeError: ’list’ object has no attribute ’shuffle’PF-5. Know how (and why) to compose functions f(g(x))
When you want to compute a function g(x) and pass the returned value as an argument to another function f(...)
PF-6. Know how (and why) to chain functions f(x).g(y)
When function f(x) returns a value on which you want to invoke one of its defined operations g(...) with a parameter y. We didn’t fully exercise this skill this term.
PF-7. Know how to document a function (""" or ”’)
1.3 Question 1 sample final exam questions
Question 1 on the final will give you a chance to demonstrate your Python skills. Specifically, in small "sound bites" you will identify the value of a given expression. I find these very useful in quickly targeting the skills I assume you know.
Starting with the following variable definitions
>>> values = [1, 10, 9, 13, 2, 5] >>> x = 3 >>> y = 5 >>> s = ’trial and error’
What is the value of these expressions? You can try them out by hand and verify them using IDLE or the Canopy Editor.
values.index(x,y)
values[y-x] + values[y] - values[x]
len(s.split())
s[x]+s[y]+s[x+y]
s[:y]*x
[x]*y
Now that we have used a number of functions that operate directly on lists, I ask slightly different questions (as you can see by reviewing the sample final exam from last year in the class Study Guide).
After invoking values.insert(x,y), what is the value of values?
After invoking y -= x + y, what is the value of y?
After invoking values.remove(2), what is the value of values?
1.4 Question 2 sample final exam questions
If we get here, this question is a programming question that you are to complete. The following question is just a bit more challenging than what I would have on an exam:
Write a function onlyDuplicates that takes a list of values and returns a new list that only contains those elements which appear more than once in the original list
In [1]: myList = [1, 3, 2, 1, 4, 5, 2] In [2]: onlyDuplicates(myList) Out[2]: [1, 2]
Note: there are two approaches you could follow to solve this function. We’ll discuss (at least) these two tomorrow.
1.5 Working with data from Forecast API
Download and retrieve the following Zip file and unpack its contents. It contains a main Python file "day22.py" which invokes functions using a third party API which I’ve provided for use with the forecast.io service. The term "third party" means that this code was written by someone else and you only have to use it. It also assumes that you don’t even need to look at the source code, but of course it is there all the same in case you want to investigate further.
The third party code is found in the "forecastio" folder. Note that you must keep this folder and all of its contents unchanged. This allows the API calls to work as programmed.
Once you have downloaded and unpacked the files, open "day22.py" within Canopy and "run the module" to make the currentDailySummary function available. This function will return a string reflecting the current daily forecast for the given latitude and longitude coordinates. Give it a try using the wlat and wlong values provided for you which reflect the coordinates of Worcester.
Once loaded you should see the following:
In [1]: currentDailySummary(wlat, wlong) Out[1]: u’High=83.28 Low=60.14 Light rain today through Sunday, with temperatures falling to 71\xb0F on Friday.’
Again, retrieve the file at day22.zip file.
1.6 Version : 2014/08/21
(c) 2014, George Heineman