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.

Labs and projects are tightly coupled. Each project is initiated with a set of lab exercises. In the projects, you will write code that expands on the code written in lab. For each project, you will be writing Python code and running it. The programs typically create images. You will be posting some of these images, along with a description of the project, on a Wiki page. You will then turn in your code by putting it in a special directory on one of the campus computer servers. Today's lab will allow you to practice each of these tasks.


Introduction to the Terminal

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. Anything you can do in the Finder, you can do in a terminal (the reverse is not true).

When working in a terminal, the current directory is called your working directory. A terminal will usually start in the home directory of your account.

In a terminal, we often need to specify a file or directory on the hard drive. The complete description of where a directory or file on a computer is located is called its path.

Directories in a path are separated by a forward slash /. The topmost directory on the filesystem is indicated by a single slash. The directory tree for your computer is all relative to the top level directory. In other words, starting with a /, you can type the complete path to any file on your computer. For example, the the path to Caitrin's home directory is:


Most of the time, however, you will be specifying paths relative to your current directory. Any path that starts without a slash is relative to your current working directory. Note that paths are case-sensitive, so be sure to use proper capitalization.

You will use paths, and filenames to change directories, list the contents of directories, and run Python programs.

Basic Terminal Commands

Below are some important Terminal commands. We'll walk through how to use them together in lab.

Useful Features

Some useful terminal properties are the following.

Terminal Commands, Continued

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

Introduction to the file servers

A fileserver is a central file system you can access from any computer. It's like having a virtual USB key you can plug into a computer to store your files. Colby maintains two different fileservers we use in this course. One of these (the Personal server) is for you to store the code you are working on. The other (Courses) is for you to turn in code. We will give instructions regarding the Courses directory in the project instructions. For now, let's focus on where you should keep code that is work in progress.

There are two ways to mount the file server.

This system is backed up regularly, and you can access it from any computer on the Colby network. We strongly suggest you store all of your work for this course into your personal directory. You can, in fact, work directly from your personal directory. Let's practice doing that for the first project.

Project Prep

  1. Mount your personal directory.
  2. In the terminal, navigate to it. Type cd followed by the path to the directory. E.g. for Caitrin, it may look like this:

    cd /Volumes/personal/ceeaton

    Or, use a shortcut. If you type cd in the terminal, then drag the icon for your directory onto the terminal, then the path to it will automatically be pasted.

  3. Make a directory (in your personal folder) named project1. You can do this using the Finder, or you can type mkdir project1 into the terminal.

Introduction to Text Editing

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 easier to read the code. There are many editors to choose from.

In lab, I will be using TextWrangler or the free version of BBEdit, which is fundamentally the same. It is a free text editor for the Mac and has all the features we need for writing programs. If you would like to install it on your personal computer, go to the TextWrangler web site and follow their instructions.

Project Prep

  1. Open TextWrangler
  2. We need to set a couple of preferences.
    1. We need to set it up to display line numbers. This will help later, when you encounter error messages that tell you where an error is by supplying the file's name and the line's number.
    2. We need to tell it to "Auto-expand tabs". Python doesn't care whether you use tabs or spaces. But just by looking at code, it is hard for you to tell whether you used tabs or spaces. So, let's tell the text editor to insert 4 spaces (instead of a tab) when you press the tab button.
  3. Create a new file (Cmd-n). Save it as smart.py in your project1 directory.
  4. Put in the line:

    print('You are smart')

  5. Then save the file (Cmd-s).
  6. Now, you can instruct Python to run the program. In the terminal type

    python3 smart.py

    Congratulations, you just ran your first Python program. (Python humor)

    Note that it works only if the current working directory is the one that contains the file smart.py. So, one thing you need to do when working on a project is to ensure that your Terminal is in the right directory. Think of it as making sure TextWrangler and Terminal agree about where the programs are. Note that, in general, you run Python programs by typing

    python3 <filename>

For your information

Here are some other text editors you should know about:

Introduction to the Python turtle

Python is a simple yet powerful interpreted language for making computers do stuff. 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).

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

More complete documentation is available on the Python documentation site. Let's write our first turtle program.

Project Prep

  1. In TextWrangler, create a new file, and save it as simple_turtle.py in the project1 folder in your Personal directory.
  2. Type your name, a date, and the course and project in a comment at the top of the file. Every Python file you ever write should start with these three lines.


    A comment in Python is any text preceded by a hash #. Comments are not executed, so they do not have to contain Python-approved syntax. This is a great way to annotate your code with useful information in a human-friendly format, like sentences. It is strongly recommended that you use comments throughout your code to explain what it is meant to do and why.

  4. On the next line below your comments, put the instruction:

    from turtle import *

    That will import all of the turtle commands. In other words, it will give you the ability to direct the turtle to draw for you.

  5. On the last line, put the instruction:

    input('Press Enter to continue')

    That will keep the turtle window open until you press return in the terminal. When you are finished, save a copy of your Python file to your directory on the Personal server.

  6. Save the file in your project1 directory. Call it simple_turtle.py because it is your first turtle program. Notice that the filename ends in with the .py extension - that is meant to indicate the file contains Python code.
  7. Now, let's make the program do something. Using the right() and forward() turtle functions (commands), let's draw a hexagon. We place one command on each line, and put them in between our import and input() commands. I don't want to ruin the fun of figuring out how to draw a hexagon, so let me give you an example of how to draw a square:
    forward( 100 )
    right( 90 )
    forward( 100 )
    right( 90 )
    forward( 100 )
    right( 90 )
    forward( 100 )
    right( 90 )
  8. Save!
  9. Run the program from the terminal:

    python3 simple_turtle.py
  10. What does the program do? What happens if you change the values passed to forward() or right()?

    Remember, you have to save the file before you run it again on the Terminal.

Introduction to the Wiki

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.

Tutorial Videos

Log in to the wiki, but wait until Caitrin says "Your turn" before clicking on the "Create your personal space" link. The system can't handle more than one space-creation at a time.

For each project, you will create a new page in your wiki 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.

Project Prep

  1. Make a page for Project 1. Go to the Add menu in the upper right corner, and select the Page option. Replace the "New Page" title with something meaningful, like "Caitrin's CS151 Project 1" so that the page content is clear from the title.
  2. Now save your page. Even though it has no content, let's save it -- it is a good idea to save often. After saving your page, it will show up in the list of child pages on the bottom of your home page. You can link to it from your home page by editing the home page, pressing the link button (it looks like a chain) and searching your space for it. But it is also perfectly acceptable to simply leave it as a child page.
  3. In all of your project wiki pages, you will need to include pictures of the images you created with your program. Since this project is an introduction to writing Python code, the first required picture is of the tools you need to write and run the code. Get your screen set up: Run your add3.py program and leave the output visible in the Terminal. Then make sure the terminal and TextWrangler are all visible (and make sure TextWrangler has add3.py displayed). Take a picture of these three things by pressing Shift-Cmd-4. A cursor will pop up. Draw a box around TextWrangler, and the Terminal. The computer will take a picture of it and place that picture on your desktop. I have found that it is a good idea to make your picture as narrow as possible. Otherwise, it can be hard to view it on the wiki page. Also, you should change the name of the file containing the image. The Wiki tends to have problems with dates within filenames.
  4. Now let's edit the content of the Project 1 wiki page. Click on the link to it, then press Edit. Write a stub for your write-up saying "These are the tools we use." and place the image in the page. (Press the mountain button and upload the image from your Desktop.)
  5. Label the page. The label field is a text field at the bottom of the page, and is different from the title of your page. Each assignment will specify a label, which we will use to find your pages so we can show off your work and so we can grade it. For this project, add the label cs152s18project1.
  6. Save it, sit back and think about how much you have already accomplished.

For Your Information

If you ever need to remove a page, go to that page and select "Remove" from the "Tools" menu in the upper right.

Remember to save your wiki page before navigating away from it. The wiki won't detect it and remind you.

How to turn in code

You will turn in your code by putting it in a directory in the Courses server. On the Courses server, you should have access to a directory called CS151, and within that, a directory with your user name. Files that you put into that directory you can edit, read, and write, and the professor can edit, read, and write, but no one else. To hand in your code and other materials, you will create a new directory, such as project1, and then copy your code into the project directory for that week.

As with the Personal server, there are two ways to mount the appropriate directory.

Project Prep

Practice turning in your code by copying your entire project1 directory from your Personal server into your private folder on the Courses server. The easiest way to do this is to drag and drop the folder from one Finder (one open to Personal) to another (one open to your private folder on Courses).

When you are done with the lab exercises, you may start on the rest of the project.

© 2018 Caitrin Eaton.