SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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...
 

Kürzlich hochgeladen

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
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Kürzlich hochgeladen (20)

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
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

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