Lab 1
Re-presenting Python


Preliminary Setup

  1. If you haven't already logged into a lab machine, do so now. I can help you with username/password information.
  2. Make a "csc120" folder. In that folder, make a "workspace" folder. You can use this folder for both labs and projects. Each lab or project will be a folder within the workshop folder.

Integrated Development Environments

In previous classes where you've used Python, you used a program called Idle to edit and run your programs. However, Python does not equal Idle. Python is a language in which you can write programs, but the tool you use to edit those programs is simply a text editor. Any text editor will do. [Note: Similarly, English is not Microsoft Word].

Once your programs are in the right language (in this case Python), they can be compiled or interpreted by appropriate tools (in this case, the Python interpreter). When you used Idle before, it provided a window for text file editing (where you could type your programs) and a way to run those programs. To run the programs, Idle would call the Python interpreter to do the job. Because editing and running are integrated together in Idle, we call Idle an Integrated Development Environment (IDE).

Another IDE, one used by professional developers, is called PyCharm. PyCharm is made by the JetBrains company, which also makes other IDEs, including ones designed for Java, C++, Ruby, etc.... In this course, you will use PyCharm instead of Idle. But remember, the language you are using is still the same old Python you are familiar with.

Getting Started with PyCharm

Launch PyCharm from the dock icon, if one is available, or you can find it by using the launcher. You'll be presented with options to Create New Project or Open an existing one. Choose the Create New Project option, which will result in a window like the following:

You can click the button with the 3 dots to the right of the Location field to navigate to the csc120/workspace folder you created earlier. In the example above, I'm creating a project called CassLab1 in my workspace folder. Please name your project with your name and lab number as well.

Add some Starter Code

In many labs, including this one, I'll be providing you with starter code that you need to augment with your own code. Download and add to your project folder:

  1. Right-click the link above and choose Save as..., and save the file to your project folder. Then in the lefthand pane in PyCharm, you should see that is in the project.

This program lets the user play a very simple game of dominoes. You can imagine that this might be a very early step along the way to making a full-featured dominoes game.

Have a look around

Now that you have a project and some code, explore to see what you have:
  1. Read the code. Can you figure out what each line is trying to do?
  2. Can you tell what functions are expected from the boneyard and domino module?
  3. Look at your files and folders. Notice that your project is just a folder (named the same as your project) that contains the .py files you've added to it.

Add the required modules

Now that you have an idea of what functions the domino and boneyard modules must provide, you'll be adding those modules to your project:
  1. Add the provided to your project. Download it into the project folder and you should see it show up in PyCharm.
  2. Read the boneyard module. Notice that it provides the functions used in the main module. If there are functions you don't understand, you can look them up in the Python documentation.

    Notice that the module represents the boneyard as a list of domino representations, which are created using the create function in the domino module.

  3. Add a new module to your project. It needs to have all of the functions that the main and boneyard modules expect it to have (i.e. create(left, right), get_left(domino), get_right(domino), and as_str(domino)). Start by having the two getters just return 0 and having the other two functions just pass.
  4. Now, you can run the code. The first time you run the code, you need to tell PyCharm what you want to run: choose "Run..." from the "Run" menu, and select Afterwards, you can re-run by clicking the green play button in the toolbar. Note: the program won't work, but it should run. To stop it running, you can click the red X on the left hand side of the output console.

Finish the domino module

  1. Start by writing as_str:
    def as_str(domino):
        return "[%d | %d]" % (get_left(domino), get_right(domino))
  2. Re-run the program and see what's changed.
  3. Write the create function, which needs to create a representation for a domino with the given left and right values. For this lab, you should use a tuple to represent a domino (where the left value is the first item in the tuple and the right value is the second value). Recall that (x, y) creates a tuple with values x and y.
  4. Re-run the program and see what's changed.
  5. Now, write get_left and get_right. These functions get the left value and the right value of a given domino. The representation created by the create function determines how these two functions must do their job.
  6. Re-run the program and see what's changed. Does the program do what we expect yet?

How to turn in this lab

Before turning in any program in this class, remember this mantra:

Just because it works doesn't mean it's good.

Part of your grade will also come from things like how understandable and readable your code is. You will also be graded on the neatness, presentation, and style of your program code.

For all labs, turn in only an electronic version. Please create a tarball of your PyCharm project and email it to me at

Follow these instructions (or ask me to show you a shortcut):

  1. In a terminal window, go to your workspace folder (the folder that contains the folders that are your PyCharm projects).
  2. Create the tarball with something like this:
    tar -cvzf CassLab1.tar.gz CassLab1
    See this page for an explanation of what that command is doing.
  3. Email the created file (the tar.gz tarball) to me.

Ask for help if you're having problems!