Lab 1 - Getting Started with Python (Jan 17, 2014)

In this course, we will be using the programming language Python 2. The current version (as of Dec 2013) is Python 2.7.6, which you can freely install on your personal computer. (NOTE: Python 3, which was the Python of the future 5 years ago, is still the Python of the future, which means you shouldn't use it yet. The syntax is a bit different from Python 2, so most code in Python 2 will not work in Python 3 without some tweaking, and vice versa.)

The first lab session is designed to get you acquainted with Python 2 and some basic programming. Python 2 is already installed on the lab machines (both in PHSC and Dale Hall), which run some version of Windows. There are different ways of interfacing with Python---the default way on Windows is to use Python's interpreter/editor called IDLE, which is what we will do in the lab. The default way on Mac/Un*x, where Python should already be installed (though your version may need to be updated) is from the command line (either running the interpreter or executing scripts). For assignments, you may interface with Python however you like.

Note: Python is sensitive to indentation when working with a block of multiple lines of code. Since the IDLE interpreter does automatic indentation, you may have difficulties doing a direct copy-paste into the IDLE interpreter window, even though copy-paste into the IDLE program window (or command line interpreter) may work. One issue is whether spaces are properly preserved in the source you copy from. (However, don't use copy-paste for this assignment, or you will learn nothing.)

Task 1: Go through One Day of IDLE Toying to get familiar with IDLE. (At the part where he tells you to start playing around, you can try a couple of examples from here, but don't spend more than 2 minutes on this.) Note: the comma at the end of the code print i, in this tutorial tells Python not to print a carriage return (newline), so everything will be printed on the same line. If you instead use the code print i without the comma, all the numbers will appear on different lines.

Task 2: Go through the Instant Hacking tutorial to get familiar with the basics of programming and Python 2. The last section (More Abstraction...) is not too important for us, and you can skip it if you like. The author of this page says not to type directly into the interpreter, but it looks to me like everything works fine. You may either work directly in the Interpreter window or in the Program window, whichever you prefer. The tutorial is not designed for interpreter mode, but I think you can use interpreter mode for almost, if not, everything. Note: when defining functions, include an empty line (hit enter/return again) to finish the function definition. Also, don't waste minutes of lab time calling the sleep function, as appealing as that might be.

You may not finish this during the lab period, but you are expected to finish this on your own before next Friday. Also it will help for the...

Lab Homework 1 (due Fri Jan 24): This assignment is meant to get us familiar with lists and some basic linear algebra in Python. (Clarification: for each exercise, use loops, e.g. a for or while loop, to write your functions. E.g., don't just use range(1,n+1) for the first exercise.)

  1. Write a function called "count_vec(n)" that returns (not prints) a vector (list of numbers) [1, 2, 3, ..., n]. Test this for n=0, 1, 5, 10.
  2. Write a function called "scale_vec(v,c)" that takes in a vector (list of numbers) v and number c returns the vector c times v (i.e., each element of the list should be multiplied by c). Test this for v=count_vec(5) and the values c=-1, 0, 0.5, 1, 2, 10.
  3. Write a function called "shift_vec(v,c)" that takes in a vector (list of numbers) v and number c returns the vector v+c (i.e., each element of the list should be added to by c). Test this for v=count_vec(5) and the values c=-1, 0, 0.5, 1, 2, 10.
  4. Write a function called "count_mat(n)" that returns an n by n matrix whose coefficients are the integers from 1 to n^2, in order. Here we represent a matrix as a list of n row vectors. For example count_mat(2) should return the list [[1, 2], [3, 4]], which we view as the 2x2 matrix with top row [1 2] and bottom row [3 4]. Test this for n=0, 1, 2, 5.

Further references: For various assignments, including possibly the above one, you may need to learn a little more Python on your own. The Official Python Tutorial is a good place to learn about the basics in more detail, though this assumes you are familiar with some programming terms and concepts. The Beginner's Guide has more references, including lists of Python intros for those with programming experience and those without (this is how I found the tutorial above). In particular, this WikiBook Tutorial for Non-programmers seems like a good tutorial, which is considerably more detailed than the one in Task 2. Last, when all else fails, you can just do a web search for "How do I do XXX in Python?"



Labs Page Course Home