Introduction to Python and Matplotlib
Upcoming SlideShare
Loading in...5
×
 

Introduction to Python and Matplotlib

on

  • 448 views

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

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

Statistics

Views

Total Views
448
Views on SlideShare
448
Embed Views
0

Actions

Likes
0
Downloads
17
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Introduction to Python and Matplotlib Introduction to Python and Matplotlib Presentation Transcript

  • Introduction to Pythonand Scientific PythonFran¸cois BiancoUnige21th Dec 2011Fran¸cois Bianco Introduction to Python
  • Outlook1 Python features and syntax2 Examples, pylab, and iPython3 Flat files 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
  • 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 different 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
  • 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 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 “==”Fran¸cois Bianco Introduction to Python
  • 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
  • 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
  • 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
  • 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
  • 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
  • Array slicingExample: access to specific 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Pro and consConsNo GUIDocumentation spread on different 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
  • Other good reasons to learn PythonUsed 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), ...Fran¸cois Bianco Introduction to Python
  • 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