MIT6 0001F16 Getting Start
MIT6 0001F16 Getting Start
This handout will cover how to set up Python and introduce you to Spyder, the Python development
environment we will be using throughout this course. It will also show you how to check that the Python
library packages matplotlib and numpy were successfully installed. They will be used later in this class to
work with charts.
1. Visit the download page for Contiuum Analytics. NOTE: the installer we’ll be downloading and
installing is larger than the average file, because it contains Python, associated packages, a code
editor, and some other toys. It may take 15-20 minutes in total to download and install when
executing the commands.
2. Windows Operating System: There are two versions of the installer, one for 32-bit Windows, and one
for 64-bit Windows. Microsoft provides instructions here to determine (relatively easily) if you have a
32 or 64-bit setup. If you are running 32-bit Windows, be sure that you select the 32-bit version, and
if you are running 64-bit Windows, you can download either version.
The class is taught using Python 3.5. Do not download any other version.
3. Macintosh Operating System: There is only one version of the installer: the 64-bit version. Contiuum
does not offer a 32-bit installer for Macintosh, since Macs post-2011 are 64-bit only (and post-2007
are 64-bit compatible).
The class is taught using Python 3.5. Do not download any other version.
1
Installing Python and Spyder
1. To install Python after downloading the graphical installer, double click the .exe (Windows) or .pkg
(Mac) file and follow the instructions on the screen. There are some additional notes and comments
on installing/uninstalling Python on Contiuum’s website here.
2. Note that Contiuum’s Python distribution should (per their quick start guide) install cleanly into a
single directory, should not require Administrator or root privileges, and should not affect other
Python installs on your system (if you have any), or interfere with OSX Frameworks. Let us know via
Piazza if you’re having any issues, so that others can learn along with you.
3. When installing Anaconda, a pop-up menu may ask whether or not to “Add Anaconda to my PATH
environment variable”, and “Register Anaconda as my default Python 3.5.” We suggest accepting both
options for the purposes of this class.
When you download and install the Anaconda distribution of Python, you are getting several tools related to
Python development. One of the tools is Spyder (Scientific Python Development Environment), an integrated
development environment useful for writing, running, and debugging code. There are two primary ways to
access the tools and open Spyder:
(2)
(1)
2
Spyder Application Overview
Opening the Spyder application should present you with the following window (screenshot is from Windows,
but similar on Macs). This window contains two commonly used parts, among others:
(1) Code Editor that lets you create and edit existing Python source files [YELLOW CIRCLE 1]
(2) IPython interpreter pane, which gives you access to the Python interactive mode [YELLOW CIRCLE 2].
During the following discussion of Spyder’s features, you should start Spyder and try to replicate the
screenshots.
1. The IPython prompt looks something like this: “In [1]:”, and can be used to run Python code. You can
type Python code directly into this prompt, and pressing enter executes the code fragment.
2. Try typing the following after the prompt and pressing the enter key: 3*5
3
Using the IPython prompt – examples and exercises:
Addition (+), subtraction (-), multiplication (*), division (/), modulo (%) and power (**) operators are built
into the Python language. This means you can use them right away. If you want to use a square root in your
calculation, you can either raise something to the power of 0.5 or you can import the math module. Do not
worry about what it means right now, we will cover this later during the course. Below are two examples of
square root calculation:
In [1]: 16**0.5
Out [1]: 4.0
In [3]: math.sqrt(16)
Out[3]: 4.0
In [1]: math.log(16, 2)
Out [1]: 4.0
In [2]: math.cos( 0 )
Out [2]: 1.0
(these are just for practice, solutions will not be graded or collected in class)
1. 6+4*10
2. (6+4)*10
a. (Compare this to #1, and note that Python uses parentheses just like you would in
normal math to determine order of operations!)
3. 23.0 to the 5th power
4. Positive root of the following equation:
a. 34*x^2 + 68*x - 510
b. Recall:
i. Given a*x^2 + b*x + c,
ii. Then x1 = (-b +sqrt(b*b - 4*a*c))/(2*a)
5. import math
math.cos(3.4)**2+math.sin(3.4)**2
4
Saving the file
1. Saving your files in a location specific for this course can aid in organization. To do that, first create a
directory (or folder) for your 6.0001 material, with an appropriate name.
2. Now you can save your "hello world" file in the new folder that you just created.
3. From the Spyder File menu, click “Save As” and then navigate to your course folder before typing a
name for this file, e.g., "pyTest". Make sure that you’re saving the file as type Python (see below for
images on Mac and PC).
2. You should see two parts to the output in the IPython console. First, The runfile(…) line shows you the
filepath on your computer for the file you just ran. Second, the output of the file, “hello, world!”,
should appear.
3. Go back to the Code Editor and add the following line: print 'I like 6.001!'
4. Select “Run File” again (you can also use the green triangular Run button on the toolbar or the
shortcut F5), and observe the change in result.
6. Now reopen your test file by double-clicking its name in the File menu’s "Open recent" option.
7. Congratulations - your Python environment has been installed, and you now know how to enter
expressions in Python and how to save, run, and open files!
8. For additional help on Spyder, go the Help menu within Spyder, and select the “Spyder tutorial”
option.
5
Installing and Testing Matplotlib and Numpy
To work with charts, you will need these Python library packages: matplotlib and numpy. You will not need
these packages until later assignments, but they will be used in class demos during lecture so we encourage
you to download them early.
If you’re using Anaconda Python, your system should already have numpy and matplotlib installed. Start
Spyder and type “import numpy” and “import matplotlib” into the prompts. If no errors show up, you already
have them installed. If errors show up, reach out via Piazza to the instructors!
To test that you have successfully installed matplotlib and numpy via a script, run the code provided in
pkgtest.py, which should generate a chart for you. If you have problems with installation, be sure to go to
office hours or post on Piazza for help.
--------
Asfandyar Qureshi, Feb 2006
Edited by Vladimir Bychkovsky, Sept 2006
Edited by Calvin On, Feb 2007
Edited by Yang Zhang, Sep 2008
Edited by Chih-yu Chao, Feb 2009
Edited by Sari Canelake, Dec 2009
Edited by Anjali Muralidhar, Feb 2013
Edited by Niki Castle, Feb 2013
Edited by Prashan Wanigasekara, Feb 2014
Edited by Zachary Gil Freeman, Feb 2015
Edited by Ana Bell, August 2016
6
MIT OpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.