Professional Documents
Culture Documents
Introduction to Python
Further Info
Eric Christiansen
UCSD CSE
This work is licensed under the Creative Commons Attribution 3.0 License. Based on a MATLAB tutorial by Tim Marks
Why Python?
Introduction to Python
Further Info
What is Python?
Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is:
Why Python?
Introduction to Python
Further Info
What is Python?
Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is:
cross platform
Why Python?
Introduction to Python
Further Info
What is Python?
Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is:
cross platform object oriented
Why Python?
Introduction to Python
Further Info
What is Python?
Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is:
cross platform object oriented open source
Why Python?
Introduction to Python
Further Info
Why Python?
Introduction to Python
Further Info
Why Python?
Introduction to Python
Further Info
Pythons 3rd -party libraries can help you with these tasks.
Why Python?
Introduction to Python
Further Info
Python is enhanced by a large set of scientic libraries that are being actively developed.
Why Python?
Introduction to Python
Further Info
Python is enhanced by a large set of scientic libraries that are being actively developed.
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
Why Python?
Introduction to Python
Further Info
Python is enhanced by a large set of scientic libraries that are being actively developed.
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
Why Python?
Introduction to Python
Further Info
Why Python?
Introduction to Python
Further Info
Why Python?
Introduction to Python
Further Info
bioinformatics functions
Biopython
Why Python?
Introduction to Python
Further Info
bioinformatics functions
Biopython
Why Python?
Introduction to Python
Further Info
bioinformatics functions
Biopython
neural nets
Fast Articial Neural Network (FANN) Library
Why Python?
Introduction to Python
Further Info
bioinformatics functions
Biopython
neural nets
Fast Articial Neural Network (FANN) Library
Why Python?
Introduction to Python
Further Info
Is it hard to learn?
Why Python?
Introduction to Python
Further Info
Is it hard to learn?
Why Python?
Introduction to Python
Further Info
Python vs MATLAB
Advantages of MATLAB:
Why Python?
Introduction to Python
Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used
Why Python?
Introduction to Python
Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing
Why Python?
Introduction to Python
Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation
Why Python?
Introduction to Python
Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box
Why Python?
Introduction to Python
Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box
Advantages of Python:
Why Python?
Introduction to Python
Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box
Advantages of Python:
open source means no limits on use
Why Python?
Introduction to Python
Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box
Advantages of Python:
open source means no limits on use appears to approximately superset MATLABs functionality
Why Python?
Introduction to Python
Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box
Advantages of Python:
open source means no limits on use appears to approximately superset MATLABs functionality modern language with support for object orientation
Why Python?
Introduction to Python
Further Info
Python vs MATLAB
Advantages of MATLAB:
already widely used designed specically for scientic computing easy to nd documentation good IDE with debugging and proling support out of the box
Advantages of Python:
open source means no limits on use appears to approximately superset MATLABs functionality modern language with support for object orientation support for calling functions in other languages
Why Python?
Introduction to Python
Further Info
Basics
To get information on an object from the interpreter
h e l p <o b j e c t >
Commenting:
Inline comments are preceded with # Block comments are surrounded with
Why Python?
Introduction to Python
Further Info
Vectors
Many of these functions come from SciPy.
from s c i p y import
Vectors:
N v v v v v = = = = = = 5 # [1 ,2 ,3] # array ([1 ,2 ,3]) # a r r a y ( [ [ 1 ] , [ 2 ] , [ 3 ] ] )# array ([[1 ,2 ,3]]) # transpose (v) # # # a r a n g e ( 4 ,4) # # p i a r a n g e ( 4 ,4)/4 arange ( 4 ,4 ,.5) # [] # a scalar a list a column v e c t o r a column v e c t o r a column v e c t o r transpose a vector ( row t o column o r column t o row ) a vector in a s p e c i f i e d range : arange ( s t a r t , stop , s t e p ) empty l i s t
Scientic Programming in Python 9 / 22
v = v = v = v =
Why Python?
Introduction to Python
Further Info
Matrices
len (v) m = array ([[1 ,2 ,3] ,\ [4 ,5 ,6]]) # a 2 x3 m a t r i x m[ 1 , 2 ] == m [ 1 ] [ 2 ] # access a matrix element # m a t r i x [ row , column ]
# # # #
Why Python?
Introduction to Python
Further Info
Matrices:
m v v m = = = = zeros ([2 ,3]) ones ( [ 1 , 3 ] ) rand (3 ,1) eye (3) # # # # a matrix of zeros a matrix of ones rand matrix ( see a l s o randn ) i d e n t i t y m a t r i x ( 3 x3 )
Why Python?
Introduction to Python
Further Info
Why Python?
Introduction to Python
Further Info
Why Python?
Introduction to Python
Further Info
Vector Operations
a = array ([1 ,4 ,6 ,3]) sum ( a ) mean ( a ) var (a) std (a) max ( a ) a = array ([[1 ,2 ,3] ,\ [4 ,5 ,6]]) mean ( a , 0 ) amax ( a , 1 ) amax ( a ) # # # # # # vector sum o f v e c t o r e l e m e n t s mean o f v e c t o r e l e m e n t s variance standard deviation maximum
# # # # # #
Why Python?
Introduction to Python
Further Info
Matrix Operations
dot ( t r a n s p o s e ( a r r a y ( [ 1 , 2 , 3 ] ) ) , \ array ([4 ,5 ,6])) # row v e c t o r 1 x3 t i m e s column # v e c t o r 3 x1 r e s u l t s i n a # s i n g l e number , a l s o known # as dot / i n n e r product dot ( a r r a y ( [ [ 1 ] , [ 2 ] , [ 3 ] ] ) , \ a r r a y ( [ [ 4 , 5 , 6 ] ] ) ) # column v e c t o r 3 x1 t i m e s row # v e c t o r 1 x3 r e s u l t s i n 3 x3 # m a t r i x , a l s o known # as outer product a = rand (3 ,2) # 3 x2 m a t r i x b = rand (2 ,4) # 2 x4 m a t r i x dot ( a , b ) # 3 x4 m a t r i x
Why Python?
Introduction to Python
Further Info
f = open ( s a v e f i l e , w ) # open a r c h i v e f i l e p i c k l e . dump ( o b j , f ) # dump o b j e c t t o a r c h i v e f . close () # close archive f i l e del obj # clear object # from memory archive f i l e object archive archive f i l e
Why Python?
Introduction to Python
Further Info
Why Python?
Introduction to Python
Further Info
Importing Functions
Save the following code to mylib.py:
def myfunc ( a , b ) : r e t u r n a+b 2
Why Python?
Introduction to Python
Further Info
Plotting
Why Python?
Introduction to Python
Further Info
Imaging
We use the Python Imaging Library as well as matplotlib / pylab.
from p y l a b import import Image im = Image . open ( my image . j p g ) im . show ( ) # we can d i s p l a y t h e image ima = a r r a y ( im ) # t y p e c a s t i n g to a r r a y # extracts pixel values i m r = Image . f r o m s t r i n g ( RGB , \ ( ima . s h a p e [ 1 ] , ima . s h a p e [ 0 ] ) , \ ima . t o s t r i n g ( ) ) # c o n v e r t a r r a y i n t o image img = mean ( ima , 2 ) # average color i n t e n s i t i e s # f o r each p i x e l imshow ( img ) autumn ( ) # s e t d e f a u l t c o l o r m a p t o autumn show ( )
Scientic Programming in Python 20 / 22
Why Python?
Introduction to Python
Further Info
More Help?
Why Python?
Introduction to Python
Further Info
More Help?
Many guides and tutorials are available online: Dive Into Python
python introduction for programmers
Why Python?
Introduction to Python
Further Info
More Help?
Many guides and tutorials are available online: Dive Into Python
python introduction for programmers
A list of tutorials for Python and some of its many libraries can be found at http://www.awaretek.com/tutorials.html
Why Python?
Introduction to Python
Further Info
Questions?