python for earth scientists - university of...

29
Python for Earth Scientists Andrew Walker [email protected] for Earth Scientists: 27 & 29 Sept. 2011

Upload: others

Post on 18-Feb-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Python for Earth Scientists

Andrew Walker

[email protected]

for Earth Scientists:27 & 29 Sept. 2011

Page 2: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

Python is:

A dynamic, interpreted programming language.

Page 3: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

Python is:

A dynamic, interpreted programming language.

Sourcecode

Objectcode

Compiler Linker Executable

Static, compiled language

Data

Output

Page 4: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

Python is:

A dynamic, interpreted programming language.

Sourcecode

Interpreter

Data

Output

The program can change at

run time

Page 5: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

Python is:

A dynamic, interpreted programming language.

Sourcecode

Interpreter

Data

Output

The program can change at

run time

Autogenerated plot of sompot

1 2.5 4 5.5 7 8.5 100

10

20

30

40

50

Page 6: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

Python is:

A dynamic, interpreted programming language...

... strongly typed but dynamically typed.

Page 7: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

Python is:

A dynamic, interpreted programming language...

... strongly typed but dynamically typed.

Weak Strong

Static

DynamicPerlMatlabJavaScript

ErlangScheme

Python

JavaFortran

C

C++

Haskell

x = 1 + “2.0”

a = 5 ...

a = “cat”

Weak

Dynamic

Staticreal(dp) :: r...

r = 5.2_dp

Some typing styles

Page 8: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

Python is:

A dynamic, interpreted programming language...

... strongly typed but dynamically typed ...

... imperative and object oriented.

Page 9: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

Python is:

A dynamic, interpreted programming language...

... strongly typed but dynamically typed ...

... imperative and object oriented.

programming

imperative

declarative

procedural

object oriented

logic

functional

(Can look procedural)

Page 10: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

http://xkcd.com/353/

for Earth Scientists:27 & 29 Sept. 2011

Python is:

A dynamic, interpreted programming language...

... strongly typed but dynamically typed ...

... imperative and object oriented ...

... fun to learn and use.

Page 11: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

Multi-paradigm

Designed

Encourages rapid (agile) development

Comes withbatteries included

Modern

Good as ‘glue’

Opinionated

Testable

Portable

Free

Open

Evolving

Page 12: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

TodayI’ll talk for ~30 mins on Python’s syntax and landscapePractical 1: First steps I’ll say something about types, modules, scripts and the standard library Practical 2: Scripts and modules

Now.

2 pm.

3 pm.

4 pm.

I’ll explain object oriented programming

Practical 3: Object oriented programming

I’ll introduce SciPy, NumPy and Matplotlib

Practical 4: NumPy, SciPy and Matplotlib

1 pm.

2 pm.

3 pm.

4 pm.

Thursday

We should finish by 5 pm. Don’t worry if you don’t finish all the exercises.

Page 13: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Syntaxprint “Hello, world!”

a = 1 # an integerb = 2.7 # a floatc = “Hello, world” # a stringprint aprint bprint cx1 = 6 # This is OK1x = 7 # This is an error

for Earth Scientists:27 & 29 Sept. 2011

Starting with this is compulsory

Variable assignment, comments and three types

Page 14: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Syntax

a = 1b = 10c = a + b # What is c?(4 < 5) # True(4 > 5) # False((4 < 5) or (4 > 5)) # True

for Earth Scientists:27 & 29 Sept. 2011

Some mathematical and logical operators.

Page 15: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Syntax

if sky == ‘blue’: birdsong = Trueelif sky == ‘black’: birdsong = Falseelse: pass #do nothing

for Earth Scientists:27 & 29 Sept. 2011

If, elif, else. Note indentation. No case statement.

Page 16: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Syntax

for Earth Scientists:27 & 29 Sept. 2011

a = 0while (a < 10): print a a = a + 1

While loop

for a in range(10): print a

Iteration

Page 17: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Syntax

for Earth Scientists:27 & 29 Sept. 2011

b = add_ten(5)print b

Function use

def add_ten (value): value = value + 10 return value

Function definition

Page 18: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Landscape

#!/usr/bin/env python“””List files in the workingdirectory

“””import os

def main(): print os.getcwd() for fn in os.listdir(‘.’): print fn

if __name__ == ‘__main__’: main()

for Earth Scientists:27 & 29 Sept. 2011

The interpreter

Language parser and implementation

Host computer &operatingsystem:

access to disks,

network, users etc.

Page 19: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Landscape

#!/usr/bin/env python“””List files in the workingdirectory

“””import os

def main(): print os.getcwd() for fn in os.listdir(‘.’): print fn

if __name__ == ‘__main__’: main()

for Earth Scientists:27 & 29 Sept. 2011

The interpreter

Language parser and implementation

User module

Third party

module

Standard library module

Extensionmodule

(compiled)

Host computer &operatingsystem:

access to disks,

network, users etc.

Page 20: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

The interpreter

Language parser and implementation

CPython IronPythonJython PyPy

C Java C# Python

Reference implementation.

Runs on windows, unix, mac etc.

Extension modules are .so/.dll

Targets Java virtual machine. Can load Java libraries as extension modules

Targets .NET framework. Can integrate with

silverlight, visual basic etc.

Compiler and implementation.

Includes JIT compiler and

can be very fast

Page 21: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Standard library

• Mathematical functions

• Advanced string processing

• Many data types and specialised algorithms

• Threading and multiprocessing

• Access to OS facilities

• Networking: sockets, http, email, ftp

• Data storage, database access

• Compression, encryption, hashing

• File formats, XML

• Graphics

~300 modules designed to be shipped with interpreter

for Earth Scientists:27 & 29 Sept. 2011 http://docs.python.org/library/

Page 22: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Package index16725 free to use packages (collections of modules) in a searchable index

http://pypi.python.org/for Earth Scientists:27 & 29 Sept. 2011

Page 23: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

for Earth Scientists:27 & 29 Sept. 2011

Integrated development environment

Interpreter ± collection of modules ± other stuff

= Python distribution

The interpreter

Language parser and implementation

Standard library module

Standard library module

Standard library module

Standard library

modules

Third party

module

Third party

module

Third party

module

Third party

modules

Module management and upgrade

tool

Distribution

Page 24: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Some distributions

• Official CPython distribution

• Distribution with OS: Mac, Linux

• Commercial: ActivePython, Enthought

• Specialised: stackless, embedded

http://wiki.python.org/moin/PythonDistributionsfor Earth Scientists:27 & 29 Sept. 2011

Page 25: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

VersioningIn principle language version is separate from interpreter version and versioning of modules. However, CPython and Python stay in lock-step

0.9.0

1990

for Earth Scientists:27 & 29 Sept. 2011

1995 2000 2005 2010

1.0 1.62.0 2.4 2.5 2.6 2.7

3.0 3.1 3.2 3.2.2

2.7.2

Page 26: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Development

for Earth Scientists:27 & 29 Sept. 2011

Interpreter developed in a similar way to other large pieces of open source code (distributed version control, bug tracker, etc.)

Language developed by the adoption of Python Enhancement Proposals (PEPs) - open process but ultimately decided on by “BDFL”

http://docs.python.org/devguide/#http://www.python.org/dev/peps/

(Python Language Moratorium until late 2012)

Page 27: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

http://docs.python.org/

http://www.python.org/

for Earth Scientists:27 & 29 Sept. 2011

Page 28: Python for Earth Scientists - University of Leedshomepages.see.leeds.ac.uk/~earawa/PythonEarthSci/PfES_1.pdf · 2017-10-25 · I’ll talk for ~30 mins on Python’s syntax and landscape

Enthought Python Distribution

Commercial python distribution aimed at scientists. Free for academics. What we will use because it is easy to install and has all the modules we need.

for Earth Scientists:27 & 29 Sept. 2011 http://www.enthought.com/products/epd.php