- •Introduction
- •Introduction to Python and its use in science
- •Launching Python
- •Installing Python on your computer
- •The Canopy window
- •The Interactive Python Pane
- •Interactive Python as a calculator
- •Python Modules
- •Variables
- •Importing Modules
- •Getting help: documentation in IPython
- •Programming is a detail-oriented activity
- •Exercises
- •Strings, Lists, Arrays, and Dictionaries
- •Strings
- •Lists
- •NumPy arrays
- •Dictionaries
- •Random numbers
- •Exercises
- •Input and Output
- •Keyboard input
- •Screen output
- •File input
- •File output
- •Exercises
- •Plotting
- •An interactive session with pyplot
- •Basic plotting
- •Logarithmic plots
- •More advanced graphical output
- •Exercises
- •Conditionals and Loops
- •Conditionals
- •Loops
- •List Comprehensions
- •Exercises
- •Functions
- •Methods and attributes
- •Exercises
- •Curve Fitting
- •Exercises
- •Numerical Routines: SciPy and NumPy
- •Special functions
- •Linear algebra
- •Solving non-linear equations
- •Solving ODEs
- •Discrete (fast) Fourier transforms
- •Exercises
- •Installing Python
- •IPython Notebooks
- •Python Resources
APPENDIX
A
INSTALLING PYTHON
For scientific programming with Python, you need to install Python and three scientific Python libraries: NumPy, SciPy, and MatPlotLib. There are many more libraries you can install, but Python along with NumPy, SciPy, and MatPlotLib are those that are essential for scientific programming.
A.1 Installing Python
There are a number of ways to install Python and the scientific libraries you will need on your computer. Some are easier than others. You can install Python and the scientific libraries you need from “source” and compile them yourself. This is not recommended unless you are an expert in Python, in which case you have little need for this manual.
For most people, the simplest way to install Python and all the scientific libraries you need is to install either Canopy or Spyder. Canopy and Spyder are integrated development environments (IDEs) for Python. They have a number of very useful features and tools. First, they have syntax highlighting, which colors different parts Python syntax according to function, making code easier to read. Second, and more importantly, they run a program in the background called PyFlakes that checks the validity of the Python syntax as you write it. It’s like a spelling and grammar checker all rolled into one, and it is extremely useful, for novice and expert alike. The Canopy and Spyder IDEs have a number of other useful features, which we do not go into here, but expect you will learn about as you become more familiar with Python. Canopy is a simpler IDE than Spyder, and easier for novices to learn and maintain. Spyder has more advanced features, which you may find useful as you become more expert in Python programming.
Canopy is written, maintained, and distributed by the software company Enthought (http://www.enthought.com/). There are two versions of Canopy. One version, Canopy
179
Introduction to Python for Science, Release 0.9.23
Express, is completely free to everybody and contains all the libraries you will need for this manual. The other, Canopy Basic, contains nearly every library you are ever likely to need for scientific computing. It is free to academic users; others pay a fee. Go to https://www.enthought.com/products/canopy/ and press the “Get Canopy” button, which will take you to a page where you can either download Canopy Express or request and academic license, which will allow you to download Canopy Basic.
Spyder |
provides a |
completely open |
source programming |
environment |
for |
Python. |
The entire |
Spyder distribution |
is free to all and |
can be found |
at |
https://code.google.com/p/spyderlib/. It also includes nearly all the scientific libraries you are likely to need for scientific computing.
In this manual, we assume you are using Canopy, but the Spyder interface is very similar to Canopy so that most users should have no difficulty using Spyder with this manual. If you choose to use Spyder, launch Spyder and then go to the Preferences menu and then under the Console menu, select the Advanced settings tab; tick the box Start an IPython kernel at startup (it may already be selected, in which case you need to do nothing). You only need to do this once, which sets up the IPython console when Spyder is launched. Once that is done, you should be able to follow everything written in this manual.
A.2 Testing your installation of Python
Running the Python program below tests your installation of Python to verify that the installation was successful. In particular, it tests that the NumPy, SciPy, and MatPlotLib libraries that are needed for this manual are properly installed.
If you are a student, you should input your first and last names inside the single quotes on lines 15 and 16, respectively.
Instructors can modify lines 21-23 to suit the needs of the course.
1 # This code tests that your Python installation worked. 2 # It generates a png image file that you should e-mail 3 # to the address shown on the plot
4 import scipy
5import numpy
6import matplotlib
7 import matplotlib.pyplot as plt
8 import platform
9import socket
10
11 # If you are a student, please fill in your first and last
180 |
Appendix A. Installing Python |
Introduction to Python for Science, Release 0.9.23
12# names inside the quotes in the two lines below. Do not
13# modify anything else in this file
14
15your_first_name = ’Dana’
16your_last_name = ’Martin’
17
18# If you are an instructor, modify the next 3 lines.
19# You do not need to modify anything else in this file.
20
21classname = ’Intro Phys I’
22term = ’Fall_2013’ # must contain no spaces
23email = ’hmwkemail@univX.edu’
24
25plt.plot([0,1], ’r’, [1,0], ’b’)
26plt.text( 0.5, 0.8, ’{0:s} {1:s}’.format(your_first_name, your_last_name),
27horizontalalignment=’center’,
28size = ’x-large’,
29bbox=dict(facecolor=’purple’, alpha=0.4))
30plt.text( 0.5, 0.1,
31’{1:s}\nscipy {2:s}\nnumpy {3:s}\nmatplotlib {4:s}\non {5:s}\n{6:s}’.format
32classname,
33term,
34scipy.__version__,
35numpy.__version__,
36matplotlib.__version__,
37platform.platform(),
38socket.gethostname()
39) ,
40horizontalalignment=’center’
41)
42filename = your_last_name + ’_’ + your_first_name + ’_’ + term + ’.png’
43plt.title(’*** E-mail the saved version of this plot, ***\n’ +
44’"{0:s}" to pine@nyu.edu’.format(filename), fontsize=12)
45plt.savefig(filename)
46plt.show()
A.2. Testing your installation of Python |
181 |
Introduction to Python for Science, Release 0.9.23
182 |
Appendix A. Installing Python |