# 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:



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.4 Use python's built-in help function to find out what operations are available on data¶

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.7 How many total times does the character "s" appear in the names?¶

Do this in a way that is not case sensitive,

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

## 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.2 Append the fourth planet to the end of the list¶

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

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

## 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 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?