Introduction
This tutorial explains how to use your own computer to complete assignments for CIS 61, as well as introduce some of the basics of Python. If you are using a lab computer, most of the instructions are the same, except you won’t have to install anything.
Install Python 3
Python 3 is the primary programming language used in this course. Use the instructions below to install the Python 3 interpreter. (The instructions may feature older versions of Python 3, but the steps are similar.)
macOS
Download and install Python.
Windows
Download Python and make sure to check the “Add Python 3.x to PATH” box, which will allow you to execute the python
command from your terminal.
After installing please close and open your Terminal.
Install Sublime Text Editor
Using the terminal
Let’s check if everything was installed properly! First, open a new terminal window, if you haven’t already.
When you first open your terminal, you will start in the home directory. The home directory is represented by the ~
symbol.
Don’t worry if your terminal window doesn’t look exactly the same; the important part is that the text on the left-hand side of the
$
has a~
(tilde). That text might also have the name of your computer.
If you see your home directory instead of~
: Try runningecho ~
. If it displays the same path, that’s also fine.
Python Interpreter
We can use the terminal to check if your Python 3 interpreter was installed correctly. Try the following command:
python3
If the installation worked, you should see some text printed out about the interpreter followed by >>>
on its own line. This is where you can type in Python code. Try typing some expressions you saw in lecture, or just play around to see what happens! You can type exit()
or Ctrl-D
to return to your command line.
If you are using Windows and the
python3
command doesn’t work, try using justpython
orpy
.
Organizing your files
In this section, you will learn how to manage files using terminal commands.
Make sure your prompt contains a
$
somewhere in it and does not begin with>>>
. If it begins with>>>
you are still in a Python shell, and you need to exit. See above for how.
Directories
The first command you’ll use is ls
. Try typing it in the terminal:
ls
The ls
command lists all the files and folders in the current directory. A directory is another name for a folder (such as the Documents
folder). Since you’re in the home directory right now, you should see the contents of your home directory.
Changing directories
To move into another directory, use the cd
command. Let’s try moving into your Desktop directory. First, make sure you’re in your home directory (check for the ~
on your command line) and use ls
to see if the Desktop
directory is present. Try typing the following command into your terminal, which should move you into that directory:
cd Desktop
If your desktop directory is not located within your home directory and you can’t find it, ask a TA or a lab assistant for help.
There are a few ways to return to the home directory:
cd ..
(two dots). The..
means “the parent directory”. In this case, the parent directory ofcs61a
is your home directory, so you can usecd ..
to go up one directory.cd ~
(the tilde). Remember that~
means home directory, so this command will always change to your home directory.cd
(cd
on its own). Typing justcd
is a shortcut for typingcd ~
.
You do not have to keep your files on your Desktop if you prefer otherwise. Where you keep your files locally will not affect your grade. Do whatever is easiest and most convenient for you!
Making new directories
The next command is called mkdir
, which makes new directories. Let’s make a directory called CIS61
on your Desktop to store all of the assignments for this class:
mkdir CIS61
A folder named CIS61
will appear on your Desktop (assuming that Desktop is your present working directory. If not use pwd). You can verify this by using the ls
command again or by simply checking your Desktop.
At this point, let’s create some more directories. First, make sure you are in the ~/Desktop/
directory. Then, create folders called CIS61
Lecture1
and Lab1
inside of your CIS61
folder:
mkdir Lecture1
mkdir Lab1
Now if you list the contents of the directory (using ls
), you’ll see two folders, Lecture1
and Lab1
.
Useful Python command line options
When running a Python file, you can use options on the command line to inspect your code further. Here are a few that will come in handy. If you want to learn more about other Python command-line options, take a look at the documentation.
- Using no command-line options will run the code in the file you provide and return you to the command line.
python3
-i
: The-i
option runs your Python script, then opens an interactive session. In an interactive session, you run Python code line by line and get immediate feedback instead of running an entire file all at once. To exit, typeexit()
into the interpreter prompt. You can also use the keyboard shortcutCtrl-D
on Linux/Mac machines orCtrl-Z Enter
on Windows. If you edit the Python file while running it interactively, you will need to exit and restart the interpreter in order for those changes to take effect.python3 -i <filename>.py
-m doctest
: Runs doctests in a particular file. Doctests are surrounded by triple quotes ("""
) within functions. Each test in the file consists of>>>
followed by some Python code and the expected output (though the>>>
are not seen in the output of the doctest command).python3 -m doctest <filename>.py