Title: An Intro to Python
1An Intro to Python
- Randy Heiland
- heiland_at_indiana.edu
- June 17, 2008
2An Intro to Python
- How I learned to stop worrying and loved to
develop software again.
3Python - a scripting language
- Released in 1991 by Guido van Rossum (at Google
since Dec 05 where Python is heavily used) -
- Python is absolutely free, even for commercial
use - Dynamically typed
- Strongly typed
- Auto memory mgt
4Whats so great about Python?
- High-level lng Syntax (minimal, clean)
- Interpreted Interactive
- (--gt rapid prototyping/development)
- Glue-iness, Wrap-ability (www.swig.org)
- Introspection
- History with science apps (my 1st exposure, LANL)
- Vibrant (community) and evolving (language)
- www.python.org/doc/Humor.html
5In a nutshell
- Software engineers stay sane while continuing to
be very productive - Application scientists arent put off by
programming complexity - Creates a short bridge between the two
6Implementations
- C CPython
- Java Jython
- .NET IronPython
7So show me some code!
interpreter
- python
- Python 2.5.1
- gtgtgt x 'Euler'
- gtgtgt x
- 'Euler'
- gtgtgt x 2.718
- gtgtgt x 5j
- gtgtgt x
- (2.7185j)
- gtgtgt x.real, x.imag
- (2.718, 5.0)
dynamic typing
8- gtgtgt x'2'
- gtgtgt y2
- gtgtgt xy
- Traceback (most recent call last)
- File "ltstdingt", line 1, in ltmodulegt
- TypeError cannot concatenate 'str' and 'int'
objects - gtgtgt int(x) y
- 4
interpreter
- python
- Python 2.5.1
- gtgtgt x 'Euler'
- gtgtgt x
- 'Euler'
- gtgtgt x 2.718
- gtgtgt x 5j
- gtgtgt x
- (2.7185j)
- gtgtgt x.real, x.imag
- (2.718, 5.0)
strong typing
dynamic typing
9Python Functions
- gtgtgt def foo(x,y)
- ... z xy
- ... print z
- ...
- gtgtgt foo(3,4)
- 12
- gtgtgt foo(fun',5)
- funfunfunfunfun
Indentation required for statement blocks
10Python vs. Perl syntaxe.g. hash (dictionary vs.
assoc array)
- for i in range(5)
- x
- for j in range(3)
- xji j
- print x
- --gt outputs
- 0 0, 1 1, 2 2
- 0 1, 1 2, 2 3
- 0 2, 1 3, 2 4
- 0 3, 1 4, 2 5
- 0 4, 1 5, 2 6
- for i (0 .. 6000-1)
- x()
- for j (0 .. 1000-1)
- xji j
- xj
-
-
11Python Modules
- A module is a file containing Python definitions
and statements - ltmodule-namegt.py
- import the module to use it
- .pyc byte-compiled, arch-independent file
- E.g. in file foo.py
- def foo(x,y)
- z xy
- print z
12Large standard library(Python batteries
included)
Just one example module
- gtgtgt import os
- gtgtgt dir(os)
- '_copy_reg', '_execvpe', '_exists', '_exit',
'_get_exports_list', '_make_stat_result',
'_make_statvfs_result', '_pickle_stat_result',
'_pickle_statvfs_result', '_spawnvef', 'abort',
'access', 'altsep', 'chdir', 'chmod', 'chown',
'chroot', 'close', 'confstr', 'confstr_names',
'ctermid', 'curdir', 'defpath', 'devnull', 'dup',
'dup2', 'environ', 'error', 'execl', 'execle',
'execlp', 'execlpe', 'execv', 'execve', 'execvp',
'execvpe', 'extsep', 'fchdir', 'fdopen', 'fork',
'forkpty', 'fpathconf', 'fstat', 'fstatvfs',
'fsync', 'ftruncate', 'getcwd', 'getcwdu',
'getegid', 'getenv', 'geteuid', 'getgid',
'getgroups', 'getloadavg', 'getlogin', 'getpgid',
'getpgrp', 'getpid', 'getppid', 'getsid',
'getuid', 'isatty', 'kill', 'killpg', 'lchown',
'linesep', 'link', 'listdir', 'lseek', 'lstat',
'major', 'makedev', 'makedirs', 'minor', 'mkdir',
'mkfifo', 'mknod', 'name', 'nice', 'open',
'openpty', 'pardir', 'path', 'pathconf',
'pathconf_names', 'pathsep', 'pipe', 'popen',
'popen2', 'popen3', 'popen4', 'putenv', 'read',
'readlink', 'remove', 'removedirs', 'rename',
'renames', 'rmdir', 'sep', 'setegid', 'seteuid',
'setgid', 'setgroups', 'setpgid', 'setpgrp',
'setregid', 'setreuid', 'setsid', 'setuid',
'spawnl', 'spawnle', 'spawnlp', 'spawnlpe',
'spawnv', 'spawnve', 'spawnvp', 'spawnvpe',
'stat', 'stat_float_times', 'stat_result',
'statvfs', 'statvfs_result', 'strerror',
'symlink', 'sys', 'sysconf', 'sysconf_names',
'system', 'tcgetpgrp', 'tcsetpgrp', 'tempnam',
'times', 'tmpfile', 'tmpnam', 'ttyname', 'umask',
'uname', 'unlink', 'unsetenv', 'urandom',
'utime', 'wait', 'wait3', 'wait4', 'waitpid',
'walk', 'write'
13Another standard lib module
- gtgtgt import math
- gtgtgt dir(math)
- '__doc__', '__file__', '__name__', 'acos',
'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh',
'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod',
'frexp', 'hypot', 'ldexp', 'log', 'log10',
'modf', 'pi', 'pow', 'radians', 'sin', 'sinh',
'sqrt', 'tan', 'tanh' - gtgtgt math.cos(math.pi)
- -1.0
Alternatively gtgtgt from math import gtgtgt cos(pi)
14Beyond the standard lib - installing community
modules
- There are many freely available community (3rd
party) modules available. You just need to
install them - e.g., from a shell - python setup.py install
- If the installation method is not obvious from
the modules download site, rf - http//docs.python.org/inst/inst.html
- http//peak.telecommunity.com/DevCenter/EasyInstal
l
15Beyond the standard lib - community packages -
e.g. NumPy
- Google for numpy, download/install it.
- gtgtgt from numpy import arange
- gtgtgt tvals arange(0.,5., 0.1)
- gtgtgt tvals
- array( 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
0.7, 0.8, 0.9, 1. , - 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7,
1.8, 1.9, 2. , 2.1, - 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8,
2.9, 3. , 3.1, 3.2, - 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9,
4. , 4.1, 4.2, 4.3, - 4.4, 4.5, 4.6, 4.7, 4.8, 4.9)
- gtgtgt tvals1
- 0.10000000000000001
16MATLAB vs. Python/NumPy
- www.scipy.org/NumPy_for_Matlab_Users
- gtgtgt from numpy import
- gtgtgt b array( (1.5,2,3), (4,5,6) )
- gtgtgt b
- array( 1.5, 2. , 3. ,
- 4. , 5. , 6. )
- gtgtgt b.shape
- (2, 3)
- gtgtgt dir(linalg)
- 'LinAlgError', '__builtins__', '__doc__',
'__file__', '__name__', '__path__', 'cholesky',
'det', 'eig', 'eigh', 'eigvals', 'eigvalsh',
'info', 'inv', 'lapack_lite', 'linalg', 'lstsq',
'norm', 'pinv', 'qr', 'solve', 'svd',
'tensorinv', 'tensorsolve', 'test'
NOTE NumPy is written in C and therefore quite
fast
17Basic Data Structures
- Lists
- Tuples
- Sets
- Dictionaries
- gtgtgt a 2.718, 'fred', 13
- gtgtgt dir(a)
- 'append', 'count', 'extend', 'index', 'insert',
'pop', 'remove', 'reverse', 'sort' - gtgtgt a1 56
- gtgtgt a
- 2.718, 56, 13
- gtgtgt a.sort()
- gtgtgt a
- 2.718, 13, 56
(mutable)
18Basic Data Structures (cont.)
- List comprehensions - concise way to map or
filter lists
- gtgtgt a
- 2.718, 13, 56
- gtgtgt import math
- gtgtgt math.exp(x) for x in a
- 15.149991940878165, 442413.39200892049,
2.0916594960129961e24
in the std lib
19Basic Data Structures (cont.)
- dictionary - 11 reln between key-value pairs
- (hash in Perl Hashtable in Java)
- gtgtgt simParam dict(latticesquare,
xdim100,ydim100,hex1) - gtgtgt grids
- square' 0, hex' 1
- gtgtgt dir(IUtoys)
- , 'clear', 'copy', 'fromkeys', 'get',
'has_key', 'items', 'iteritems', 'iterkeys',
'itervalues', 'keys', 'pop', 'popitem',
'setdefault', 'update', 'values'
20(free) Python shells
- IDLE
- ConTEXT (Windows only)
- IPython (ipython.scipy.org)
- Eclipse w/ pydev
21Testimonials/Users
- Google
- YouTube
- LLNL, ANL, LBL, LANL
-
-
- May/June 2007 issue of Computing in Science
Engineering (CiSE)
22Useful Python pkgs
- Data/Analysis
- DBs mysql-python
- Numerics NumPy
- RPy R from Python
- Storage (HDF5) PyTables
- Visualization
- SciVis VTK (Python bindings)
- Plotting matplotlib
- Graphs bgl-python
23Useful Python pkgs (cont)
- Science
- Chemistry UCSF Chimera, PyMOL, VMD
- Bioinfo BioPython
- Physics PyROOT
- Imaging ITK (Python bindings)
- www.scipy.org/Topical_Software
- www.python.org/about/apps
24Useful Python pkgs (cont)
- Infrastructure
- Web dev django, TurboGears
- Web Services ZSI, xml
- HPC pyMPI, MYMPI, pp
-
(parallelpython.com) - Grid pyGlobus, pyGridWare
- Star-P Python client
- www.interactivesupercomputing.com/starpexpres
s/042007/8_Python_Users.html
25matplotlib
- gtgtgt from pylab import
- gtgtgt def my_func(t)
- s1 cos(2pit)
- e1 exp(-t)
- return s1e1
- gtgtgt tvals arange(0., 5., 0.1)
- gtgtgt plot(tvals, my_func(tvals))
- ltmatplotlib.lines.Line2D object at 0x16ca9d50gt
- gtgtgt show()
- gtgtgt plot(tvals, my_func(tvals), 'bo', tvals,
my_func(tvals), 'k') - gtgtgt show()
26Python-related projects at IU
lifescienceweb.org SDA Lab Mooney Lab
27chembiogrid.org
28CompuCell3D
James Glazier, Physics Simulate cellular
behavior
29NLOPredict Simpson Lab SDA Lab
- UCSF Chimera (molecular viewer)
-
- NumPy
-
- matplotlib
30VisPort server
ZSI Python-wrapped VTK PyTables pyGlobus