Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Introduction to Python for Science 2013.pdf
Скачиваний:
60
Добавлен:
21.05.2015
Размер:
2.41 Mб
Скачать

Introduction to Python for Science

Release 0.9.23

David Pine

December 19, 2013

CONTENTS

1

Introduction

3

 

1.1

Introduction to Python and its use in science . . . . . . . . . . . . . . .

3

2

Launching Python

5

 

2.1

Installing Python on your computer . . . . . . . . . . . . . . . . . . . .

5

 

2.2

The Canopy window . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

2.3

The Interactive Python Pane . . . . . . . . . . . . . . . . . . . . . . . .

6

 

2.4

Interactive Python as a calculator . . . . . . . . . . . . . . . . . . . . .

11

 

2.5

Python Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

14

 

2.6

Python functions: a first look . . . . . . . . . . . . . . . . . . . . . . .

15

 

2.7

Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

17

 

2.8

Script files and programs . . . . . . . . . . . . . . . . . . . . . . . . . .

20

 

2.9

Importing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

 

2.10

Getting help: documentation in IPython . . . . . . . . . . . . . . . . . .

25

 

2.11

Programming is a detail-oriented activity . . . . . . . . . . . . . . . . .

26

 

2.12

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

27

3 Strings, Lists, Arrays, and Dictionaries

29

 

3.1

Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

30

 

3.2

Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

31

 

3.3

NumPy arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

35

 

3.4

Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

46

 

3.5

Random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

48

 

3.6

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

52

4

Input and Output

55

 

4.1

Keyboard input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

55

i

4.2 Screen output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.3 File input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 4.4 File output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 4.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

5

Plotting

73

 

5.1

An interactive session with pyplot . . . . . . . . . . . . . . . . . . .

74

 

5.2

Basic plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

75

 

5.3

Logarithmic plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

89

 

5.4

More advanced graphical output . . . . . . . . . . . . . . . . . . . . . .

91

 

5.5

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

96

6

Conditionals and Loops

99

 

6.1

Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

99

 

6.2

Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

105

 

6.3

List Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . .

110

 

6.4

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

112

7

Functions

115

 

7.1

User-defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . .

115

 

7.2

Methods and attributes . . . . . . . . . . . . . . . . . . . . . . . . . . .

129

 

7.3

Example: linear least squares fitting . . . . . . . . . . . . . . . . . . . .

130

 

7.4

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

136

8

Curve Fitting

141

 

8.1

Using linear regression for fitting non-linear functions . . . . . . . . . .

141

 

8.2

Nonlinear fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

146

 

8.3

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

153

9

Numerical Routines: SciPy and NumPy

157

 

9.1

Special functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

158

 

9.2

Linear algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

161

 

9.3

Solving non-linear equations . . . . . . . . . . . . . . . . . . . . . . . .

165

 

9.4

Solving ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

170

 

9.5

Discrete (fast) Fourier transforms . . . . . . . . . . . . . . . . . . . . .

173

 

9.6

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

178

A

Installing Python

179

 

A.1

Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

179

 

A.2

Testing your installation of Python . . . . . . . . . . . . . . . . . . . . .

180

B

IPython Notebooks

183

 

B.1

An interactive interface . . . . . . . . . . . . . . . . . . . . . . . . . . .

183

ii

B.2

Installing and launching an IPython notebook . . . . . . . . . . . . . . .

183

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

193

C.1

Web resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

193

C.2

Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

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

CHAPTER

ONE

INTRODUCTION

1.1 Introduction to Python and its use in science

This manual is meant to serve as an introduction to the Python programming language and its use for scientific computing. It’s ok if you have never programmed a computer before. This manual will teach you how to do it from the ground up.

The Python programming language is useful for all kinds of scientific and engineering tasks. You can use it to analyze and plot data. You can also use it to numerically solve science and engineering problems that are difficult or even impossible to solve analytically.

While we want to marshall Python’s powers to address scientific problems, you should know that Python is a general purpose computer language that is widely used to address all kinds of computing tasks, from web applications to processing financial data on Wall Street and various scripting tasks for computer system management. Over the past decade it has been increasingly used by scientists and engineers for numerical computations, graphics, and as a “wrapper” for numerical software originally written in other languages, like Fortran and C.

Python is similar to Matlab and IDL, two other computer languages that are frequently used in science and engineering applications. Like Matlab and IDL, Python is an interpreted language, meaning you can run your code without having to go through an extra step of compiling, as required for the C and Fortran programming languages. It is also a dynamically typed language, meaning you don’t have to declare variables and set aside memory before using them. Don’t worry if you don’t know exactly what these terms mean. Their primary significance for you is that you can write Python code, test, and use it quickly with a minimum of fuss.

3

Introduction to Python for Science, Release 0.9.23

One advantage of Python over similar languages like Matlab and IDL is that it is free. It can be downloaded from the web and is available on all the standard computer platforms, including Windows, MacOS, and Linux. This also means that you can use Python without being tethered to the internet, as required for commercial software that is tied to a remote license server.

Another advantage is Python’s clean and simple syntax, including its implementation of object oriented programming (which we do not emphasize in this introduction).

An important disadvantage is that Python programs can be slower than compiled languages like C. For large scale simulations and other demanding applications, there can be a considerable speed penalty in using Python. In these cases, C, C++, or Fortran is recommended, although intelligent use of Python’s array processing tools contained in the NumPy module can greatly speed up Python code. Another disadvantage is that compared to Matlab and IDL, Python is less well documented. This stems from the fact that it is public open source software and thus is dependent on volunteers from the community of developers and users for documentation. The documentation is freely available on the web but is scattered among a number of different sites and can be terse. This manual will acquaint you with the most commonly-used web sites. Search engines like Google can help you find others.

You are not assumed to have had any previous programming experience. However, the purpose of this manual isn’t to teach you the principles of computer programming; it’s to provide a very practical guide to getting started with Python for scientific computing. Perhaps once you see some of the powerful tasks that you can accomplish with Python, you will be inspired to study computational science and engineering, as well as computer programming, in greater depth.

4

Chapter 1. Introduction

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]