Python Another Handout
Transcript of Python Another Handout
-
7/30/2019 Python Another Handout
1/49
Introduction Python Tutorial Numerics & Plotting Standard library
An introduction to the Python programming
language
Prabhu Ramachandran
Department of Aerospace Engineering
IIT Bombay
16, July 2007
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard library
Outline
1 Introduction
Introduction to Python
2 Python Tutorial
Preliminaries
Data types
Control flow, functions
Modules, exceptions, classes
Miscellaneous
3 Numerics & Plotting
NumPy Arrays
Plotting: Matplotlib
SciPy4 Standard library
Quick Tour
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
2/49
Introduction Python Tutorial Numerics & Plotting Standard library
Introduction to Python
Introduction
Creator and BDFL: Guido van RossumBDFL == Benevolent Dictator For Life
Conceived in December 1989
The name Python: Monty Pythons Flying Circus
Current stable version of Python is 2.5.x
PSF license (like BSD: no strings attached)
Highly cross platform
Runs on the Nokia series 60!
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryIntroduction to Python
Resources
Available as part of any sane GNU/Linux distribution
Web: http://www.python.org
Documentation: http://www.python.org/doc
Free Tutorials:
Official Python tutorial:
http://docs.python.org/tut/tut.html
Byte of Python: http://www.byteofpython.info/
Dive into Python: http://diveintopython.org/
Prabhu Ramachandran Introduction to Python
http://www.python.org/http://www.python.org/dochttp://docs.python.org/tut/tut.htmlhttp://www.python.org/http://www.python.org/dochttp://docs.python.org/tut/tut.htmlhttp://www.byteofpython.info/http://diveintopython.org/http://diveintopython.org/http://www.byteofpython.info/http://docs.python.org/tut/tut.htmlhttp://www.python.org/dochttp://www.python.org/ -
7/30/2019 Python Another Handout
3/49
Introduction Python Tutorial Numerics & Plotting Standard library
Introduction to Python
Why Python?
High level, interpreted, modular, OO
Easy to learn
Easy to read code
Much faster development cycle
Powerful interactive interpreter
Rapid application development
Powerful standard library
Interfaces well to C++, C and FORTRAN libraries
In short: there is little you cant do with it
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryIntroduction to Python
A quote
I came across Python and its Numerical extension in
1998 . . . I quickly fell in love with Python programming
which is a remarkable statement to make about a
programming language. If I had not seen others with the
same view, I might have seriously doubted my sanity.
Travis Oliphant (creator of NumPy)
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
4/49
Introduction Python Tutorial Numerics & Plotting Standard library
Introduction to Python
Why not ***lab?
Open Source, Free
Portable
Python is a real programming language: large and small
programs
Can do much more than just array and math
Wrap large C++ codes
Build large code bases via SCons
Interactive data analysis/plotting
Parallel applicationJob scheduling on a custom cluster
Miscellaneous scripts
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryIntroduction to Python
Why not Python?
Can be slow for high-performance applications
This can be fairly easily overcome by using C/C++/FORTRAN
extensions
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
5/49
Introduction Python Tutorial Numerics & Plotting Standard library
Introduction to Python
Use cases
NASA: Python Streamlines Space Shuttle Mission DesignAstraZeneca Uses Python for Collaborative Drug Discovery
ForecastWatch.com Uses Python To Help Meteorologists
Industrial Light & Magic Runs on Python
Zope: Commercial grade CMS
RedHat: install scripts, sys-admin tools
Numerous success stories:
http://www.pythonology.com/success
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryIntroduction to Python
Before we begin
This is only an introduction
Python is a full-fledged programming language
Please read the tutorial
It is very well written and can be read in one afternoon
Prabhu Ramachandran Introduction to Python
http://www.pythonology.com/successhttp://www.pythonology.com/success -
7/30/2019 Python Another Handout
6/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Preliminaries: The Interpreter
Python is interpretedInterpreted vs. Compiled
Interpreted languages allow for rapid testing/prototyping
Dynamically typed
Full introspection of code at runtime
Did I say dynamic?
Does not force OO or a particular programming paradigm
down your throat!
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Preliminaries . . .
Will follow the Official Python tutorial
No lexical blocking
Indentation specifies scope
Leads to easier to read code!
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
7/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Using the interpreter
Starting up: python or ipython
Quitting: Control-D or Control-Z (on Win32)
Can use it like a calculator
Can execute one-liners via the -c option: python -c
"print hello world"
Other options via python -h
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
IPython
Recommended interpreter, IPython:
http://ipython.scipy.org
Better than the default Python shell
Supports tab completion by defaultEasier object introspection
Shell access!
Command system to allow extending its own behavior
Supports history (across sessions) and logging
Can be embedded in your own Python code
Support for macros
A flexible framework for your own custom interpreter
Other miscellaneous conveniences
Well get back to this later
Prabhu Ramachandran Introduction to Python
http://ipython.scipy.org/http://ipython.scipy.org/ -
7/30/2019 Python Another Handout
8/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Basic IPython features
Startup: ipython [options] files
ipython [-wthread|-gthread|-qthread]:
Threading modes to support wxPython, pyGTK and Qt
ipython -pylab: Support for matplotlib
TAB completion:
Type object_name. to see list of options
Also completes on file and directory names
object? shows docstring/help for any Python object
object?? presents more docs (and source if possible)
Debugging with %pdb magic: pops up pdb on errors
Access history (saved over earlier sessions also)Use : move up history
Use string: search history backwards
Use Esc >: get back to end of history
%run [options] file[.py] lets you run Python code
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Basic concepts
Dynamically typed
Assignments need not specify a type
a = 1a = 1. 1
a = " foo "
a = SomeClass ( )
Comments:
a = 1 # Inl i n e comments
# Comment i n a l i n e t o i t s e l f .
a = " # T hi s i s n o t a comment ! "
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
9/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Basic concepts
No lexical scoping
Scope determined by indentation
f o r i i n r an ge ( 1 0 ) :
p r i n t " i n s i d e l o op : " ,
# Do s ome th in g i n t h e l o op .
p r i n t i , i i
p r i n t " l o op i s done ! " # T hi s i s o ut si de t h e l oo p !
Assignment to an object is by referenceEssentially, names are bound to objects
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Basic types
Basic objects: numbers (float, int, long, complex), strings,
tuples, lists, dictionaries, functions, classes, types etc.Types are of two kinds: mutable and immutable
Immutable types: numbers, strings, None and tuples
Immutables cannot be changed in-place
Mutable types: lists, dictionaries, instances, etc.
Mutable objects can be changed
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
10/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
What is an object?
A loose and informal but handy descriptionAn object is a particular instance of a general class of things
Real world example
Consider the class of cars made by Honda
A Honda Accord on the road, is a particular instance of the
general class of cars
It is an object in the general sense of the term
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Objects in a programming language
The object in the computer follows a similar idea
An object has attributes (or state) and behavior
Object contains or manages data (attributes) and hasmethods (behavior)
Together this lets one create representation of real things on
the computer
Programmers then create objects and manipulate them
through their methods to get things done
In Python everything is essentially an object you dont have
to worry about it
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
11/49
-
7/30/2019 Python Another Handout
12/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Strings
s = t h i s i s a s t r i n g s = T hi s one has " q uo te s " i n s i d e !
s = " The r ev er se w i t h s i n g l eq uo te s i n s i d e ! "
l = " A l on g s t r i n g spanning s ev er al l i n e s \
one more l i n e \
y e t a n o th e r "
t = " " " A t r i p l e quoted s t r i n g
does n o t need t o be escaped a t t h e end and
" can have n e st e d q u ot e s " and whatnot . " " "
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Strings
>>> word = " h e l l o "
>>> 2word + " w o rl d " # A r i t h me t i c on s t r i n g s
h e l l o h e l l o wo rl d
>>> p r i n t word [ 0 ] + word [ 2 ] + word [1]
h l o>>> # S t r i n g s are " immutable "
. . . word [ 0 ] = H
Traceback ( most r ec en t c a l l l a s t ) :
F i l e " < s t d i n > " , l i n e 1 , i n ?
T yp eE rr or : o b j e c t d oe sn t s u pp o r t i t em a ss ig nm en t
>>> len ( word ) # The l en gt h o f t h e s t r i n g
5> > > s = u Unicode s t r i n g s ! # unicode s t r i n g
>>> # Raw s t r i n g s ( no te t he l e ad i ng r )
. . . raw_s = r A \ LaTeX s t r i n g $ \ a lp ha \ nu$
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
13/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
More on strings> > > a = h e l l o w or l d
>>> a . s t a r t s w i t h ( h e l l )
True
>>> a . endswith ( ld )
True>>> a . upper ( )
HELLO WORLD
>>> a . upper ( ) . lower ( )
h e l l o w or l d
>>> a . s p l i t ( )
[ h e l l o , world ]
>>> . j o i n ( [ a , b , c ] )
abc
>>> x , y = 1 , 1. 23 4>>> x i s %s , y i s %s %(x , y )
x i s 1 , y i s 1.234
>>> # c o ul d a ls o do : x i s %d , y i s %f %( x , y )
See http://docs.python.org/lib/typesseq-strings.html
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Lists
Lists are mutable
Items are indexed at 0
Last element is -1
Length: len(list)
Prabhu Ramachandran Introduction to Python
http://docs.python.org/lib/typesseq-strings.htmlhttp://docs.python.org/lib/typesseq-strings.html -
7/30/2019 Python Another Handout
14/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
List: examples
>>> a = [ spam , eggs , 100 , 12 34 ]
> > > a [ 0 ]
spam > > > a [ 3 ]
1234
>>> a[2]
100
> > > a[ 1:1 ]
[ eggs , 100 ]
>>> a [ : 2 ] + [ bacon , 2
2][ spam , eggs , bacon , 4 ]
>>> 2a [ : 3 ] + [ Boe! ]
[ spam , eggs , 100 , spam , eggs , 100 , Boe! ]
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Lists are mutable
>>> a = [ spam , eggs , 100 , 12 34 ]
>>> a [ 2 ] = a [ 2 ] + 23
>>> a[ spam , eggs , 123 , 12 34 ]
>>> a = [ spam , eggs , 100 , 12 34 ]
>>> a [ 0 : 2 ] = [ 1 , 1 2] # Replace some i t ems
>>> a
[ 1 , 12 , 123 , 1234]
>>> a [ 0 : 2 ] = [ ] # Remove some it em s
>>> a
[ 1 23 , 12 34 ]
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
15/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
List methods
>>> a = [ spam , eggs , 100 , 12 34 ]
>>> l e n ( a )4
>>> a . revers e ( )
>>> a
[ 12 34 , 100 , eggs , spam ]
>>> a . append ( [ x , 1 ] ) # L i s t s can c o n t a i n l i s t s .
>>> a
[ 12 34 , 100 , eggs , spam , [ x , 1 ] ]
>>> a . e x te n d ( [ 1 , 2 ] ) # E xt en d t h e l i s t .>>> a
[ 12 34 , 100 , eggs , spam , [ x , 1 ] , 1 , 2 ]
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Tuples
>>> t = ( 0 , 1 , 2 )
>>> p r i n t t [ 0 ] , t [ 1 ] , t [ 2 ] , t [
1 ] , t [
2 ] , t [
3]0 1 2 2 1 0
>>> # T u pl e s a r e i mm ut ab le !
. . . t [ 0 ] = 1
Traceback ( most r ec en t c a l l l a s t ) :
F i l e " < s t d i n > " , l i n e 1 , i n ?
T y pe E rr o r : o b j e c t doesn t s u pp o r t i t em a ss ig nm en t
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
16/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Dictionaries
Associative arrays/mappings
Indexed by keys (keys must be immutable)
dict[key] = value
keys() returns all keys of the dict
values() returns the values of the dict
has_key(key) returns if key is in the dict
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Dictionaries: example
> > > t e l = { ja ck : 4098 , sape : 41 39}
>>> t e l [ g u id o ] = 4127
>>> t e l
{ sape : 4139 , g u id o : 4127 , ja ck : 40 98}>>> t e l [ ja ck ]
4098
>>> del t e l [ sape ]
>>> t e l [ i r v ] = 4127
>>> t e l
{ g u id o : 4127 , i r v : 4127 , ja ck : 40 98}
>>> t e l . keys ( )
[ g u id o , i r v , ja ck ]>>> t e l . h as_ ke y ( g u id o )
True
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
17/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Control flow
Control flow is primarily achieved using the following:
if/elif/else
for
while
break, continue, else may be used to further control
pass can be used when syntactically necessary but nothing
needs to be done
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
If example
x = i n t ( r a w_ i n pu t ( " Please e n te r an i n t e g e r : " ) )
# r a w_ in pu t asks t h e user f o r i n p u t .
# i n t ( ) t y p e c a s t s t h e r e s u l t i n g s t r i n g i n t o an i n t .
i f x < 0 :
x = 0
p r i n t N e ga t iv e changed t o z er o
e l i f x = = 0 :
p r i n t Zero
e l i f x = = 1 :
p r i n t Sin gle
else :p r i n t More
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
18/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
If example
>>> a = [ ca t , window , d e f e n e s t r a t e ]
>>> i f ca t i n a :
. . . p r i n t "meaw"
. . .
meaw
>>> p et s = { ca t : 1 , dog : 2 , cro c : 10}
>>> i f cro c i n pets :
. . . p r i n t pets [ cro c ]
. . .10
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
for example
>>> a = [ ca t , window , d e f e n e s t r a t e ]
>>> f o r x i n a :
. . . p r i n t x , l en ( x )
. . .c a t 3
window 6
d e f e n e st r a t e 12
> > > k n i g h t s = { gal lah ad : t h e p ur e ,
. . . r o b i n : t he brave }
>>> f o r k , v i n k n i g ht s . i t e r i t e m s ( ) :
. . . p r i n t k , v
. . .g a ll a ha d t he pure
r o b i n t he brave
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
19/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
for and range example
>>> f o r i i n r an ge ( 5 ) :
. . . p r i n t i , i i
. . .
0 0
1 1
2 4
3 9
4 16
>>> a = [ a , b , c ]
>>> f o r i , x i n e n u m e r a t e ( a ) :
. . . p r i n t i , x
. . .
0 a
1 b
2 c
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
while example
>>> # F ib on ac ci s e r i e s :
. . . # t he sum o f two el emen ts d e f i ne s t he n ex t
. . . a , b = 0 , 1
>>> while b < 1 0 :. . . p r i n t b
. . . a , b = b , a+b
. . .
1
1
2
3
5
8
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
20/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Functions
Support default and keyword argumentsScope of variables in the function is local
Mutable items are passed by reference
First line after definition may be a documentation string
(recommended!)
Function definition and execution defines a name bound to the
function
You canassign a variable to a function!
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Functions: examples
>>> def f i b ( n ) : # w r i t e F i b o n a c c i s er ie s up t o n
. . . " " " P r i n t a F ib on ac ci s e r i e s up t o n . " " "
. . . a , b = 0 , 1
. . . while b < n :
. . . p r i n t b ,
. . . a , b = b , a+b
. . .
>>> # Now c a l l t h e f u n c t i on we j u s t d ef in ed :
. . . f i b ( 2 00 0 )
1 1 2 3 5 8 1 3 2 1 3 4 5 5 8 9 1 4 4 2 3 3 3 7 7 6 1 0 9 8 7 1 5 9 7
> > > f = f i b # Assign a v a r i ab l e t o a f u n c ti o n
>>> f (1 0)
1 1 2 3 5 8
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
21/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Functions: default arguments
def ask_ok ( prompt , r e t r i e s =4 , c o m pl a i n t = Yes o r no ! ) :
while True :ok = r a w _ i np u t ( p ro mp t )
i f ok i n ( y , ye , yes ) :
r et u r n True
i f ok i n ( n , no , nop , nope ) :
r et u r n False
r e t r i e s = r e t r i e s 1
i f r e t r i e s < 0 :
r a i s e I OError , bad u s e r
p r i n t c o m p l a i n t
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Functions: keyword arguments
def p a r r o t ( v o lt a g e , s t a t e = a s t i f f ,
a c t i o n = voom , t yp e= Norwegian Blue ) :
p r i n t "
T h i s p a r r o t woul dn t " , a ct io n ,p r i n t " i f you p u t " , v ol ta g e , " V o l t s t h ro u gh i t . "
p r i n t " Lovely plumage , t he " , t yp e
p r i n t " I t s " , s ta te , " ! "
p a r r o t (1 0 0 0)
p a r r o t ( a c t i o n = VOOOOOM , v o l t a g e = 1000000)
p a r r o t ( a t housand , s t a t e = p us hi ng up t he d a i s i e s )
p a r r o t ( a m i l l i o n , b e r e f t o f l i f e , jump )
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
22/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Modules
Define variables, functions and classes in a file with a .py
extension
This file becomes a module!Modules are searched in the following:
Current directory
Standard: /usr/lib/python2.3/site-packages/ etc.
Directories specified in PYTHONPATH
sys.path: current path settings (from the sys module)
The import keyword loads a module
One can also use:
from module import name1, name2, name2
where name1 etc. are names in the module, module
from module import * imports everything from
module, use only in interactive mode
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Modules: example
# f oo . py
some_var = 1
def f i b ( n ) : # w r i t e F i b o n a c c i s er ie s up t o n
" " " P r i n t a F ib on ac ci s e r i e s up t o n . " " "
a , b = 0 , 1while b < n :
p r i n t b ,
a , b = b , a+b
# EOF
>>> import f o o
>>> f o o . f i b ( 1 0 )1 1 2 3 5 8
>>> fo o . some_var
1
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
23/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Namespaces
A mapping from names to objects
Modules introduce a namespace
So do classes
The running scripts namespace is __main__
A modules namespace is identified by its name
The standard functions (like len) are in the __builtin__
namespace
Namespaces help organize different names and their bindingsto different objects
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Exceptions
Pythons way of notifying you of errorsSeveral standard exceptions: SyntaxError, IOError etc.
Users can also raise errors
Users can create their own exceptions
Exceptions can be caught via try/except blocks
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
24/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Exception: examples
>>> 10 ( 1 / 0 )
Traceback ( most r ec en t c a l l l a s t ) :F i l e " < s t d i n > " , l i n e 1 , i n ?
Z e ro D iv i s io n Er r o r : i n t e g e r d i v i s i o n or modulo by zero
>>> 4 + spam3
Traceback ( most r ec en t c a l l l a s t ) :
F i l e " < s t d i n > " , l i n e 1 , i n ?
NameError : name spam i s not d e f i n e d
>>> 2 + 2
Traceback ( most r ec en t c a l l l a s t ) :F i l e " < s t d i n > " , l i n e 1 , i n ?
T y p e Er r o r : c a nn o t c o n c at e n a te s t r and i n t o b j e c t s
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Exception: examples
>>> while True :
. . . t r y :
. . . x = i n t ( ra w_ i n p u t ( " E n t er a number : " ) )
. . . break
. . . except V a l u e E r r o r :
. . . p r i n t " I n v a l i d number , t r y a ga in . . . "
. . .
>>> # To r a i s e e xc ep ti on s
. . . r a i s e ValueError , " y ou r e r r o r message "
Traceback ( most r ec en t c a l l l a s t ) :
F i l e " < s t d i n > " , l i n e 2 , i n ?V a l u e E r r o r : y ou r e r r o r message
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
25/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Classes: the big picture
Lets you create new data types
Class is a template for an object belonging to that classNote: in Python a class is also an object
Instantiating a class creates an instance (an object)
An instance encapsulates the state (data) and behavior
(methods)
Allows you to define an inheritance hierarchy
A Honda car is a car.
A car is an automobile.A Python is a reptile.
Programmers need to think OO
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Classes: whats the big deal?
Lets you create objects that mimic a real problem being
simulated
Makes problem solving more natural and elegant
Easier to create code
Allows for code-reuse
Polymorphism
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
26/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Class definition and instantiation
Class definitions when executed create class objects
Instantiating the class object creates an instance of the class
class Foo ( o b j e c t ) :
pass
# c l as s o b j e c t c re at ed .
# C re at e an i n st a nc e o f Foo .
f = Foo ( )
# Can a s si gn an a t t r i b u t e t o t he i ns ta nc e
f . a = 100
p r i n t f . a
100
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Classes . . .
All attributes are accessed via the object.attribute
syntax
Both class and instance attributes are supported
Methodsrepresent the behavior of an object: crudely think of
them as functions belonging to the object
All methods in Python are virtual
Inheritance through subclassing
Multiple inheritance is supported
No special public and private attributes: only goodconventions
object.public(): public
object._private() & object.__priv(): non-public
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
27/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Classes: examples
class MyClass( ob je ct ) :
" " " Example c l a ss ( t h i s i s t he c l a ss d o c s t r i n g ) . " " "
i = 12345 # A c la s s a t t r i b u t e def f ( s e l f ) :
" " " T h i s i s t h e method d o c s t r i n g " " "
r et u r n h e l l o w or ld
>>> a = MyClass ( ) # c re at es an i n st an c e
>>> a . f ( )
h e l l o w or ld
>>> # a . f ( ) i s e q u i v al e n t t o MyClass . f ( a ). . . # T hi s a ls o e x pl a i ns why f has a s e l f argument .
. . . MyClass . f ( a )
h e l l o w or ld
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Classes (continued)
self is conventionally the first argument for a method
In previous example, a.f is a method object
When a.f is called, it is passed the instance a as the first
argument
If a method called __init__exists, it is called when the
object is created
If a method called __del__exists, it is called before the
object is garbage collected
Instance attributes are set by simply setting them in self
Other special methods (by convention) like __add__ let you
define numeric types:http://docs.python.org/ref/specialnames.html
http://docs.python.org/ref/numeric-types.html
Prabhu Ramachandran Introduction to Python
http://docs.python.org/ref/specialnames.htmlhttp://docs.python.org/ref/numeric-types.htmlhttp://docs.python.org/ref/specialnames.htmlhttp://docs.python.org/ref/numeric-types.htmlhttp://docs.python.org/ref/numeric-types.htmlhttp://docs.python.org/ref/specialnames.html -
7/30/2019 Python Another Handout
28/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Classes: examples
class Bag ( MyClass ) : # Shows how t o d e r i v e c l a s s e s
def _ _ i n i t _ _ ( s e l f ) : # c a l l e d on o b j e c t c r ea t i o n .
s e l f . data = [ ] # an i ns ta n ce a t t r i b u t e def add ( s e l f , x ) :
s e l f . dat a . append ( x )
def a d dt wi c e ( s e l f , x ) :
s e l f . add ( x )
s e l f . add ( x )
> > > a = B a g ( )
>>> a . f ( ) # I n h e r i t e d method
h e l l o w or ld >>> a . add ( 1 ) ; a . a dd tw ic e ( 2 )
>>> a . data
[ 1 , 2 , 2 ]
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Derived classes
Call the parents __init__ if needed
If you dont need a new constructor, no need to define it in
subclass
Can also use the super built-in function
class AnotherBag(Bag):
def _ _ i n i t _ _ ( s e l f ) :
# Must c a l l p a r e n t s _ _ i n i t _ _ e x p l i c i t l y
Bag . _ _ i n i t _ _ ( s e l f )
# A l t e r n a t i v e l y use t h i s :
s up er ( AnotherBag , s e l f ) . _ _ i n i t _ _ ( )
# Now s et up any more dat a .
s e l f . more_data = [ ]
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
29/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Classes: polymorphism
class Drawable ( ob je ct ) :
def draw ( s e l f ) :
# J u s t a s p e c i f i c a t i o n .
pass
class Square ( Drawable ) :
def draw ( s e l f ) :
# draw a s qu ar e .
class C i r c l e ( D ra wa bl e ) :
def draw ( s e l f ) :
# draw a c i r c l e .
class A r t i s t ( Drawa ble ) :
def draw ( s e l f ) :
f o r o b j i n s e l f . drawables :
ob j . draw ( )
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Stand-alone scripts
Consider a file f.py:
# ! / us r / bin / env py t hon
" " " Module l e v e l document ation . " " "
# F i r s t l i n e t e l l s t h e s h e l l t h a t i t should use P ython # t o i n t e r p r e t t h e code i n t h e f i l e .
def f ( ) :
p r i n t " f "
# Check i f we a re r u nn i n g s ta n da l on e o r as module .
# When impor t ed , __name__ w i l l not be __main__
i f __name__ == __main__ :
# T hi s i s n ot exe cuted when f . py i s i mp or te d .
f ( )
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
30/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
More IPython features
Input and output caching:
In: a list of all entered input
Out: a dict of all output
_, __, __are the last three results as is _N
%hist [-n] macro shows previous history, -n suppresses
line number information
Log the session using %logstart, %logon and %logoff
%run [options] file[.py] running Python code
%run -d [-b]: debug script with pdb N is the line
number to break at (defaults to 1)
%run -t: time the script
%run -p: Profile the script
%prun runs a statement/expression under the profiler
%macro [options] macro_name n1-n2 n3-n4 n6
save specified lines to a macro with name macro_name
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
More IPython features . . .
%edit [options] [args]: edit lines of code or file
specified in editor (configure editor via $EDITOR)
%cd changes directory, see also
%pushd, %popd, %dhist
Shell access
!command runs a shell command and returns its output
files = %sx ls or files = !ls sets files to all
result of the ls command
%sx is quiet
!ls $files passes the files variable to the shell
command
%alias alias_name cmd creates an alias for a system
command
%colors lets you change the color scheme to
NoColor, Linux, LightBG
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
31/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
More IPython features . . .
Use ; at the end of a statement to suppress printing output
%who, %whos: print information on variables%save [options] filename n1-n2 n3-n4: save
lines to a file
%time statement: Time execution of a Python statement
or expression
%timeit [-n -r [-t|-c]] statement: time
execution using Pythons timeit module
Can define and use profiles to setup IPython differently:math, scipy, numeric, pysh etc.
%magic: Show help on all magics
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
File handling
>>> # Reading f i l e s :
. . . f = open ( / path / to / f i le _n am e )
>>> d a ta = f . r ea d ( ) # Read e n t i r e f i l e .
>>> l i n e = f . r e a d l i n e ( ) # Read one l i n e .
>>> # Read e n t i r e f i l e appending each l i n e i n t o a l i s t
. . . l i n e s = f . r e a d l i ne s ( )
>>> f . c los e ( ) # c l os e t he f i l e .
>>> # W r it in g f i l e s :
. . . f = open ( / path / to / f i le _n am e , w )
>>> f . w r i t e ( h e l l o w o rl d \ n )
tell(): returns int of current positionseek(pos): moves current position to specified byte
Call close() when done using a file
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
32/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Math
math module provides basic math routines for floats
cmath module provides math routies for complex numbers
random: provides pseudo-random number generators for
various distributions
These are always available and part of the standard library
More serious math is provided by the NumPy/SciPy modules
these are not standard and need to be installed separately
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryPreliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Timing and profiling
Timing code: use the time module
Read up on time.time() and time.clock()
timeit: is a better way of doing timing
IPython has handy time and timeit macros (type
timeit? for help)
IPython lets you debug and profile code via the run macro
(type run? on the prompt to learn more)
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
33/49
Introduction Python Tutorial Numerics & Plotting Standard library
Preliminaries Data types Control flow, functions Modules, exceptions, classes Miscellaneous
Odds and ends
dir([object]) function: attributes of given object
type(object): returns type informationstr(), repr(): convert object to string representation
isinstance, issubclass
assert statements let you do debugging assertions in code
csv module: reading and writing CSV files
pickle: lets you save and load Python objects (serialization)
os.path: common path manipulationsCheck out the Python Library reference:
http://docs.python.org/lib/lib.html
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
The numpy module
Manipulating large Python lists for scientific computing is slow
Most complex computations can be reduced to a few standard
operationsThe numpy module provides:
An efficient and powerful array type for various common data
types
Abstracts out the most commonly used standard operations on
arrays
Numeric was the first, then came numarray. numpy is the
latest and is the future
This course uses numpy and only covers the absolute basics
Prabhu Ramachandran Introduction to Python
http://docs.python.org/lib/lib.htmlhttp://docs.python.org/lib/lib.htmlhttp://docs.python.org/lib/lib.htmlhttp://docs.python.org/lib/lib.htmlhttp://docs.python.org/lib/lib.html -
7/30/2019 Python Another Handout
34/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
Basic concepts
numpy arrays are of a fixed size (arr.size) and have the
same type (arr.dtype)
numpy arrays may have arbitrary dimensionalityThe shape of an array is the extent (length) of the array along
each dimension
The rank(arr) of an array is the dimensionality of the
array
The arr.itemsize is the number of bytes (8-bits) used for
each element of the array
Note: The shape and rank may change as long as the
size of the array is fixed
Note: len(arr) != arr.size in general
Note: By default array operations are performed elementwise
Indices start from 0
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
Examples of numpy
# S im pl e a r r a y math exampl e
>>> from numpy import
>>> a = a r r a y ( [ 1 , 2 , 3 , 4 ] )
>>> b = a r r a y ( [ 2 , 3 , 4 , 5 ] )
> > > a + b # Element w ise a d d i t i o n ! a rr ay ( [ 3 , 5 , 7 , 9 ] )
>>> p r i n t p i , e # P i and e are d ef in e d .
3.14159265359 2.71828182846
# Create a rr ay from 0 t o 10
>>> x = arange ( 0 . 0 , 1 0 .0 , 0 . 0 5)
>>> x
= 2
pi / 10 # m u l t i p l y a rr ay by s c a l ar v a l ue a r ra y ( [ 0 . , 0 . 0 3 1 4 , . . . , 6 . 2 5 2 ] )
# a pp ly f u nc t io n s t o a rr ay .
>>> y = s i n ( x )
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
35/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
More examples of numpy
# S iz e , shape , r an k , t y p e e t c .
>>> x = a rr ay ( [ 1 . , 2 , 3 , 4 ] )
>>> s i z e ( x )
4>>> x . dtype # o r x . d t yp e . c ha r
d
>>> x . shape
( 4 , )
>>> p r i n t r an k ( x ) , x . i t e m s i z e
1 8
>>> x . t o l i s t ( )
[ 1 . 0 , 2 .0 , 3 . 0 , 4 . 0 ]
# A rr ay i n de x in g
>>> x [ 0 ] = 10
>>> p r i n t x [ 0 ] , x [1]
1 0. 0 4 .0Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
Multi-dimensional arrays
>>> a = a rr ay ( [ [ 0 , 1 , 2 , 3 ] ,
. . . [ 1 0 , 1 1 , 1 2 , 1 3 ] ] )
>>> a . shape # ( rows , c olu mn s )
( 2 , 4 )
# Acc ess in g and s e t t i n g v al ue s
>>> a [ 1 , 3 ]
13
>>> a [ 1 , 3 ] = 1
> > > a [ 1 ] # The second row
ar ra y ([1 0 ,1 1 ,12 ,1 ] )
# F l a t t e n / r a v e l a rr a ys t o 1D a rr a ys >>> a . f l a t # o r r a v e l ( a )
ar ra y ([ 0 ,1 ,2 ,3 ,10 ,11 ,12 ,1 ] )
# Note : f l a t r ef er en ce s o r i g i n a l memory
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
36/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
Slicing arrays
>>> a = a r r ay ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )
>>> a [ 0 , 1 : 3 ]
a r ra y ( [ 2 , 3 ] )>>> a [ 1 : , 1 : ]
a r ra y ( [ [ 5 , 6 ] ,
[ 8 , 9 ] ] )
>>> a [ : , 2 ]
a rr ay ( [ 3 , 6 , 9 ] )
# S t r i d i n g . . .
>>> a [ 0 : : 2 , 0 : : 2 ]a r ra y ( [ [ 1 , 3 ] ,
[ 7 , 9 ] ] )
# A l l t he se s l i c e s a re r e fe r e nc e s t o t he same memory !
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
Array creation functions
array(object, dtype=None,
copy=1,order=None, subok=0,ndmin=0)
arange(start, stop=None, step=1,
dtype=None)
linspace(start, stop, num=50,
endpoint=True, retstep=False)
ones(shape, dtype=None, order=C)
zeros((d1,...,dn),dtype=float,order=C)
identity(n)
empty((d1,...,dn),dtype=float,order=C)
ones_like(x), zeros_like(x), empty_like(x)
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
37/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
Array math
Basic elementwise math (given two arrays a, b):
a + b add(a, b)
a - b , subtract(a, b)
a * b, multiply(a, b)
a / b , divide(a, b)
a % b , remainder(a, b)
a ** b, power(a, b)
Inplace operators: a + = b, or add(a, b, a) etc.
Logical operations: equal (==), not_equal (!=),
less () etc.
Trig and other functions:sin(x), arcsin(x),
sinh(x), exp(x), sqrt(x) etc.
sum(x, axis=0), product(x, axis=0): sum and
product of array elements
dot(a, b)
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
Advanced
Only scratched the surface of numpy
Ufunc methods: reduce, accumulate, outer,
reduceat
Typecasting
More functions: take, choose, where, compress,
concatenate
Array broadcasting and None
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
38/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
About matplotlib
Easy to use, scriptable, Matlab-like 2D plotting
Publication quality figures and interactive capabilitiesPlots, histograms, power spectra, bar charts, errorcharts,
scatterplots, etc.
Also does polar plots, maps, contours
Support for simple TEX markup
Multiple output backends (images, EPS, SVG, wx, Agg, Tk,
GTK)
Cross-platform: Linux, Win32, Mac OS X
Good idea to use via IPython: ipython -pylab
From scripts use: import pylab
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
More information
More information here: http://matplotlib.sf.net
http://matplotlib.sf.net/tutorial.html
http://matplotlib.sf.net/screenshots.html
Prabhu Ramachandran Introduction to Python
http://matplotlib.sf.net/tutorial.htmlhttp://matplotlib.sf.net/screenshots.htmlhttp://matplotlib.sf.net/screenshots.htmlhttp://matplotlib.sf.net/screenshots.htmlhttp://matplotlib.sf.net/http://matplotlib.sf.net/tutorial.htmlhttp://matplotlib.sf.net/screenshots.htmlhttp://matplotlib.sf.net/screenshots.htmlhttp://matplotlib.sf.net/tutorial.htmlhttp://matplotlib.sf.net/ -
7/30/2019 Python Another Handout
39/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
Basic plotting with matplotlib
>>> x = arange ( 0 , 2p i , 0 . 05 )
>>> p l o t ( x , s i n ( x ) ) # Same as p l o t ( x , s i n ( x ) , b
)>>> p l o t ( x , s i n ( x ) , ro )
>>> axi s ( [ 0 , 2 p i , 1 , 1 ] )
>>> x l a b e l ( r $ \ c h i $ , c o l o r = g )
>>> y l a b e l ( r s i n ( $ \ chi $ ) , c o l o r = r )
>>> t i t l e ( A s i mpl e f i g u r e , f o n t s i z e =20)
>>> s a v e f i g ( / tmp / t e s t . eps )
# M u l t i p l e p l o t s i n one f i g u r e
>>> t = arange ( 0 . 0 , 5 . 2 , 0 . 2 )# r ed dashes , b l ue sq ua re s and gr een t r i a n g l e s
>>> p l o t ( t , t , r , t , t 2 , bs , t , t 3 , g^ )
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
Basic plotting . . .
# Set p r op e r t i e s o f o bj e c ts :
>>> p l o t ( x , s i n ( x ) , l i n e w i d t h = 2. 0 , c o l o r = r )
>>> l , = p l o t ( x , s i n ( x ) )
>>> s et p ( l , l i n e w i d t h = 2. 0 , c o l o r = r )
>>> l . s e t _ l i n e w i d t h ( 2 . 0 ) ; l . s e t _ c o l o r ( r )
>>> draw ( ) # Redraws c u r r e n t f i g u r e .
>>> set p ( l ) # P r i n t s a v ai l a b l e p r o p e r t i e s
>>> c l o s e ( ) # Closes t he f i g u r e .
# M u l t i p l e f i g u r e s :
>>> f i g u r e ( 1 ) ; p l o t ( x , s i n ( x ) )
>>> f i g u r e ( 2 ) ; p l o t ( x , tanh ( x ) )>>> f i g u r e ( 1 ) ; t i t l e ( Easy as 1 , 2 , 3 )
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
40/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
Basic plotting . . .
>>> f i g u r e ( 1 )
>>> s u b p l o t ( 2 1 1 ) # Same as s u b p l o t ( 2 , 1 , 1 )
>>> p l o t ( x , cos ( 5x )exp(x ) )
>>> s u b p lo t ( 2 , 1 , 2 )
>>> p l o t ( x , cos ( 5x ) , r , l a b e l = cosi ne )
>>> p l o t ( x , s i n ( 5x ) , g , l a b e l = si ne )
>>> legend ( ) # Or l eg en d ( [ c o si n e , s i n e ] )
>>> t e x t (1 , 0 , (1 , 0) )
>>> axes = gca ( ) # C ur re nt a x i s >>> f i g = g cf ( ) # C u r r e n t f i g u r e
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
X-Y plot
Example code
t 1 = a ra nge ( 0 . 0 , 5 . 0 , 0 . 1 )t 2 = a ra ng e ( 0 . 0 , 5 . 0 , 0 . 0 2 )t 3 = a ra ng e ( 0 . 0 , 2 . 0 , 0 . 0 1 )su b p lo t (2 1 1 )p l o t ( t 1 , c os ( 2 p it 1 )exp( t1 ) , bo ,
t 2 , c os ( 2 p it 2 )exp( t2 ) , k )g r i d ( T r u e )t i t l e ( A t a l e o f 2 s ub pl ot s )y l a b e l ( Damped )su b p lo t (2 1 2 )p l o t ( t 3 , c os ( 2 p it3 ) , r )g r i d ( T r u e )x l a b e l ( t i m e ( s ) )y l a b e l ( Undamped )
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
41/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
Errorbar
Example code
t = a r ang e ( 0 . 1 , 4 , 0 . 1 )s = e x p ( t )e = 0 . 1a bs ( r a nd n ( l e n ( s ) ) )f = 0 . 1a bs ( r a nd n ( l e n ( s ) ) )g = 2eh = 2 fe r r o r b ar ( t , s , [ e , g ] , f , f m t= o )x l a b e l ( Dista n ce (m) )y l a b e l ( He ig h t (m) )t i t l e ( Mean an d s t a n d a rd e r r o r \
as a f u n c ti o n o f d is t an c e )
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
Semi-log and log-log plots
Example code
d t = 0 . 0 1
t = a ra ng e ( d t , 2 0 . 0 , d t )su b p lo t (3 1 1 )semilogy ( t , exp( t / 5 . 0 ) )y l a b e l ( se milo g y )g r i d ( T r u e )su b p lo t (3 1 2 )s e m i lo g x ( t , s i n ( 2 p i t ) )y l a b e l ( se milo g x )g r i d ( T r u e )# m i no r g r i d on t o o
g ca ( ) . xa xis . g r i d (Tru e , wh ich = min o r )su b p lo t (3 1 3 )l o g l o g ( t , 20exp( t / 1 0 . 0 ) , b as ex = 4 )g r i d ( T r u e )y l a b e l ( l o g l o g b as e 4 on x )
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
42/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
Histogram
Example code
mu , s ig ma = 1 0 0 , 15x = mu + sigmarandn(10000)# t h e h is t og ra m o f t he d at a
n , b i ns , p at c he s = h i s t ( x , 1 00 , normed = 1)# add a b es t f i t l i n e
y = n or mp df ( b i n s , mu , s ig ma )l = p l o t ( b i ns , y , r , l i n e w i d t h = 2)xl im (4 0 , 1 6 0 )x l a b e l ( Smarts )y l a b e l ( P )t i t l e ( r $ \rm{ IQ : } \ / \mu = 1 0 0 ,\ / \ s igma =1 5$ )
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
Bar charts
Example code
N = 5menMeans = ( 2 0 , 3 5 , 3 0 , 3 5 , 2 7 )menStd = ( 2 , 3 , 4 , 1 , 2 )
# t h e x l o c a ti o n s f o r t he gr ou ps i n d = a ra ng e ( N )# t h e w id th o f t he b ar s
w i d t h = 0 . 35p1 = bar ( ind , menMeans, width ,
c o l o r = r , ye r r= me nS td )womenMeans = (2 5 , 3 2 , 3 4 , 2 0 , 2 5 )womenStd = ( 3 , 5 , 2 , 3 , 3 )p2 = bar ( ind +width , womenMeans, width ,
c o l o r = y , ye rr =womenStd)y l a b e l ( Scores )t i t l e ( S core s b y g ro up a nd g e nd e r )x t i c k s ( i n d + w i dt h ,
( G1 , G2 , G3 , G4 , G5 ) )x l i m (width , le n ( ind ) )y t i c k s ( a r an ge ( 0 , 4 1 , 1 0 ) )l eg en d ( ( p1 [ 0 ] , p2 [ 0 ] ) ,
( Men , Women ) , shadow=True )
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
43/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
Pie charts
Example code
# m ake a s q ua r e f i g u r e an d a x es
f i g u r e ( 1 , f i g s i z e =( 8 , 8 ) )ax = ax es ( [ 0 . 1 , 0 . 1 , 0 . 8 , 0 . 8 ] )l a b e l s = Fro gs , Hogs , Dogs , Logs f r a cs = [ 1 5 , 3 0 , 45 , 1 0]e xp lo de = ( 0 , 0 . 05 , 0 , 0 )p i e ( f r a c s , e x pl o de = ex p lo d e , l a b e l s = l a b e l s ,
a u t o p c t = %1.1 f%% , shadow=True)t i t l e ( Raining Hogs and Dogs ,
bbox={ f a c e c o l o r : 0.8 , pad : 5 } )
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
Scatter plots
Example code
N = 3 0x = 0 . 9 ra n d (N)y = 0 . 9 ra n d (N)# 0 t o 10 p o in t r ad iu se s
a re a = p i(1 0 rand (N))2volume = 4 00 + ra n d (N)450sc a t te r (x , y , s= are a , ma rke r= o , c=volume ,
a lp h a = 0 .7 5 )x l a b e l ( r $ \ D e l t a _ i $ , s i ze = xl a r g e )y l a b e l ( r $ \ Delta_ { i +1}$ , s i ze = xl a r g e )t i t l e ( r Volume and perc ent change )g r i d ( T r u e )c o l o r b a r ( )s a v e f i g ( s c a t t e r )
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
44/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
Polar
Example code
f i g u r e ( f i g s i z e = ( 8 , 8 ) )a x = a xe s ( [ 0 . 1 , 0 . 1 , 0 . 8 , 0 . 8 ] , p o l a r =T ru e ,
a xisb g = #d5de9c )r = a ra ng e ( 0 , 1 , 0 . 0 0 1 )t h e t a = 22p irp o l a r ( t h e t a , r , c o l o r = #ee8d18 , l w =3 )# t he r ad iu s o f t he g r id l a be l s
s e t p ( a x . t h e t a g r i d l a b e l s , y = 1 . 07 5 )t i t l e ( r " $ \ t h e t a = 4 \ p i r " , f o n t s i z e = 20 )
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
Contours
Example code
x = a r a n g e (3 .0 , 3 . 0 , 0 . 0 25 )y = a r a n g e (2 .0 , 2 . 0 , 0 . 0 25 )X , Y = m es hg ri d ( x , y )
Z1 = b i v a ri a t e _n o r ma l ( X , Y , 1 . 0 , 1 . 0 , 0 . 0 , 0 . 0 )Z2 = b i v a ri a t e _n o r ma l ( X , Y , 1 . 5 , 0 . 5 , 1 , 1 )# d i f f e r e n c e o f G au ss ia ns
Z = 1 0 . 0 ( Z2 Z1 )im = imshow ( Z , i n t e r p o l a t i o n = b i l i n e a r ,
o r i g i n = lower ,cmap=cm. gray , ext ent =(3 , 3 ,2,2))
l e v e l s = a r an ge (1 .2 , 1 . 6 , 0 . 2 )# l a b e l e ve ry s ec ond l e v e l
c l a be l ( CS, l e v e l s [ 1 : : 2 ] , i n l i n e =1 ,f m t = %1.1f , f o n t s i z e = 14 )
CS = c o n to u r ( Z , l e v e l s ,o r i g i n = lower ,l i n e w i d t h s =2 ,e x t e n t = (3 , 3 ,2,2))
# make a c o l o rb a r f o r t h e c o nt o ur l i n e s CB = c o l o r b a r ( CS , s h r i n k = 0 . 8 , e x t en d = b o t h )t i t l e ( L i ne s w i t h c o l o r b a r )h ot ( ) ; f l a g ( )
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
45/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
Velocity vectors
Example code
X , Y = m e sh g ri d ( a ra ng e ( 0 , 2 p i , . 2 ) ,arange(0,2 p i , . 2 ) )
U = c o s ( X )V = s i n ( Y )Q = q ui ve r ( X[ : : 3 , : : 3 ] , Y [ : : 3 , : : 3 ] ,
U [ : : 3 , : : 3 ] , V [ : : 3 , : : 3 ] ,c o l o r = r , u n it s = x ,l i n e w i d t h s = ( 2 , ) ,e d g e co lo rs= ( k ) ,h e a d a x i s l e n g t h =5 )
qk = q u iv e rk e y ( Q, 0 . 5 , 0 . 03 , 1 , 1 m / s ,f o n t p r o p e r t i e s ={ weight : bold } )
a x i s ( [
1 , 7 ,
1, 7 ] )t i t l e ( t r i a n g u l a r head ; s c al e \
w i t h x v i ew ; b l a c k e dg es )
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryNumPy Arrays Plotting: Matplotlib SciPy
Maps
For details see http://matplotlib.sourceforge.net/screenshots/plotmap.py
Prabhu Ramachandran Introduction to Python
http://matplotlib.sourceforge.net/screenshots/plotmap.pyhttp://matplotlib.sourceforge.net/screenshots/plotmap.py -
7/30/2019 Python Another Handout
46/49
Introduction Python Tutorial Numerics & Plotting Standard library
NumPy Arrays Plotting: Matplotlib SciPy
Using SciPy
SciPy is Open Source software for mathematics, science, and
engineering
import scipy
Built on NumPy
Provides modules for statistics, optimization, integration, linear
algebra, Fourier transforms, signal and image processing,
genetic algorithms, ODE solvers, special functions, and more
Used widely by scientists world overDetails are beyond the scope of this tutorial
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryQuick Tour
Standard library
Very powerful
Batteries included
Example standard modules taken from the tutorial
Operating system interface: osSystem, Command line arguments: sys
Regular expressions: re
Math: math, random
Internet access: urllib2, smtplib
Data compression: zlib, gzip, bz2, zipfile, and
tarfile
Unit testing: doctest and unittest
And a whole lot more!Check out the Python Library reference:
http://docs.python.org/lib/lib.html
Prabhu Ramachandran Introduction to Python
http://docs.python.org/lib/lib.htmlhttp://docs.python.org/lib/lib.html -
7/30/2019 Python Another Handout
47/49
Introduction Python Tutorial Numerics & Plotting Standard library
Quick Tour
Stdlib: examples
>>> import os
>>> os . system ( d at e )F r i Jun 10 2 2 :1 3 :0 9 IST 2005
0
>>> os . getcwd ( )
/ home / pra bhu
>>> os . c h d i r ( / tmp )
>>> import os
>>> d i r ( os )
< r e t u r ns a l i s t o f a l l module f u n ct i o ns >>>> help ( os )
< ex t en siv e manual page from module s d o c st r i ng s >
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryQuick Tour
Stdlib: examples
>>> import sys
>>> # P r i n t t he l i s t o f command l i n e a rg s t o Python
. . . p r i n t sys . ar gv[ ]
>>> import r e # R eg ul ar e x p r es s i o ns
>>> r e . f i n d a l l ( r \ bf [ az ] ,
. . . which f o o t o r hand f e l l f a s t e s t )
[ f o o t , f e l l , f a s t e s t ]
>>> re . sub ( r ( \ b [ az ] + ) \ 1 , r \1 ,
. . . c at i n t h e t h e h at )
c at i n t h e h at
Prabhu Ramachandran Introduction to Python
-
7/30/2019 Python Another Handout
48/49
Introduction Python Tutorial Numerics & Plotting Standard library
Quick Tour
Stdlib: examples
>>> import math>>> math . cos ( math . p i / 4 . 0 )
0.70710678118654757
>>> math . l o g ( 1 0 2 4 , 2 )
10.0
>>> import random
>>> random . cho ice ( [ a p pl e , p ea r , banana ] )
p ea r
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryQuick Tour
Stdlib: examples
>>> import u r l l i b 2>>> f = u r l l i b 2 . u rl op en ( h t t p : / / www. pyt hon . or g / )
>>> p r i n t f . read (100 )
-
7/30/2019 Python Another Handout
49/49
Introduction Python Tutorial Numerics & Plotting Standard library
Quick Tour
Stdlib: examples
>>> import z l i b
> > > s = w i t c h wh ich has wh ich w i tc h e s w r i s t watch >>> l e n ( s )
41
>>> t = z l i b . compress ( s )
>>> le n ( t )
37
>>> z l i b . decompress ( t )
w i t c h wh ich has wh ich w i tc h e s w r i s t watch
>>> z l i b . crc32 ( t )
1438085031
Prabhu Ramachandran Introduction to Python
Introduction Python Tutorial Numerics & Plotting Standard libraryQuick Tour
Summary
Introduced Python
Basic syntax
Basic types and data structures
Control flow
Functions
Modules
Exceptions
Cl