1 Getting Ready For Final Exam
1.1 List Operations
1.2 Skills Review Beginning
1.2.1 Python Programming Syntax
1.2.2 Python Functions
1.3 Question 1 sample final exam questions
1.4 Question 2 sample final exam questions
1.5 Working with data from Forecast API
1.6 Version : 2014/ 08/ 21

CS 1004 Aug 12 2014

Lecture Path: 22
Back Next



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.

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
1.2.2 Python Functions

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.

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).

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