logo资料库

Introduction to Python for Science.pdf

第1页 / 共204页
第2页 / 共204页
第3页 / 共204页
第4页 / 共204页
第5页 / 共204页
第6页 / 共204页
第7页 / 共204页
第8页 / 共204页
资料共204页,剩余部分请下载后查看
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
Python functions: a first look
Variables
Script files and programs
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
User-defined functions
Methods and attributes
Example: linear least squares fitting
Exercises
Curve Fitting
Using linear regression for fitting non-linear functions
Nonlinear fitting
Exercises
Numerical Routines: SciPy and NumPy
Special functions
Linear algebra
Solving non-linear equations
Solving ODEs
Discrete (fast) Fourier transforms
Exercises
Installing Python
Installing Python
Testing your installation of Python
IPython Notebooks
An interactive interface
Installing and launching an IPython notebook
Using an IPython Notebook
Running programs in an IPython Notebook
Annotating an IPython Notebook
Editing and rerunning a notebook
Quitting an IPython notebook
Working with an existing IPython notebook
Python Resources
Web resources
Books
Index
Introduction to Python for Science Release 0.9.23 David Pine December 19, 2013
CONTENTS 1 Introduction 1.1 Introduction to Python and its use in science . . . . . . . . . . . . . . . 2 Launching Python . . . . . Installing Python on your computer . . The Canopy window . . . The Interactive Python Pane . . Interactive Python as a calculator Python Modules . Python functions: a first look . . . . . . . . . . . . . . . . . . . . . . . . 2.1 . . . . . . . . . . . . . . . . . . . . . 2.2 . . . . . . . . . . . . . . . . . . . . . 2.3 . . . . . . . . . . . . . . . . . . . . . 2.4 . . . . . . . . . . . . . . . . . . . . . 2.5 . . . . . . . . . . . . . . . . . . . . . 2.6 . . . . . . . . . . . . . . . . . . . . . 2.7 Variables . . . . . . . . . . . . . . . . . . . . . . 2.8 2.9 . . . . . . . . . . . . . . . . . . . . . 2.10 Getting help: documentation in IPython . . . . . . . . . . . . . . . . . . 2.11 Programming is a detail-oriented activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.12 Exercises . . Script files and programs . Importing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Strings Lists 3 Strings, Lists, Arrays, and Dictionaries . . . . . . 3.1 . 3.2 . 3.3 NumPy arrays 3.4 Dictionaries . 3.5 3.6 . . . . Random numbers . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Input and Output 4.1 Keyboard input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 3 5 5 5 6 11 14 15 17 20 24 25 26 27 29 30 31 35 46 48 52 55 55 i
4.2 4.3 4.4 4.5 Screen output . . File input . . File output Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Plotting Basic plotting . . Logarithmic plots . 5.1 An interactive session with pyplot . . . . . . 5.2 5.3 . 5.4 More advanced graphical output . . 5.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 62 66 70 73 74 75 89 91 96 6 Conditionals and Loops . . . Conditionals Loops . . . List Comprehensions . Exercises . . 6.1 6.2 6.3 6.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 99 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 . . . . . . . . . . . . . . . . . . . . . 110 . . . . . . . . . . . . . . . . . . . . . 112 7 Functions 115 . . . . . . . . . . . . . . . . . . . . . 115 . . . . . . . . . . . . . . . . . . . . . 129 Example: linear least squares fitting . . . . . . . . . . . . . . . . . . . . 130 Exercises . . . . . . . . . . . . . . . . . . . . . . 136 7.1 User-defined functions . 7.2 Methods and attributes . 7.3 7.4 . . . . . . . . . . . . . . . . . . . . . . 8 Curve Fitting 141 8.1 Using linear regression for fitting non-linear functions . . . . . . . . . . 141 8.2 Nonlinear fitting . . . . . . . . . . . . . . . . . . . . . . 146 . . . . . . . . . . . . . . . . . . . . . 153 . 8.3 Exercises . . . . . . . . . . . . . . . . . . . . 9 Numerical Routines: SciPy and NumPy . . . . 157 . . . . . . . . . . . . . . . . . . . . . 158 9.1 . . . . . . . . . . . . . . . . . . . . . 161 9.2 . . . . . . . . . . . . . . . . . . . . . 165 9.3 9.4 . . . . . . . . . . . . . . . . . . . . . 170 9.5 Discrete (fast) Fourier transforms . . . . . . . . . . . . . . . . . . . . . 173 . . . . . . . . . . . . . . . . . . . . . 178 9.6 . Special functions . Linear algebra . . . Solving non-linear equations . Solving ODEs . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Installing Python Installing Python . A.1 A.2 Testing your installation of Python . . . . . . . . . 179 . . . . . . . . . . . . . . . . . . . . . 179 . . . . . . . . . . . . . . . . . . . . 180 B IPython Notebooks B.1 An interactive interface . . . . . . 183 . . . . . . . . . . . . . . . . . . . . . 183 ii
Installing and launching an IPython notebook . . . . . . . . . . . . . . . 183 B.2 B.3 Using an IPython Notebook . . . . . . . . . . . . . . . . . . . . . . 185 B.4 Running programs in an IPython Notebook . . . . . . . . . . . . . . . . 187 B.5 Annotating an IPython Notebook . . . . . . . . . . . . . . . . . . . . . 187 B.6 Editing and rerunning a notebook . . . . . . . . . . . . . . . . . . . . . 190 B.7 Quitting an IPython notebook . . . . . . . . . . . . . . . . . . . . . 191 B.8 Working with an existing IPython notebook . . . . . . . . . . . . . . . . 191 . . . . C Python Resources C.1 Web resources . C.2 Books . . . . . . . . . . . . . . . . . . . . . . . . 193 . . . . . . . . . . . . . . . . . . . . . 193 . . . . . . . . . . . . . . . . . . . . . 195 Index 197 iii
iv
Introduction to Python for Science, Release 0.9.23 Contents: CONTENTS 1
Introduction to Python for Science, Release 0.9.23 2 CONTENTS
分享到:
收藏