Python For Scientific Programming

3y ago
54 Views
7 Downloads
366.83 KB
54 Pages
Last View : Today
Last Download : 3m ago
Upload by : Adalynn Cowell
Transcription

Python for Scientific ProgrammingPaul SherwoodCCLRC Daresbury Laboratoryp.sherwood@dl.ac.uk4/28/2005homebacknext

Overview Introduction to the language– (thanks to David Beazley) Some important extension modules– free tools to extend the interpreter– extending and embedding with C and C Chemistry projects in Python– some examples of science projects that use Python Our experiences– a GUI for quantum chemistry codes in Python– a users perspectiveFor URLs of the packages referred to in this talk, please seehttp://www.cse.clrc.ac.uk/qcg/python

Introduction to the Language An Interpreted Language .–––– Dynamic nature (typing, resolving etc)New code can be entered in a running shellModules can be updated in a running interpreterSilently compiles to intermediate bytecodeKey Language Features––––It has efficient high-level data structuresA simple but effective approach to object-oriented programmingElegant syntaxDynamic typing

Language Overview Based on Slide series “An Introduction to Python by David M.BeazleyDepartment of Computer ScienceUniversity of Chicagobeazley@cs.uchicago.eduO'Reilly Open Source ConferenceJuly 17, ine.zip Author of the "Python Essential Reference" in 1999 (New RidersPublishing).All of the material presented here can be found in that source

PythonWhat is it?A freely available interpreted object-oriented scripting language.Often compared to Tcl and Perl, but it has a much different flavor.And a lot of people think it's pretty cool.HistoryDeveloped by Guido van Rossum in early 1990's.Named after Monty Python.Influences include ABC, Modula-2, Lisp, C, and shell scripting.Availabilityhttp://www.python.orgIt is included in most Linux distributions.Versions are available for Unix, Windows, and Macintosh.JPython. Python interpreter written in Java (http://www.jpython.org).

Starting PythonChances are, Python is already installed on your machine.unix % pythonPython 1.5.2 (#1, Sep 19 1999, 16:29:25) [GCC 2.7.2.3] on linux2 Copyright 19911995 Stichting Mathematisch Centrum, Amsterdam This starts the interpreter and allows you to type programs interactively.On Windows and MacintoshPython is launched as an application.An interpreter window will appear and you will see the prompt.IDLEAn integrated development environment for Python.Available at www.python.org.

Your First ProgramHello World print "Hello World“Hello World Well, that was easy enough.Python as a calculator 3*4.5 518.5 Basically, interactive mode is just a simple read-eval loop.Something more complicated for i in range(0,10):. print i012. etc .

Programs and FilesPrograms are generally placed in .py files like this# helloworld.pyprint "Hello World"To run a file, give the filename as an argument to the interpreterunix % python helloworld.pyHello Worldunix %Or you can use the Unix #! trick#!/usr/local/bin/pythonprint "Hello World"Or you can just double-click on the program (Windows)

Program TerminationProgram Execution Programs run until there are no more statements to execute. Usually this is signaled by EOF Can press Control-D (Unix) or Control-Z (Windows) to exit interactiveinterpreterForcing Termination Raising an exception: raise SystemExitCalling exit manually:import syssys.exit(0)

Variables and ExpressionsExpressions Standard mathematical operators work like other languages:3 53 (5*4)3 ** 2'Hello' 'World'Variable assignmenta 4 3b a * 4.5c (a b)/2.5a "Hello World" Variables are dynamically typed (No explicit typing, types may changeduring execution). Variables are just names for an object. Not tied to a memory locationlike in C.

Conditionalsif-else# Compute maximum (z) of a and bif a b:z belse:z aThe pass statementif a b:pass# Do nothingelse:z aNotes: Indentation used to denote bodies. pass used to denote an empty body. There is no '?:' operator.

Conditionalselif statementif a ' ':op PLUSelif a '-':op MINUSelif a '*':op MULTIPLYelse:op UNKNOWN Note: There is no switch statement.Boolean expressions: and, or, notif b a and b c:print "b is between a and c”if not (b a or b c):print "b is still between a and c" Note: &&, , and ! are not used.

Basic Types (Numbers and Strings)Numbersa 3b 4.5c 517288833333Ld 4 3j# Integer# Floating point# Long integer (arbitrary precision)# Complex (imaginary) numberStringsa 'Hello'# Single quotesb "World"# Double quotesc "Bob said 'hey there.'" # A mix of bothd '''A triple quoted string can span multiple lineslike this'''e """Also works for double quotes"""

Basic Types (Lists)Lists of arbitrary objectsa [2, 3, 4]# A list of integersb [2, 7, 3.5, "Hello"] # A mixed listc []# An empty listd [2, [a,b]]# A list containing a liste a b# Join two listsList manipulationx a[1]y b[1:3]z d[1][0][2]b[0] 42# Get 2nd element (0 is first)# Return a sublist# Nested lists# Change an elementList methodsa.append("foo")a.insert(1,"bar")len(a)# Append an element# Insert an element# Length of the list del a[2] # Delete an element

Basic Types (Tuples)Tuplesf (2,3,4,5)g (1,)h (2, [3,4], (10,11,12))# A tuple of integers# A one item tuple# A tuple containing mixed objectsTuple Manipulationx f[1]x 3 y f[1:3]y (3,4) z h[1][1]# Element access.# Slices.# Nesting. z 4Comments Tuples are like lists, but size is fixed at time of creation. Can't replace members (said to be "immutable") Why have tuples at all? This is actually a point of much discussion.

Basic Types (Dictionaries)Dictionaries (Associative Arrays)a {}# An empty dictionaryb { 'x': 3,'y': 4 }c { 'uid': 105, 'login': 'beazley', 'name' : 'David Beazley' }Dictionary Accessu c['uid']# Get an elementc['shell'] "/bin/sh"# Set an elementif c.has key("directory"):# Check for presence of an memberd c['directory']else:d Noned c.get("directory",None) # Same thing, more compactk c.keys()# Get all keys as a list

LoopsThe while statementwhile a b:# Do somethinga a 1The for statement (loops over members of a sequence)for i in [3, 4, 10, 25]:print i# Print characters one at a timefor c in "Hello World":print c# Loop over a range of numbersfor i in range(0,100):print i

FunctionsThe def statement# Return the remainder of a/bdef remainder(a,b):q a/br a - q*breturn r# Now use ita remainder(42,5) # a 2Returning multiple values (a common use of tuples)def divide(a,b):q a/br a - q*breturn q,rx,y divide(42,5) # x 8, y 2

ClassesThe class statementclass Account:def init (self, initial):self.balance initialdef deposit(self, amt):self.balance self.balance amtdef withdraw(self,amt):self.balance self.balance – amtdef getbalance(self):return self.balanceUsing a classa ithdraw(50)print a.getbalance()

ExceptionsThe try statementtry:f open("foo")except IOError:print "Couldn't open 'foo'. Sorry."The raise statementdef factorial(n):if n 0:raise ValueError,"Expected non-negative number"if (n 1):return 1else:return n*factorial(n-1)

ExceptionsUncaught exceptions factorial(-1)Traceback (innermost last):File " stdin ", line 1, in ?File " stdin ", line 3, in factorialValueError: Expected non-negative number

FilesThe open() functionf open("foo","w")# Open a file for writingg open("bar","r")# Open a file for readingReading and writing dataf.write("Hello World")data g.read()# Read all dataline g.readline()# Read a single linelines g.readlines() # Read data as a list of linesFormatted I/OUse the % operator for strings (works like C printf)for i in range(0,10):f.write("2 times %d %d\n" % (i, 2*i))

ModulesLarge programs can be broken into modules# numbers.pydef divide(a,b):q a/b r a - q*breturn q,rdef gcd(x,y):g ywhile x 0:g xx y%xy greturn gThe import statementimport numbersx,y numbers.divide(42,5)n numbers.gcd(7291823, 5683) import creates a namespace and executes a file

Python LibraryPython is packaged with a large library of standard modulesString processingOperating system interfacesNetworkingThreadsGUIDatabaseLanguage servicesSecurity.And there are many third party modulesXMLNumeric ProcessingPlotting/Graphicsetc.All of these are accessed using 'import'import stringa string.split(x)

Summary so far .You have seen about 90% of what you need to knowPython is a relatively simple language.Most novices can pick it up and start doing things right away.The code is readable.When in doubt, experiment interactively. for more of David Beazley’s slides, see the web pages (link at end).

Standards and Portability Not subject to any standardisation effort, it is essentially asingle implementation– i.e. python is defined by an open-source program, written in C,which can be ported to a wide range of platforms.– Jython is the main exception. A Python interpreter which runs in aJava VM In practice– How portable is the interpreter? It can easily be downloaded for Windows, Linux, Mac OSX– Some issues with some modules, e.g. TkInter on Mac OSXor compiled from Source– Wide user base is comforting– Main portability issues will be around the extensions

Extension Packages The range of freely downloadable modules is one of thestrengths of Python Usually adding a module to your distribution is relativelypainless– code is dynamically loaded from the interpreter, no relinking ofinterpreter needed– standardised approach to compiling and/or installing as part of yourpython installation (distutils module provides setup.py)– binary distributions are usually available (.rpm under linux, .exeinstallers in windows)

Extension Packages Numerical Python– adds an multidimensional array datatype, with access to fastroutines (BLAS) for matrix operations Scientific Python– basic geometry (vectors, tensors, transformations, vector and tensorfields), quaternions, automatic derivatives, (linear) interpolation,polynomials, elementary statistics, nonlinear least-squares fits, unitcalculations, Fortran-compatible text formatting, 3D visualization viaVRML, and two Tk widgets for simple line plots and 3D wireframemodels. Interfaces to the netCDF, MPI, and to BSPlib. SciPy– includes modules for graphics and plotting, optimization, integration,special functions, signal and image processing, genetic algorithms,ODE solvers, and others

Extension Packages GUI toolkits– Tkinter python bindings to Tk toolkit, shipped with python and used bypython’s own IDE (IDLE) still some problems here on MacOS/X– Pmw (Python MegaWidgets) more complex widgets based on Tkinter– wxPython– pyQT– pyGTK Also consider .– Anygui write once, run with any toolkit

Extension Packages 3D Visualisation– pyOpenGL low level 3D primitives– Visual Python (now vpython) low level– VTK large and powerful visualisation toolkit can be tricky to build from source Graph plotting– matplotlib pure python library with matlib-like approach– Pmw/BLT BLT is a Tk extension, Pmw provides bindings– R Bindings general purpose statistics language with plotting tools

Extension Packages Web– Zope is a web server written in Python– Python can be used as a CGI language install mod python into apache to avoid start-up costs of eachscript Grid and e-Science– Python tools for XML pyXML 4suite package (recommended)– Python COG kit for globus client side tools

Tools Windows– I regard Mark Hammond’s PythonWin is essential good handling of windows processes access to MFC, COM etc convenient way to move data from scientific applications toExcel and similar windows software Wrapping - automated generation of python commandsfrom libraries and their header files– SWIG - general purpose tool– SIP - specialised for Python and C – VTK - incorporates internal wrapping code for its C classes

Development Environments IDLE– Python’s native IDE Emacs python mode– My choice– useful tools to handle code indentation (important)– ctrl-C ctrl-C executes the buffer Other Shells available– PythonWin– PyCrust– Ipython There is also an outlining editor: Leo

Extending and Embedding Python is a C program and has a well developed and welldocumented API for– Extending Python writing your own functions, classes etc in C, C etc– needed to overcome limitations of interpreter performance– Embedding Python simplest case, just call python functions from within your code(e.g. to take advantage of extension modules) more generally– provide a number of extensions to the interpreter– embed python as a command line interpreter for yourapplication

Native Code Extension Modules (Example taken from the standard Python docs). Let's create an extension module called "spam" and let'ssay we want to create a Python interface to the C libraryfunction system(). This function takes a null-terminated character string asargument and returns an integer. We want this function tobe callable from Python as follows: import spam status spam.system("ls -l")

Source File Structure Begin by creating a file spammodule.c. The first line of our file pulls in the Python API#include Python.h All user-visible symbols defined by Python.h have a prefixof "Py" or "PY", except those defined in standard headerfiles. "Python.h" includes a few standard header files: stdio.h , string.h , errno.h , and stdlib.h

C Code This will be called when the Python expression"spam.system(string)"is evaluatedstatic PyObject *spam system(self, args)PyObject *self;PyObject *args;{char *command;int sts;if (!PyArg ParseTuple(args, "s", &command))return NULL;sts system(command);return Py BuildValue("i", sts);}

Initialisation To declare to Python, provide a method table:static PyMethodDef SpamMethods[] {.{"system", spam system, METH VARARGS,"Execute a shell command."},.{NULL, NULL, 0, NULL}/* Sentinel */}; provide an initialisation function named “init” modulevoidinitspam(void){(void) Py InitModule("spa

Python for Scientific Programming Paul Sherwood CCLRC Daresbury Laboratory p.sherwood@dl.ac.uk. Overview Introduction to the language – (thanks to David Beazley) Some important extension modules – free tools to extend the interpreter – extending and embedding with C and C Chemistry projects in Python – some examples of science projects that use Python Our experiences – a GUI for .

Related Documents:

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Python is readable 5 Python is complete—"batteries included" 6 Python is cross-platform 6 Python is free 6 1.3 What Python doesn't do as well 7 Python is not the fastest language 7 Python doesn't have the most libraries 8 Python doesn't check variable types at compile time 8 1.4 Why learn Python 3? 8 1.5 Summary 9

Bruksanvisning för bilstereo . Bruksanvisning for bilstereo . Instrukcja obsługi samochodowego odtwarzacza stereo . Operating Instructions for Car Stereo . 610-104 . SV . Bruksanvisning i original

site "Python 2.x is legacy, Python 3.x is the present and future of the language". In addition, "Python 3 eliminates many quirks that can unnecessarily trip up beginning programmers". However, note that Python 2 is currently still rather widely used. Python 2 and 3 are about 90% similar. Hence if you learn Python 3, you will likely

There are currently two versions of Python in use; Python 2 and Python 3. Python 3 is not backward compatible with Python 2. A lot of the imported modules were only available in Python 2 for quite some time, leading to a slow adoption of Python 3. However, this not really an issue anymore. Support for Python 2 will end in 2020.

Python Programming - This is a textbook in Python Programming with lots of Practical Examples and Exercises. You will learn the necessary foundation for basic programming with focus on Python. Python for Science and Engineering - This is a textbook in Python Programming with lots of Examples, Exercises, and Practical Applications

CR ASH COURSE PY THON CR ASH COURSE 2ND EDITION ERIC MATTHES SHELVE IN: PROGRAMMING LANGUAGES/ PYTHON 39.95 ( 53.95 CDN) LEARN PYTHON— FAST! COVERS PYTHON 3.X Python Crash Course is the world's best-selling guide to the Python programming language. This fast-paced, thorough introduction to programming with Python will