ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 17.04.2021

Просмотров: 3700

Скачиваний: 3

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.
background image

Introduction to Python for

Science

Release 0.9.23

David Pine

December 19, 2013


background image

background image

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


background image

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


background image

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