Homework: Introduction to Python

Due: Thursday, Sept. 21

Your assignment should be handed in an ipython notebook checked into your github repository. To download this assignment, your best option is to clone the original github repository for the course website:

git clone https://github.com/rabernat/research_computing.git

You will find the assignment in content/Assignments/ directory, along with the two data files.

If you really don't want to clone the repo, you can download it directly. You will also need to download the two data files.

Read the official Python Tutorial sections 1-5. This excellent, well written tutorial introduces all of the most important concepts of the core python language and its standard library. Even if you don't read it, you should know where to find it; if you get stuck, it should be your first place to turn for help.

Constraints

  • This assignment should be done in basic python: don't import any extra modules unless specifically instructed to do so.
  • You should work on this assignment alone.
  • Try to do the problems yourself. Refer to the lecture notes and the Python Tutorial if you get stuck. Avoid random googling and stack-overflowing. A perfect solution is less important than the process of figuring it out yourself.

1. Dictionaries and Strings

In this section we will explore a dictionary whose keys and values are strings.

Cut and paste the following code to read a data file. (You are not expected to understand exactly what is happening here...it's just a quick way for us to get some data into python.)

import pickle
with open('class_names.pkl', 'rb') as file:
    data = pickle.load(file)

This will load some pre-generated data into the variable data.

1.1 Examine the data by allowing the notebook to print it

(Just enter data on the last line of the cell and execute it.)

1.2 Comare to the output of print(data)

Which one is prettier?

1.3 Determine what type of object is data

1.4 Use python's built-in help function to find out what operations are available on data

(What is the meaning of the methods that start with __?)

By now it should be clear that data is a dict (i.e. "dictionary") that maps UNIs to names for all students in the class.

For the remaining questions, the point is to have python tell you the answer. (Obviously you could just manually count / sort the data, but that defeats the purpose!)

1.5 How many entries are there in data

1.6 Is your UNI in data?

1.7 How many total times does the character "s" appear in the names?

Do this in a way that is not case sensitive,

1.7 Create a new dictionary that contains the same keys but only the first names as values

1.8 What is the longest first name?

1.9 Check that the first letter of each UNI matches the first letter of each first name

(Be careful about the case.)

2 Lists and Numbers

In this section, we will play with a list of numbers.

Keep in mind that doing lots of numerical calculations on lists of numbers (using just core python) is somewhat awkward and inefficient. Numpy makes this sort of work much easier; in order to appreciate numpy, however, it is useful to first do things the "hard way".

Run this chunk of code to load some data.

with open('numbers.pkl', 'rb') as file:
    numbers = pickle.load(file)

2.1 Confirm that numbers is indeed a list

2.2 How many items are in numbers?

2.3 What is the difference between the first and last values?

2.4 What are thre first five and last five items?

2.5 What is the type of the first item? Confirm that all items have the same type.

2.6 What are the minimum and maximum values in numbers?

2.7 What is the mean value?

2.8 What is the standard deviation?

2.9 What is the largest (most positive) and smallest (most negative) change from one value to the next

Bonus: can you guess the source of these numbers?

3 Lists vs. Tuples

The difference between lists and tuples is a persistent source of confusion about python. The key difference is that lists are mutable while tuples are immutable. This short exercise is designed to help you understand what this means.

3.1 Create a list of the first three planets in the solar system

3.2 Append the fourth planet to the end of the list

(Print the list so you can verify its contents after every modification)

3.3 Venus has exploded. Remove it from the list

3.4 Convert the last item of your list to upper case

3.5 Create a tuple of the first three planets in the solar system

3.6 Try to append or remove items from the tuple

Go ahead, try it! I dare you!

3.7 Create a new tuple by concatenating a second tuple to your original tuple

(No loops needed). It should work. Check your original tuple. Did it change?

4 Standard Library: datetime

Basic python comes with a powerful "standard library" of modules which help you do useful things like interact with the operating system, perform complex pattern matching on strings using regular expressions, and open network connections. This standard library is too big for us to learn every module: the point here is just to be aware that it exists.

In this exercise we will also explore a simple but powerful module from python's standard library: the datetime module.

4.1 Import the datetime module

4.2 Read the built-in help on datetime.date and datetime.timedelta

These are the two functions we will be using from the datetime module.

4.3 Create a datetime.date object for your birthday

4.4 What day of the week was your birthday on?

4.5 Find out how many days have elapsed since your birthday?

5 Functions

Learning to write our own functions is an important step towards writing more sophisticated programs. Here will will practice writing functions.

As an example, we will consider the harmonic series, defined as

$$ \sum^N_{n=1}\frac{1}{n} $$

This series diverges logarithmically.

5.1 Write a function to compute the value of the harmonic series up to N terms

The function shold have one argument: N. It should return the series sum.

5.2 What is the value of the harmonic series after 1000 and 1000000 iterations

The alternating harmonic series:

$$ \sum^N_{n=1}\frac{(-1)^{n+1}}{n} $$

does converge.

5.3 Write a function to compute the value of the alternating harmonic series up to N terms

5.2 What is the value of the alternating harmonic series after 1000 and 1000000 iterations

5.3 Numerical convergence test

Now for something more complicated: we will write a function that tests whether another function converges. As its first argument, this function should accept another function. It should also have optional keyword arguments that specify

  • the numerical tolerance (how close is close enough to consider a series converged?)
  • the number of iterations (we can't iterate an infinite number of times, we just have to pick a "large number")

This function should print a statement telling what it found. If the test function converges, print the value it converges to.

(Hint: the alernating harmonic series oscillates strongly around the asymptotic value. So checking neighboring points may not be the best way to test for convergence. Can you think of a more robust method?)

5.3 Use your convergence test function see that the harmonic series diverges and the alternating harmonic series converges?

Explore the sensitivity to the two optional parameters. What are the tradeoffs between tolerance and number of iterations?