1 Final Third Of Course
1.1 Exam2 Review
1.2 Moving Onwards
1.3 Random Numbers
1.4 Multiple Assignment Operator
1.5 Python Functions Returning Multiple Values
1.6 Analyze real estate data and crime statistics
1.7 Version : 2014/ 08/ 06

CS 1004 Aug 05 2014

Lecture Path: 18
Back Next



ABC. A, Always, B, Be, C, Closing. Always be closing.
Glengarry Glen Ross

1 Final Third Of Course

1.1 Exam2 Review

Here are the question-by-question averages: (To be computed)

Once I complete the grading, I will fill in this section.

1.2 Moving Onwards

We are now ready to tackle the final issues remaining in your journey towards becoming a programmer.

The first part of the course focused on introducing the Python language. You learned about variables and basic python statements. You wrote functions that acted as whole programs. You learned how to read input from the user from the keyboard and print output to the console window. You learned about loops and lists. You saw logical conditions and conversions of values from ints, to floats, to strings.

In the second part of the course, I introduced the fundamental concept of writing functions that returned values, thus allowing you to write functions that participate in a computation. You then worked with functions that had parameters. Each parameter acts as a variable in the function which is initialized to the values passed in by argument. You learned how to process data found in a file.

From my perspective, there were two basic challenges in the second part of the course:

As we turn the corner to enter the third part of the course, we are going to work consistently on showing how to decompose problems into smaller, more manageable tasks which can be solved by functions that are composed together to solve the original problem.

The next set of lectures will demonstrate the case study that I had mentioned earlier (Day15) In many ways, until you follow, step by step, the design of a program, you won’t be able to do it for yourself when faced with a blank page.

Once the case study has been discussed in detail, the next three lectures will be used to review and get more experience in tackling problems, sight unseen.

But first we have two skills to cover.

1.3 Random Numbers

One of the earliest uses of computing devices was to simulate random occurrences to statistically measure complicated behaviors and models.

Of course, these numbers are not truly random, but are rather pseudo-random numbers. For most practical purposes, you can generate on demand two kinds of random numbers:

>>> import random >>> random.random() 0.24180835650788357 >>> random.randint(10,20) 15

Once you import the random module, you have access to these two functions.

You need to pay attention to random.random() which generates a random number in the range of [0, 1). That is, while 0 might be generated, 1 will never be generated.

What can you use these values for? Well, let’s say you wanted to simulate an unbiased flip of a coin. As you know each side of a coin has a 50% chance of occurring.

The following program tries 1000000 trials of a random coin flip.

import random def simulateCoinFlips(trial): """Return number of heads in toss of unbiased coin""" heads = 0 while trial > 0: # 0.0 through .499999... is heads # 0.5 through .999999... is tails if random.random() < .5: heads = heads + 1 trial = trial - 1 return heads def isFair(): """Simulate toss of a fair coin""" trials = input ("How many trials to run ") heads = simulateCoinFlips(trials) print (str(heads) + " in " + str(trials)) print (str(100.0*heads/trials) + " percent")

Is the random coin fair? You can read further.

1.4 Multiple Assignment Operator

Sometimes you might find yourself wanting to set two variables to the same initial value:

>>> a = 0 >>> b = 0

In Python (and many other programming languages), this can be shortened as follows:

>>> a = b = 0

But this is only part of the picture. What if you wanted to set two separate values to these variables: >>> a = 1 >>> b = 2

In Python (but not in other languages), you could say:

>>> a,b = 1,2

Thus you are simultaneously setting a to the value of 1, and b to the value of 2.

Now, if you ever find yourself reading lots of Python programs, you might come across this gem of a statement:

>>> x = 10 >>> y = 20 >>> x,y = y,x

After the above statements, what is the value of x and y? It turns out that in Python, the above is the recommended way to swap two variable values.

>>> print (str(x) + " " + str(y)) 20 10

That’s right, the above statement is the preferred Python way to swap variable values.

Other languages would do something like the following:

>>> x = 10 >>> y = 20 >>> >>> tmp = x >>> x = y >>> y = tmp

Thus the way to swap variable values is to introduce an intermediate temporary variable tmp used to hold onto the value of x just before x is assigned the value of y.

1.5 Python Functions Returning Multiple Values

Python functions can return multiple values without using lists. This new concept looks similar to the above multiple assignment operator. Consider the following function:

def randomPoint(): """Return a tuple representing a random point in unit square""" x = random.random() y = random.random() return (x,y)

This function computes two random values in the range [0,1) and returns a tuple containing these two values. The first value is considered to be the X coordinate, and the second the Y coordinate.

How would you invoke this function?

def main(): """demonstrate tuples in action""" pt = randomPoint() print (pt) xCoord = pt[0] yCoord = pt[1] print ("X = " + str(xCoord))

A tuple, then, is a convenient way to return multiple values from a function.

1.6 Analyze real estate data and crime statistics

Over the next few lectures I will develop a case study to analyze data provided to you in CSV files.

I am providing a number of videos to more readily describe the case study and how it is developed. Please review these videos:

HW6 preparation: video part 1

HW6 preparation: video part 2

HW6 preparation: video part 3

Download the following PreHW6.zip file which contains all necessary files to demonstrate the behavior as shown in these videos.

1.7 Version : 2014/08/06

(c) 2014, George Heineman