Getting Started with Python for CS 335

Install Anaconda on your computer

Download Anacaonda version 2018.12 with Python version 3.7 for your platform (Windows / Mac/ Linux) here. Follow the installation instructions.

Running Python after installation

Python is typically invoked from the command-line like this:

$ python foo.py  # foo.py is a file containing a Python program

However, you need to make sure that you invoke the correct version of Python. The CS lab computers have multiple versions of Python installed—your computer probably does too.

One way to ensure you use the correct version is to specify the full path:

$ ~/anaconda3/bin/python foo.py   # Possible install location
$ /anaconda3/bin/python foo.py    # Another possible install location

To avoid typing the full path every time, you can set your system PATH variable so that the directory of the desired python appears first on your path. For example, in the bash shell:

$ export PATH="/anaconda3/bin:$PATH"
$ python foo.py   # Finds python in /anaconda3/bin

On Mac/Linux systems, you can add the export command to your .bash_profile file so it runs automatically when you start a terminal session. Note: the Anaconda installer offers an option to do this at install time, in which case you may not need to do anything.

You can check that you have set the PATH correctly by seeing which python the system finds:

$ which python 
/Users/sheldon/anaconda2/bin/python

Command-line help

If you are new to the command-line:

Other ways of running Python

iPython / Jupyter notbooks

A Jupyter notebook lets you write and execute Python code interactively in your web browser. It is a powerful way to interact with code and also to mix Python code with beatifully rendered text and math for the purposes of exposition. (Jupyter notebooks were formerly called iPython notebooks.) You can start Jupyter like this:

$ jupyter notebook

This will launch the Jupyter App and open a browser window on the computer. In the browser you can create a new notebook to interactively write and execute Python code. It looks like this:

Read this tutorial for more information on running Jupyter notebooks.

Interactive command-line Python

Python can be launched with no arguments:

$ python 

This launches an interactive session where you can type commands that execute interactively, similar to R or MATLAB:

Python 3.7.1 (default, Dec 14 2018, 13:28:58)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 1
>>> print(x)
1
>>> y = x
>>> x = x+1
>>> print(y)
1

This mode can be very useful.

iPython

For interactive sessions, the program ipython (“interactive Python”) may offer advantages. It is invoked the same way:

$ ipython

The iPython interactive session looks a bit different, and provides additional capabilities:

Python 3.7.1 (default, Dec 14 2018, 13:28:58)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: x = 1

In [2]: print(x)
1

In [3]: x = x+1

In [4]: print(x)
2

IDEs

Microsoft Visual Studio Code

Anaconda now includes the option to install Microsoft Visual Studio Code as part of the installation. This is a code editor and IDE that is supposed to work well with Python. Since it ships with Anaconda it is likely to “play nicely” with anaconda style Python programming You may want to try it.

PyCharm

PyCharm is a cross-platform IDE for Python with most popular programming features. Anaconda is a Pyhton installation with many useful Python packages. Both are free and easy to install. You can choose the OS you are familiar with.

Students can choose from two free versions of PyCharm: PyCharm Community or PyCharm Edu.

One significant advantage of an IDE is debugging support.