Install Anaconda

For all three of the Python tutorials this year we will be using the Anaconda Python distribution. The Anaconda distribution contains the latest version of Python, a large number of useful libraries for scientific programming, and other useful tools.

To download the installer for Anaconda, go to www.anaconda.com/download/ and click the button to download the Python 3.6 version.

The installation process will create an Anaconda folder on your hard drive. The bin folder in that Anaconda folder contains a Python interpreter.

Install an editor and/or an IDE

To program in Python you can use either a combination of a text editor and a terminal window, or you can use an integrated development environment.

To write Python programs with a simple text editor you will want to use a text editor that offers support for the Python language: I recommend BBEdit for Mac OS X or NotePad++ for Windows.

If you would like to use an IDE, I recommend the PyCharm Community Edition IDE.

Using Python interactively

Python is an interpreted language, so one mode that you can use Python in is an interactive mode. To use this mode you simply open a terminal window and launch the Python interpreter in interactive mode.

On Windows, type "Anaconda Prompt" into the search box to launch an Anaconda terminal window. On Mac simply start the Terminal application.

To start the Python interpreter, simply type

python

into the terminal window. You should see a startup welcome message from Python followed by a command prompt.

For our first simple example, type the following two lines in at the command prompt:

msg = "Hello, World!"
print(msg)

As you type each line the interpreter will evaluate the code you typed. Code that produces printed output will result in the interpreter printing a message.

To quit the interactive session, type

quit()

Using a text editor and the command prompt

Another method you can use to run Python code is to type the code into a text editor to create a Python program. You can then execute that program with the Python interpreter.

To use this method, first create a folder to hold your Python program in a convenient location. Since the terminal starts up in your home folder, placing the project folder in your home folder is a good idea. For this example, I will assume that I have created a folder named 'example' in my home folder.

Next, start up your text editor, make a new document and copy and paste the code

n = 2
msg = "I ate " + str(n) + " donuts today."
print("Status update: " + msg)

into that file. Save the file as example.py in the example folder.

Next, open a terminal window or the Anaconda prompt window and type the commands

cd example
python example.py

The second command will cause the interpreter to load and run the Python code from example.py. You will then see the output produced by that code.

Using the PyCharm IDE

Launch the PyCharm IDE. The first thing you will see is a welcome screen that prompts you to either create a new project or open an existing project. Click the option to create a new project. By default, PyCharm will prompt you to create a new project folder in the PyCharmProjects folder in your home folder.

At start the project will be empty. To make a source code file for your Python code, right-click on the project folder in the project pane and select the option New/Python File. Name the new file example.py. and copy and paste the code

n = 2
msg = "I ate " + str(n) + " donuts today."
print("Status update: " + msg)

into that file.

To execute the code in the file, simply select Run from the PyCharm Run menu. This will open up an output pane that shows you the output from your program.

A fancier example

The Anaconda distribution includes a large number of useful libraries for doing scientific programming. Here is a somewhat more sophisticated example that makes use of the matplotlib and numpy libraries.

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(-np.pi, np.pi, 0.01)
s = np.sin(t)
plt.plot(t, s)
plt.show()

You can run this code either from the interactive prompt or from the PyCharm IDE. The example constructs a plot of the function s = sin t over the range (-π,π) and displays the plot in a graphical window.