CS 151: Lab #1

Lab Exercise 1: Getting around a computer

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.

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

    • pwd - tells you the complete pathname of the current directory
    • 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
    • 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.
    • Piping output: you can send anything sent to the standard output by a program into another program by using the vertical slash character |

      echo "print 'you are a smart student'" | python

    • Directing output: you can send stuff to a file by using the greater than symbol >

      echo "print 'you are a smarter student'" > smart.py

    • 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.
  2. 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.

    nano <filename>

    Open up the file smart.py and add a second print statement. Then quit nano and run the file by using:

    cat smart.py | python

  3. 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).

  4. BBEdit - BBEdit is a very nice GUI editor designed for writing code and html (web pages). Colby has a license for it.
  5. HTML: Hyper Text Markup Language

    HTML is a language for specifying how text should be displayed in a browser. An html file is just a text file, and you can use any of the above editors to create one. The file just needs to have the .htm suffix on it to be recognized as a web page.

    There are many resources on the web for creating html pages. For a simple page with headers, text and a few images you don't have to learn much. The following creates a simple web page.

    echo '<html><head><title>My page</title><body>CS 151 report page</body></html>' > page.htm

    Things to note:

    • HTML tags are just things enclosed in angle brackets: <tag>
    • Most tags require a starting and an ending tag. The only difference between them is the ending tag starts with a backslash.
    • The basic HTML file has two parts: a header and a body. The <head> and <body> tags delineate the two parts of the file. The whole file is bracketed by the <html> tag.
    • Any text in an html file is just text

    You can tell the browser to open the page by using the open command in the terminal.

    open page.htm

    Because the file has the htm suffix, the open command tells your default browser to open the file.

  6. Building a simple web page: start by opening the file page.htm in a text editor. Before starting anything else, make the file look nice by adding some carriage returns. Readability is essential for any project on a computer.

    • Modify the page title, which appears in the browser window
    • Add a header using the <h1> tag
    • Add a paragraph using the <p> tag
    • Change the background color using a property of the <body> tag
    • Create a bulleted list using the <ul> and <li> tags
    • Separate parts of the document using a <br> tag
    • Link to another document using the <a> tag and the href property
    • Link to an image using the <img> tag and the src property

  7. For a list of all html tags and their usage you can use a site like this one. If you put HTML tags into Google, you'll find lots of others.

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

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