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

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)Sylvain Hallé
 
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 Sylvain Hallé
 
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 2012Wen-Wei Liao
 
Function recap
Function recapFunction recap
Function recapalish sha
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seoJinTaek Seo
 
Python for Scientific Computing -- Ricardo Cruz
Python for Scientific Computing -- Ricardo CruzPython for Scientific Computing -- Ricardo Cruz
Python for Scientific Computing -- Ricardo Cruzrpmcruz
 
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 exploitationOWASP Hacker Thursday
 
[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 - 13Chris Ohk
 
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)Sylvain Hallé
 
Introduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationIntroduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationKevin Keraudren
 
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 matlabShankar Gangaju
 
Presention programming
Presention programmingPresention programming
Presention programmingsaleha iqbal
 
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 LLnagoyaN Masahiro
 
Rust - Fernando Borretti
Rust - Fernando BorrettiRust - Fernando Borretti
Rust - Fernando BorrettiTryolabs
 
Format string vunerability
Format string vunerabilityFormat string vunerability
Format string vunerabilitynuc13us
 

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

Python For Scientists
Python For ScientistsPython For Scientists
Python For Scientistsaeberspaecher
 
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).pptxjovannyflex
 
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).pptxjovannyflex
 
Introduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLIntroduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLVijaySharma802
 
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.pptxusvirat1805
 
Introduction to phyton , important topic
Introduction to phyton , important topicIntroduction to phyton , important topic
Introduction to phyton , important topicakpgenious67
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine LearningYounesCharfaoui
 
Introduction to python.pptx
Introduction to python.pptxIntroduction to python.pptx
Introduction to python.pptxpcjoshi02
 
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).pdfoptimusnotch44
 
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 1ssusera7a08a
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingVijaySharma802
 
Machine Learning on Code - SF meetup
Machine Learning on Code - SF meetupMachine Learning on Code - SF meetup
Machine Learning on Code - SF meetupsource{d}
 
Python Course Basic
Python Course BasicPython Course Basic
Python Course BasicNaiyan Noor
 
Deep Learning, Scala, and Spark
Deep Learning, Scala, and SparkDeep Learning, Scala, and Spark
Deep Learning, Scala, and SparkOswald Campesato
 
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
 
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).pdfKosmikTech1
 
Python Basics
Python BasicsPython Basics
Python BasicsPooja B S
 

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

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 microscopyFrançois Bianco
 
Vulgarization poster about supraconductivity
Vulgarization poster about supraconductivityVulgarization poster about supraconductivity
Vulgarization poster about supraconductivityFrançois Bianco
 
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 structuresFrançois Bianco
 
Poster about atomic nanolines
Poster about atomic nanolinesPoster about atomic nanolines
Poster about atomic nanolinesFrançois Bianco
 
Breakjunction for molecular contacting
Breakjunction for molecular contactingBreakjunction for molecular contacting
Breakjunction for molecular contactingFrançois Bianco
 
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 ...François Bianco
 
Introduction au microscope à effet tunnel
Introduction au microscope à effet tunnelIntroduction au microscope à effet tunnel
Introduction au microscope à effet tunnelFrançois Bianco
 
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 gigantesqueFrançois Bianco
 
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 2012Franç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

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 

Último (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 

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