SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
Introduction to Python
and Scientific Python
Fran¸cois Bianco
Unige
21th Dec 2011
Fran¸cois 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/
Fran¸cois Bianco Introduction to Python
Python is
Strongly typed (i.e. types are enforced)
Dynamically, implicitly typed (i.e. you don’t have to declare
variables)
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)
Fran¸cois Bianco Introduction to Python
Example
Example
l i s t = [1 ,1.1 ,1+1 j , ’ 1 ’ , True ]
for element in l i s t :
print element , type ( element ) , element==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) with
the equals sign “=”
Equality testing is done using two equals signs “==”
Fran¸cois 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 called
hash tables, access by key
List and tuples are access by index : list[index] (see array slicing).
Dictionary by key dictionary[’answer’].
Fran¸cois Bianco Introduction to Python
Modules loading
Classes and functions are stored in modules
from math import s i n #only one f u n c t i o n
s i n ( 0 . 3 )
import math #the whole module keeping namespace
math . s i n ( 0 . 3 )
from math import * #the whole module
cos ( 0 . 3 )
import m a t p l o t l i b as mptl #rename namespace
mptl . c o n v e r t e r ()
Fran¸cois Bianco Introduction to Python
Other features of Python
Modern way of handling errors
try :
f i l e O b j = open ( ’ fileName . t x t ’ )
except IOError :
print ErrorMessage
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 )
Fran¸cois Bianco Introduction to Python
Other features of Python
Classes, 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 according
to the t r i g g e r event .
param event Key or mouse event
t 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 statements
Example: Fibonnaci in a simple while loop
a , b = 0 ,1
while b<10:
print b
a , b = b , a+b
Easy variables assignation, and permutation, no extra variable
needed.
Fran¸cois Bianco Introduction to Python
Array slicing
Example: access to specific elements in an array
x = arange (10) #c r e a t e a vector from 0 to 9
x #the whole vector
x [ 0 ] #only the f i r s t element
x [ 3 ] #the 3 rd element
x [ −2] #the second l a t e s t element
x [ 1 : 4 ] #elements from 1 to 4
x [ : 5 ] #elements up to 5
x [ −3:] #the three l a s t elements
Fran¸cois Bianco Introduction to Python
Array masking
Example: create a mask on an array
a = arange (10)
mask = (( a % 2) == 0)
a [ mask ] = 0
This sets all the even value in a to 0.
Fran¸cois Bianco Introduction to Python
Easy plot
Example : plot with label and LATEX title
p 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 image
Example : create an image from a matrix
x = randn (20 ,20) #c r e a t e a random 20 x20 matrix
imshow ( x ) #p i x e l s c a l e
imshow ( x , extent =(0 ,1 ,0 ,1)) #add custom s c a l e
Fran¸cois Bianco Introduction to Python
Histogramm plot
Example : create an histogramm plot
mu, sigma = 100 , 15
x = mu + sigma * randn (10000)
h i s t ( x ,100)
Fran¸cois Bianco Introduction to Python
Many plots
Example : create two plots with legend
t = arange (0 ,5 ,0.05) # Vect . 0 , 0 . 0 5 , 0 . 1 , . . . , 5
s1=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 s
legend (( ’ Sin wave ’ , ’Damped exp . ’ ))
Fran¸cois Bianco Introduction to Python
iPython
Usefull magic commands in iPython
help ( obj ) #Show help
obj ? #Show doc s t r i n g
obj ?? #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 content
You want more of it ? Try %lsmagic
Fran¸cois 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
Fran¸cois Bianco Introduction to Python
Other good reasons to learn Python
Used by different universities and research centers : University
of Montreal, Princeton University, Space Telescope Science
Institute, 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), EA
Games, 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 Python
Fran¸cois Bianco Introduction to Python

Más contenido relacionado

La actualidad más candente

Function recap
Function recapFunction recap
Function recap
alish sha
 
Hacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitationHacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitation
OWASP Hacker Thursday
 

La actualidad más candente (20)

A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
 
Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings
 
Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012
 
Function recap
Function recapFunction recap
Function recap
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
Tiramisu概要
Tiramisu概要Tiramisu概要
Tiramisu概要
 
Python for Scientific Computing -- Ricardo Cruz
Python for Scientific Computing -- Ricardo CruzPython for Scientific Computing -- Ricardo Cruz
Python for Scientific Computing -- Ricardo Cruz
 
Hacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitationHacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitation
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
 
When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)
 
Pointer
PointerPointer
Pointer
 
Matplotlib
MatplotlibMatplotlib
Matplotlib
 
Introduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationIntroduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimization
 
Computer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlabComputer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlab
 
Presention programming
Presention programmingPresention programming
Presention programming
 
CS50 Lecture4
CS50 Lecture4CS50 Lecture4
CS50 Lecture4
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoya
 
Rust - Fernando Borretti
Rust - Fernando BorrettiRust - Fernando Borretti
Rust - Fernando Borretti
 
Format string vunerability
Format string vunerabilityFormat string vunerability
Format string vunerability
 
Functions
FunctionsFunctions
Functions
 

Similar a Introduction to Python and Matplotlib

Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
usvirat1805
 
Introduction to phyton , important topic
Introduction to phyton , important topicIntroduction to phyton , important topic
Introduction to phyton , important topic
akpgenious67
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 

Similar a Introduction to Python and Matplotlib (20)

Python For Scientists
Python For ScientistsPython For Scientists
Python For Scientists
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
 
Introduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLIntroduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIML
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
 
Introduction to phyton , important topic
Introduction to phyton , important topicIntroduction to phyton , important topic
Introduction to phyton , important topic
 
Python Basics
Python Basics Python Basics
Python Basics
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine Learning
 
Introduction to python.pptx
Introduction to python.pptxIntroduction to python.pptx
Introduction to python.pptx
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
Revision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdfRevision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdf
 
Chapter 1 Class 12 Computer Science Unit 1
Chapter 1 Class 12 Computer Science Unit 1Chapter 1 Class 12 Computer Science Unit 1
Chapter 1 Class 12 Computer Science Unit 1
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Machine Learning on Code - SF meetup
Machine Learning on Code - SF meetupMachine Learning on Code - SF meetup
Machine Learning on Code - SF meetup
 
Python Course Basic
Python Course BasicPython Course Basic
Python Course Basic
 
Deep Learning, Scala, and Spark
Deep Learning, Scala, and SparkDeep Learning, Scala, and Spark
Deep Learning, Scala, and Spark
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python
PythonPython
Python
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
 
Python Basics
Python BasicsPython Basics
Python Basics
 

Más de François Bianco

Fabrication and characterization of one-dimensional solid-state model systems...
Fabrication and characterization of one-dimensional solid-state model systems...Fabrication and characterization of one-dimensional solid-state model systems...
Fabrication and characterization of one-dimensional solid-state model systems...
François Bianco
 

Más de François Bianco (10)

Vulgarization poster about the Scanning Tunneling microscopy
Vulgarization poster about the Scanning Tunneling microscopyVulgarization poster about the Scanning Tunneling microscopy
Vulgarization poster about the Scanning Tunneling microscopy
 
Vulgarization poster about supraconductivity
Vulgarization poster about supraconductivityVulgarization poster about supraconductivity
Vulgarization poster about supraconductivity
 
Poster about atomic nanolines based on silicon only structures
Poster about atomic nanolines based on silicon only structuresPoster about atomic nanolines based on silicon only structures
Poster about atomic nanolines based on silicon only structures
 
Poster about atomic nanolines
Poster about atomic nanolinesPoster about atomic nanolines
Poster about atomic nanolines
 
Breakjunction for molecular contacting
Breakjunction for molecular contactingBreakjunction for molecular contacting
Breakjunction for molecular contacting
 
Thin epitaxial ferromagnetic metal films on GaAs(001) for spin injection and ...
Thin epitaxial ferromagnetic metal films on GaAs(001) for spin injection and ...Thin epitaxial ferromagnetic metal films on GaAs(001) for spin injection and ...
Thin epitaxial ferromagnetic metal films on GaAs(001) for spin injection and ...
 
Introduction au microscope à effet tunnel
Introduction au microscope à effet tunnelIntroduction au microscope à effet tunnel
Introduction au microscope à effet tunnel
 
Presentation Prix Nobel 2007 sur la magnétorésistance gigantesque
Presentation Prix Nobel 2007 sur la magnétorésistance gigantesquePresentation Prix Nobel 2007 sur la magnétorésistance gigantesque
Presentation Prix Nobel 2007 sur la magnétorésistance gigantesque
 
Présentation prix Nobel de physique 2012
Présentation prix Nobel de physique 2012Présentation prix Nobel de physique 2012
Présentation prix Nobel de physique 2012
 
Fabrication and characterization of one-dimensional solid-state model systems...
Fabrication and characterization of one-dimensional solid-state model systems...Fabrication and characterization of one-dimensional solid-state model systems...
Fabrication and characterization of one-dimensional solid-state model systems...
 

Último

會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Último (20)

Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
Mbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxMbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptx
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
MichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdfMichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdf
 
factors influencing drug absorption-final-2.pptx
factors influencing drug absorption-final-2.pptxfactors influencing drug absorption-final-2.pptx
factors influencing drug absorption-final-2.pptx
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdfPost Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 

Introduction to Python and Matplotlib

  • 1. Introduction to Python and Scientific Python Fran¸cois Bianco Unige 21th Dec 2011 Fran¸cois Bianco Introduction to Python
  • 2. 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/ Fran¸cois Bianco Introduction to Python
  • 3. Python is Strongly typed (i.e. types are enforced) Dynamically, implicitly typed (i.e. you don’t have to declare variables) 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) Fran¸cois Bianco Introduction to Python
  • 4. Example Example l i s t = [1 ,1.1 ,1+1 j , ’ 1 ’ , True ] for element in l i s t : print element , type ( element ) , element==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) with the equals sign “=” Equality testing is done using two equals signs “==” Fran¸cois Bianco Introduction to Python
  • 5. 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 called hash tables, access by key List and tuples are access by index : list[index] (see array slicing). Dictionary by key dictionary[’answer’]. Fran¸cois Bianco Introduction to Python
  • 6. Modules loading Classes and functions are stored in modules from math import s i n #only one f u n c t i o n s i n ( 0 . 3 ) import math #the whole module keeping namespace math . s i n ( 0 . 3 ) from math import * #the whole module cos ( 0 . 3 ) import m a t p l o t l i b as mptl #rename namespace mptl . c o n v e r t e r () Fran¸cois Bianco Introduction to Python
  • 7. Other features of Python Modern way of handling errors try : f i l e O b j = open ( ’ fileName . t x t ’ ) except IOError : print ErrorMessage 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 ) Fran¸cois Bianco Introduction to Python
  • 8. Other features of Python Classes, 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 according to the t r i g g e r event . param event Key or mouse event t 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. Flow control statements Example: Fibonnaci in a simple while loop a , b = 0 ,1 while b<10: print b a , b = b , a+b Easy variables assignation, and permutation, no extra variable needed. Fran¸cois Bianco Introduction to Python
  • 10. Array slicing Example: access to specific elements in an array x = arange (10) #c r e a t e a vector from 0 to 9 x #the whole vector x [ 0 ] #only the f i r s t element x [ 3 ] #the 3 rd element x [ −2] #the second l a t e s t element x [ 1 : 4 ] #elements from 1 to 4 x [ : 5 ] #elements up to 5 x [ −3:] #the three l a s t elements Fran¸cois Bianco Introduction to Python
  • 11. Array masking Example: create a mask on an array a = arange (10) mask = (( a % 2) == 0) a [ mask ] = 0 This sets all the even value in a to 0. Fran¸cois Bianco Introduction to Python
  • 12. Easy plot Example : plot with label and LATEX title p 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. Display matrix as an image Example : create an image from a matrix x = randn (20 ,20) #c r e a t e a random 20 x20 matrix imshow ( x ) #p i x e l s c a l e imshow ( x , extent =(0 ,1 ,0 ,1)) #add custom s c a l e Fran¸cois Bianco Introduction to Python
  • 14. Histogramm plot Example : create an histogramm plot mu, sigma = 100 , 15 x = mu + sigma * randn (10000) h i s t ( x ,100) Fran¸cois Bianco Introduction to Python
  • 15. Many plots Example : create two plots with legend t = arange (0 ,5 ,0.05) # Vect . 0 , 0 . 0 5 , 0 . 1 , . . . , 5 s1=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 s legend (( ’ Sin wave ’ , ’Damped exp . ’ )) Fran¸cois Bianco Introduction to Python
  • 16. iPython Usefull magic commands in iPython help ( obj ) #Show help obj ? #Show doc s t r i n g obj ?? #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 content You want more of it ? Try %lsmagic Fran¸cois Bianco Introduction to Python
  • 17. 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 Fran¸cois Bianco Introduction to Python
  • 18. Other good reasons to learn Python Used by different universities and research centers : University of Montreal, Princeton University, Space Telescope Science Institute, 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), EA Games, Industrial Light & Magic (Hollywood), ... Fran¸cois Bianco Introduction to Python
  • 19. The end “There should be one – and preferably only one – obvious way to do it.” Tim Peters, The Zen of Python Fran¸cois Bianco Introduction to Python