Introduction to Pythonand Scientific Python
Francois Bianco
Unige
21th Dec 2011
Francois Bianco Introduction to Python
Outlook
1 Python features and syntax
2 Examples, pylab, and iPython
3 Flat files examples
Based on :
Learn Python in 10 minutes :http://www.poromenos.org/tutorials/python
Python documentation : http://docs.python.org/index.html
Matplotlib : http://matplotlib.sourceforge.net/
Scipy : http://www.scipy.org/
Francois Bianco Introduction to Python
Python is
Strongly typed (i.e. types are enforced)
Dynamically, implicitly typed (i.e. you don’t have to declarevariables)
Python is
case sensitive (i.e. var and VAR are two different variables)
object-oriented (i.e. everything is an object)
able to handle memory by itself (i.e has a garbadge collector)
Francois Bianco Introduction to Python
Example
Example
l i s t = [ 1 , 1 . 1 , 1 + 1 j , ’ 1 ’ , True ]f o r e l em en t i n l i s t :
p r i n t e lement , t y p e ( e l em en t ) , e l e me nt ==1.1
No mandatory statement termination character
Blocks are specified by indentation (4 spaces, or 1 tab)
Statements that expect an indentation level end in a colon “:”
Values are assigned (in fact, objects are bound to names) withthe equals sign “=”
Equality testing is done using two equals signs “==”
Francois Bianco Introduction to Python
Data structures
Three main data structures
list = [1,2,3,4,5,6] are mutable
tuples = (1,2,3,4,5,6) are unmutable
dictionary = { ’key’:’value’, ’answer’:42, ’obj’:list } also calledhash tables, access by key
List and tuples are access by index : list[index] (see array slicing).Dictionary by key dictionary[’answer’].
Francois Bianco Introduction to Python
Modules loading
Classes and functions are stored in modules
from math import s i n #on l y one f u n c t i o ns i n ( 0 . 3 )
import math #the whole module keep ing namespacemath . s i n ( 0 . 3 )
from math import * #the whole modulecos ( 0 . 3 )
import m a t p l o t l i b as mptl #rename namespacemptl . c o n v e r t e r ( )
Francois Bianco Introduction to Python
Other features of Python
Modern way of handling errors
t r y :f i l e O b j = open ( ’ f i l e N a m e . t x t ’ )
except I O E r r o r :p r i n t E r r o r M e s s a g e
Lambda functions
f i t f u n c = lambda x , y : s q r t ( x/ y )f i t f u n c ( 4 . 5 , 2 . 3 )
Francois Bianco Introduction to Python
Other features of Python
Classes, functions...
def funct ionName ( param , o p t i o n a l P a r a m=v a l u e ) : . . .c l a s s C h i l d C l a s s ( P a r e n t C l a s s ) : . . .
Automatic documentation generation (with Doxygen)
def t o g g l e I m a g e s ( s e l f , e v e n t ) :””” Toggle the two images a c co r d i n gto the t r i g g e r even t .
\param event Key or mouse even tt r i g g e r i n g the f u n c t i o n”””
Francois Bianco Introduction to Python
Flow control statements
Example: Fibonnaci in a simple while loop
a , b = 0 ,1whi le b<10:
p r i n t ba , b = b , a+b
Easy variables assignation, and permutation, no extra variableneeded.
Francois Bianco Introduction to Python
Array slicing
Example: access to specific elements in an array
x = a ran ge ( 1 0 ) #c r e a t e a v e c t o r from 0 to 9x #the whole v e c t o rx [ 0 ] #on l y the f i r s t e l ementx [ 3 ] #the 3 rd e l ementx [−2] #the second l a t e s t e l ementx [ 1 : 4 ] #element s from 1 to 4x [ : 5 ] #element s up to 5x [ −3 : ] #the t h r e e l a s t e l ement s
Francois Bianco Introduction to Python
Array masking
Example: create a mask on an array
a = a ra nge ( 1 0 )mask = ( ( a % 2) == 0)a [ mask ] = 0
This sets all the even value in a to 0.
Francois Bianco Introduction to Python
Easy plot
Example : plot with label and LATEX title
p l o t ( a ran ge ( 5 ) )x l a b e l ( ’ I n d e x ’ )y l a b e l ( ’Sum ’ )t i t l e ( r ’ $\ sum { i =0}ˆ\ i n f t y i $ ’ )
Francois Bianco Introduction to Python
Display matrix as an image
Example : create an image from a matrix
x = randn ( 2 0 , 2 0 ) #c r e a t e a random 20 x20 mat r i ximshow ( x ) #p i x e l s c a l eimshow ( x , e x t e n t = ( 0 , 1 , 0 , 1 ) ) #add custom s c a l e
Francois Bianco Introduction to Python
Histogramm plot
Example : create an histogramm plot
mu, s igma = 100 , 15x = mu + sigma * randn (10000)h i s t ( x , 1 0 0 )
Francois Bianco Introduction to Python
Many plots
Example : create two plots with legend
t = a ran ge ( 0 , 5 , 0 . 0 5 ) # Vect . 0 , 0 . 0 5 , 0 . 1 , . . . , 5s1=s i n (2* p i * t )s2=s1 * exp(− t )p l o t ( t , s1 , ’ g−−o ’ , t , s2 , ’ r : s ’ ) # custom s t y l e sl e g e n d ( ( ’ S i n wave ’ , ’ Damped exp . ’ ) )
Francois Bianco Introduction to Python
iPython
Usefull magic commands in iPython
h e l p ( o b j ) #Show he l po b j ? #Show doc s t r i n go b j ?? #Show sou r c e code
#r e t u r n l a s t v a l u e\%who #l i s t o b j e c t s\%whos #d e t a i l l e d o b j e c t s l i s t\% h i s t −n #h i s t o r y w i thout l i n e number\%exec I n [ 4 : 7 ] #redo l i n e 4 to 7\% e d i t 4 : 7 #ed i t l i n e 4 to 7 i n s c r i p t\%run #launch a s c r i p t\% p f i l e #show sou r c e f i l e con t en t
You want more of it ? Try %lsmagic
Francois Bianco Introduction to Python
Pro and cons
Cons
No GUI
Documentation spread on different websites
Requires basics programming skills
Pro
Easy to learn
Work on every plateform (WinXP,Vista,MacOS,Linux,...)
Could be bind to Gwyddion
It’s a free software
Francois Bianco Introduction to Python
Other good reasons to learn Python
Used by different universities and research centers : Universityof Montreal, Princeton University, Space Telescope ScienceInstitute, Los Alamos National Laboratory, UC Berkeley, CERN,NASA ...
If you want to look for a job in some “small” companies :Google, HP, IBM, Nokia, Thawte Consulting (SSL certificates), EAGames, Industrial Light & Magic (Hollywood), ...
Francois Bianco Introduction to Python
The end
“There should be one – and preferably only one –obvious way to do it.”
Tim Peters, The Zen of Python
Francois Bianco Introduction to Python
Top Related