Homework 1 (due March 6)

Finish this homework in the appropriate cells, and copy this file to your home directory on ursa, so I can grab them and inspect them. Please keep the original filename.

Q1.

Create a new file called "hello.py" with just one python statement that prints "hello" to the screen. Test that it works with the command

    python hello.py

Now we want to promote this script to become a true unix command, so we want to just type in a unix terminal

      hello.py

and get the same "hello" printed to the screen.

Try this out, but what errors can you think of (and maybe you got) when you worked this out, and explain your results.

Hint: I was able to get 4 types of errors at different stages of getting this to work.

A1.

Q2.

Assign an integer value to n. Write a snippet of code that computes the sum of all even numbers up to and including n. Test this for a few small values of n, even and odd. Print out the sum for n=999 and n=1000. What happened if you set n to be a negative number.

You can use any method you like: Functions, For loops., Numpy arrays, Python lists, just to name a few.


In [ ]:
# A2.

Q3.

Why does python not have the switch/case control flow statement like one has in C and Java?

If you use an online resource, give a reference.

Q4.

For the following experiment we want to plot the performance difference between adding up the elements in a list vs. numpy array, a function of the size of the list/array. Much like in the 03-arrays time your list and numpy cell for a number of values of n (you may need to go up to n=1,000,000 or even higher).

Put your numbers in a hardcoded list in A3c. You will need to rerun the A3a and A3b cells a number of times.

Plot your results in a log-log plot. What is a nicer plot, N vs. CPU or N vs. CPU/N?


In [ ]:
# A3a.  time the python list

In [ ]:
# A3b.  time the numpy array

In [ ]:
# A3c.  plot the results