CS 151: Lab #1

Lab Exercise 1: Getting around a computer

Main course page

The purpose of this lab time is to give you an introduction to the basic tools we will be using this semester. These tools include a terminal, a text editor, and the python interpreter.


  1. Open up the Terminal application, which is a text-based method of using your computer.

    A terminal is simply a text-based interface to the computer. In a terminal, you can type commands, manipulate files, execute programs, and open documents. When working in a terminal, the current directory is called your working directory. A terminal will usually start in the top-level directory of your account. Anything you can do in the Finder, you can do in a terminal (the reverse is not true).

    Directories are separated by a forward slash /. The topmost directory is indicated by a single slash. The total directory tree is all relative to the top level directory.

    Some really important terminal commands are the following. We'll walk through how to use them together in lab.

    • ls - lists the files in the current directory
    • cd <directory> - change directory. If you use the command with no arguments, it changes the directory back to your top-level home directory. The current directory is specified as . and the parent directory of the current working directory is specified using ..
    • mv <from> <to> - move a file, including just renaming it in the current directory. The mv command will remove the file in the old location after copying it to the new location.
    • cp <from> <to> - copy a file to a new location or name
    • rm <filename> - remove the file (will not remove a directory)
    • rm -r <directory> - remove the directory and all the files and subdirectories in it

    Some very important terminal properties are the following.

    • Wildcard characters: the star character * is a wildcard character. If you want to see all the files in a directory that start with A, you can type:

      ls A*

      A possibly bad thing to do is to type rm *

    • Tab completion: when you have typed part of the name of a file or program, hitting the tab key will complete the filename as far as possible while the choice is unique. For example, if you have only one file that starts with the letter b, then typing b and then the tab key will complete the filename.

      If you have two or more files that start with b, tab completion will beep at you. If you hit tab again, then the terminal will show you the options (all the files that start with b).

    • Hitting return - you don't have to be at the end of the line to hit return. If you go back and edit something in a terminal command, you can hit return and execute the whole line no matter where the cursor is located. Try it.

    Some useful (but not as common) terminal commands are the following.

    • pwd - tells you the complete pathname of the current directory
    • less <filename> - scroll through a file
    • cat <filename> - send the file to standard output
    • echo "a string" write the text within the string to standard output
    • touch <filename> touch either updates the modification date on a file or creates an empty file if the named file does not exist. This can be useful in various situations, like when you are learning to create, rename, and delete files using a terminal.
  2. Text editors are the workhorse programs for writing code. You don't want fancy fonts or WYSIWYG layouts, you just want to see lines of text, preferably with syntax highlighting, which means that special words in a language are highlighted to make it easer to read the code. There are many editors to choose from. We'll go over a few in lab together.

    nano/pico - nano (or pico on some systems) is a very simple text editor that works in a terminal. All of the commands use the control key and are listed at the bottom of the screen. To open up a file, just type the name of the editor followed by the name of the file. If the file does not exist nano creates the file.

    nano <filename>

    Open up a file called smart.py and put in the line:

    print 'You are smart'

    Then quit nano and run the file by using:

    python smart.py

    Congratulations, you just ran your first python program.

    Some other editors you should know about include the following:

    • emacs - emacs is the battery-powered swiss-army knife with optional nuclear reactor of the editor world (emacs humor).

      If you open emacs, the opening screen tells you how to access a tutorial. The most important thing to know is how to save and get out. To save, hold the control key down and type x then s (C-x C-s). It will ask you for a filename (and give you a default if you opened a file) and then save it. To exit, hold down the control key and type x then c (C-x C-c).

    • BBEdit - BBEdit is a very nice GUI editor designed for writing code and html (web pages). Colby has a set of licenses for it, and it is installed on the lab computers and the computers on Mudd 4th.
    • JEdit - JEdit is very simlar to BBEdit, except that you can download it for free and run it on any operating system. Many students like to use it instead of BBEdit since they can use it on their own computer.
    • XCode - XCode is a powerful development environment for Macs, but you can also just use it for its editor, which is pretty decent.
  3. Colby Wiki - a Wiki is a convenient way to put content up on the web quickly and easily. We'll watch a video on how to create your personal space and then go through the process of creating a new page in your space.

    You can edit any page in your space by going to the Edit tab. There you will find one or two options for editing your page. The Rich-text option (available when using FireFox) lets you do the normal select and point and click type formatting. The wiki markup lets you use the wiki markup language to make your page.

    To make a new page that links from your home page, edit your home page and make a wiki link by putting the name of the new page in square brackets, like:

    [Project 1]

    After saving your page, the link should show up as red text. Clicking on the red text creates the new page and takes you to the editor for it. You can always choose to remove a page in the edit tab using the link on the upper right on the window.

  4. Python - a simple yet powerful interpreted language for making computers do stuff

    You have already created and run a very simply python program. You can startup the python interpreter into interactive mode by typing python in the terminal.

    Many people have written modules for python that do some sophisticated things. One of those modules is called turtle and implements turtle graphics (e.g. forward, left, right, pen down, and pen up). To import the turtle graphics module, just type:

    from turtle import *

    Then call the function reset(), which should bring up a window with a turtle in the middle (ok, it's just an arrow, but pretend).

    We can make the turtle move forwards, backwards, turn left, turn right, and pick up or put down the pen using the following commands.

    • forward(x) - move forward x pixels
    • backward(x) - move backward x pixels
    • left(a) - turn left a degrees
    • right(a) - turn right a degrees
    • up() - pick up the pen (don't draw when the turtle moves)
    • down() - put the pen down (draw when the turtle moves)

    More complete documentation is available on the python documentation site.

    Create a hexagon

  5. As you continue on, you may want to store a sequence of commands in a file. Then you can execute the sequence of commands in the file either by typing:

    python <filename>

    or by typing

    import <filename>

    while you are in the python interpreter.

    Create a file with a .py ending and write some turtle commands into the file that make a shape. On the last line, put the instruction:

    q = raw_input()

    That will keep the turtle window open until you hit return in the terminal. When you are finished, save a copy of your python file to your personal directory on filer.

When you are done with the lab exercises, you probably want to find a partner for the first assignment.