Massachusetts Institute of Technology
Dept. of Electrical Engineering and Computer Science
Spring Semester, 2006
6.099: Introduction to EECS 1

A brief tour of the 6.099 software

If you have not already installed the 6.099 software on your computer, please do so now by following the software installation instructions.

You'll be writing two kinds of Python programs this semester:

A first look at PyRo

Spend a few minutes viewing an introductory Flash 6-minute video that the PyRo folks have produced, called "Getting Started with Pyro", which you can find at the Pyro Tutorial Movies Web page. You might also want to watch the second, 11-minute, video, called "Using the command line of Pyro's graphical user interface," but you might want to just go ahead now and come back to the second video later.

Now start pyrobot on your machine and duplicate what you saw in the first video, running the avoid.py brain. Rather than exiting Pyro when you finish duplicating the demo in the video, type

dir(robot)
into the line marked Command: at the bottom of the screen and press enter. Robot here is a Pyro object, and Pyro's dir operation returns a list of the methods understood by an object. We'll learn about objects and methods in a couple of weeks.

Try some of the robot methods to see what they do. For example

robot.move(1, 0.5)
turns on the motors so that the robot moves forward at speed 1 and rotates at a rate of 0.5. (We'll worry about the units later.) With the motors turning like this, the simulated robot will eventually jam up against a wall or an obstacle, and the motors will keep turning, which you can verify by dragging the robot with the mouse and letting it go. To stop the robot, do
robot.move(0,0)
Also try
robot.range.distances()
which shows a list of the distances registered by the eight sonars.

You can experiment with more of the robot methods now, or you can view the second Pyro video that explains some of these. Or you can just wait for the first lab.

A first look at Emacs

Quit out of Pyro and start Emacs. Emacs is a sophisticated system with hundreds of commands, and it can intimidating at first. But taking fifteen minutes or so to experiment with it now will pay off during the semester and in later programming subjects.

If you've never used Emacs before, run the Emacs tutorial, by typing the esc key, then pressing ?, then pressing the letter t. Work through the tutorial, through the section on files. You may want to just skim this now and come back to it later, but at least get a sense for what's there.

Something not mentioned in the tutorial's "basic cursor control" section is that you can move the cursor by using the left, right, up down arrow keys, just as you might expect, as an alternative to the C-b, C-f, C-p, C-n keys described in the tutorial. You can also move the cursor to a new position by clicking with the mouse. You can also mark a section of text by dragging over it with the mouse, rather than using the marking commands described in the tutorial section on inserting and deleting. On Windows or the Mac, pressing delete will delete the marked region. On GNU/Linux, use C-w to delete the region as described in the tutorial.

A first look at Python

Start Emacs (if it's not already running) and open a file using the C-X C-F command. Give the file a name with extension .py, for example mytest.py. This should give you a blank window. The bar at the bottom should say (Python) to indicate that you are in Python mode.

What you should do when entering Python mode is to type C-c ! to start the python interpreter. This will split the window into two buffers, with your program on top and the Python interpreter on the bottom. Note that your blinking cursor will be in the Python interpreter window. Use C-x o to return to the buffer with your program, and then type C-c C-c after typing some Python code.. This last Emacs command will execute the buffer with your program, effectively defining the procedure myProc.

Define a procedure that takes numeric input x and returns the product of x and x+1 and x+2:

def myProc(x):
    return x*(x+1)*(x+2)
Notice that when you go to the second line (by typing enter) the line automatically indents to the correct position for Python code. You should also notice that when you type the two close parentheses, the cursor briefly flashes to the matching open parenthesis. These are some of the ways that Emacs assists you in writing Python code.

Now type C-c C-c (i.e., type control-C twice). This will evaluate your program and split the window into two buffers, with your program on top and the Python interpreter on the bottom. In the interpreter buffer, you ought to be able to evaluate

>>> myProc(100)
and see the result. You can also go back to the mytest.py buffer (by clicking with the mouse or typing C-X o, edit your procedure so that is does something else, or define a new procedure, and run your code again.

That's the basic operation of working with Emacs and Python.

Summary of Emacs Python mode

Here are the basic Emacs commands for working with Python. You can get by with just the first two of them, but the others may prove useful:
key             binding
---             -------
C-c C-c		execute buffer
C-c !		go to the Python shell

C-c |		execute region
ESC C-x		execute def or class

C-c C-n		move to next statement
C-c C-p		move to previous statement
ESC C-e		move to end of def or class
ESC C-a		move to beginning of def or class

ESC C-h		mark def or class as region
C-c #		comment region
C-U C-c #	uncomment region

C-c ?		show help for Python mode
There are also a bunch more Emacs Python commands, which you can read about by typing C-c ?.

Notes:

If you type C-c C-c and do not have the python interpreter running, in a window, confusing things happen.

Notice that when you start the second line of your procedure definition (by typing enter) the line automatically indents to the correct position for Python code. (If it does not, perhaps you forgot the colon at the end of "def myProc(x):"). You should also notice that when you type the two close parentheses, the cursor briefly flashes to the matching open parenthesis. These are some of the ways that Emacs assists you in writing Python code.