Upcoming SlideShare
×

# Introduction to Python and Matplotlib

719 views

Published on

A short introduction to Python for beginner showing principles of the interactive ipython shell and plotting with matplotlib

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
719
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
25
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Introduction to Python and Matplotlib

1. 1. Introduction to Pythonand Scientiﬁc PythonFran¸cois BiancoUnige21th Dec 2011Fran¸cois Bianco Introduction to Python
2. 2. Outlook1 Python features and syntax2 Examples, pylab, and iPython3 Flat ﬁles examplesBased on :Learn Python in 10 minutes :http://www.poromenos.org/tutorials/pythonPython documentation : http://docs.python.org/index.htmlMatplotlib : http://matplotlib.sourceforge.net/Scipy : http://www.scipy.org/Fran¸cois Bianco Introduction to Python
3. 3. Python isStrongly typed (i.e. types are enforced)Dynamically, implicitly typed (i.e. you don’t have to declarevariables)Python iscase sensitive (i.e. var and VAR are two diﬀerent variables)object-oriented (i.e. everything is an object)able to handle memory by itself (i.e has a garbadge collector)Fran¸cois Bianco Introduction to Python
4. 4. ExampleExamplel i s t = [1 ,1.1 ,1+1 j , ’ 1 ’ , True ]for element in l i s t :print element , type ( element ) , element==1.1No mandatory statement termination characterBlocks are speciﬁed 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 “==”Fran¸cois Bianco Introduction to Python
5. 5. Data structuresThree main data structureslist = [1,2,3,4,5,6] are mutabletuples = (1,2,3,4,5,6) are unmutabledictionary = { ’key’:’value’, ’answer’:42, ’obj’:list } also calledhash tables, access by keyList and tuples are access by index : list[index] (see array slicing).Dictionary by key dictionary[’answer’].Fran¸cois Bianco Introduction to Python
6. 6. Modules loadingClasses and functions are stored in modulesfrom math import s i n #only one f u n c t i o ns i n ( 0 . 3 )import math #the whole module keeping 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 ()Fran¸cois Bianco Introduction to Python
7. 7. Other features of PythonModern way of handling errorstry :f i l e O b j = open ( ’ fileName . t x t ’ )except IOError :print ErrorMessageLambda functionsf 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 )Fran¸cois Bianco Introduction to Python
8. 8. Other features of PythonClasses, functions...def functionName ( param , optionalParam=value ) : . . .class C h i l d C l a s s ( ParentClass ) : . . .Automatic documentation generation (with Doxygen)def toggleImages ( s e l f , event ) :””” Toggle the two images accordingto the t r i g g e r event .param event Key or mouse eventt r i g g e r i n g the f u n c t i o n”””Fran¸cois Bianco Introduction to Python
9. 9. Flow control statementsExample: Fibonnaci in a simple while loopa , b = 0 ,1while b<10:print ba , b = b , a+bEasy variables assignation, and permutation, no extra variableneeded.Fran¸cois Bianco Introduction to Python
10. 10. Array slicingExample: access to speciﬁc elements in an arrayx = arange (10) #c r e a t e a vector from 0 to 9x #the whole vectorx [ 0 ] #only the f i r s t elementx [ 3 ] #the 3 rd elementx [ −2] #the second l a t e s t elementx [ 1 : 4 ] #elements from 1 to 4x [ : 5 ] #elements up to 5x [ −3:] #the three l a s t elementsFran¸cois Bianco Introduction to Python
11. 11. Array maskingExample: create a mask on an arraya = arange (10)mask = (( a % 2) == 0)a [ mask ] = 0This sets all the even value in a to 0.Fran¸cois Bianco Introduction to Python
12. 12. Easy plotExample : plot with label and LATEX titlep l o t ( arange (5))x l a b e l ( ’ Index ’ )y l a b e l ( ’Sum ’ )t i t l e ( r ’ \$sum { i =0}ˆ i n f t y i \$ ’ )Fran¸cois Bianco Introduction to Python
13. 13. Display matrix as an imageExample : create an image from a matrixx = randn (20 ,20) #c r e a t e a random 20 x20 matriximshow ( x ) #p i x e l s c a l eimshow ( x , extent =(0 ,1 ,0 ,1)) #add custom s c a l eFran¸cois Bianco Introduction to Python
14. 14. Histogramm plotExample : create an histogramm plotmu, sigma = 100 , 15x = mu + sigma * randn (10000)h i s t ( x ,100)Fran¸cois Bianco Introduction to Python
15. 15. Many plotsExample : create two plots with legendt = arange (0 ,5 ,0.05) # Vect . 0 , 0 . 0 5 , 0 . 1 , . . . , 5s1=s i n (2* pi * t )s2=s1 *exp(−t )p l o t ( t , s1 , ’g−−o ’ , t , s2 , ’ r : s ’ ) # custom s t y l e slegend (( ’ Sin wave ’ , ’Damped exp . ’ ))Fran¸cois Bianco Introduction to Python
16. 16. iPythonUsefull magic commands in iPythonhelp ( obj ) #Show helpobj ? #Show doc s t r i n gobj ?? #Show source code#r e t u r n l a s t value%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 without l i n e number%exec In [ 4 : 7 ] #redo l i n e 4 to 7%e d i t 4:7 #e d i t l i n e 4 to 7 in s c r i p t%run #launch a s c r i p t% p f i l e #show source f i l e contentYou want more of it ? Try %lsmagicFran¸cois Bianco Introduction to Python
17. 17. Pro and consConsNo GUIDocumentation spread on diﬀerent websitesRequires basics programming skillsProEasy to learnWork on every plateform (WinXP,Vista,MacOS,Linux,...)Could be bind to GwyddionIt’s a free softwareFran¸cois Bianco Introduction to Python
18. 18. Other good reasons to learn PythonUsed by diﬀerent 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 certiﬁcates), EAGames, Industrial Light & Magic (Hollywood), ...Fran¸cois Bianco Introduction to Python
19. 19. The end“There should be one – and preferably only one –obvious way to do it.”Tim Peters, The Zen of PythonFran¸cois Bianco Introduction to Python