Skip to main content
About the Speakers

              Diane Mueller
              Director, Enterprise Product
              Management at ActiveState
              DianeM@ActiveState.com


              Dr. Mike Müller
              CEO, Python Academy
              mmueller@python-academy.de
Agenda

 • About ActiveState & Python Academy
 • Quick Comparison of Matlab & Python
 • Deep Dive into Python’s "Pylab" functionality
 • Includes: Python Basics, NumPy, SciPy, IPython, matplotlib
 • Other considerations
 • Q&A
About ActiveState

  • Founded 1997
  • 2 million developers, 97% of Fortune 1000 rely on ActiveState
  • Development, management, distribution solutions for dynamic
    languages
  • Core languages: Python, Perl, Tcl
  • Other: PHP, Ruby, Javascript
About Python Academy

 • specialist for Python training
 • open and custom courses
 • Python for programmers
 • Python for scientists and engineers
 • many more advanced topics (extensions, databases, GUI,
   web)
 • Python consulting
ActiveState Solutions
Packages Include

  • "Pylab": NumPy, SciPy, Matplotlib
  • GUI Toolkits: pyQT, wxPython
  • Database Connectors: Postgres, MySQL, Oracle, MSSQL,
    SqlAlchemy, ODBC
  • Cryptography: M2Crypto
Quick Comparison               Python - "Pylab"

MATLAB                            • Open Source
                                  • High-level language
  • Proprietary to Mathworks
                                  • Tools for building your
  • High-level language
                                    own "PyLab"
  • interactive environment
                                  • With custom graphical
  • Deep computational              user interfaces
    libraries
                                  • And interactive tools for
  • Visualizations                  iterative exploration,
                                    design, and problem
  • Workbench
                                    solving
                                  • Multiple Development
                                    environments available
Python - Origin

  • 1989/1990 Guido van Rossum
  • in between C and shell scripting
  • ideas from ABC, C, Smalltalk,
    Java, (Haskell)
  • 1999 version 1.5.2
  • now at 2.7 / 3.1 (3.2beta)
  • Python Software Foundation
Open Source

 • Python license
 • Python Software Foundation
 • core developers
 • developers of third party libraries
 • Python Enhancement Proposals (PEP)
Features

  • simple
  • consistent
  • readable
  • general purpose programming language
  • research and implement in the same language
  • yet good replacement for MATLAB (NumPy + matplotlib +
    IPython + SciPy)
  • can do way more than MATLAB
Users

  • Open Source
  • Google (core developers)
  • YouTube
  • German Aerospace Center
  • NASA
  • Walt Disney
  • Rackspace
  • Financial sector (AQR)
  • ...
Users

  • programming novices
  • professional software developers
  • tester
  • system admins
  • scientists and engineers, quants
  • Python scales

        • easy to get started with
        • many advanced features if needed (meta programming)
Popularity
Popularity

  • easy to get started with
  • MIT uses Python for "Introduction to CS and Programming"
  • Michigan State replaced C++ successfully with Python
  • embedded in Blender, Open Office, Inkscape, Gimp
  • its just fun for lots of people
Operating Systems

  • Windows
  • Linux
  • Mac
  • Main-Frames
  • Mobile
  • DOS
Technology

  • interpreter
  • platform independent bytecode
  • everything at run time
  • simple meta-programming
Implementations

  • CPython == Standard Python
  • Jython in Java
  • IronPython in C#
  • PyPy in Python
  • Stackless
  • more
Example of File IO
the file data.txt:


 a      b       c
 1      2       3
 4      5       6
 7      8       9
Example of File IO
>>> fobj = open('data.txt')
>>> header = fobj.next().split()
>>> header
['a', 'b', 'c']
>>> for line in fobj:
...     numbers.append([int(entry) for entry in line.split()])
...
>>> numbers
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Connecting to Your Data Sources

  • csv module in standard library
  • read and write Excel files (xlrd, xlwt)
  • or use COM with MS Office
  • read and write HDF5 files (PyTables, h5py)
  • netCDF (pynetcdf, nectdf4-python, pupynere)
  • binary files (struct from standard library)
  • more (just google for desired format and Python)
Databases

  • connect to all major database
  • Oracle
  • SQL-Server
  • MySQL
  • PostgresSQL
  • DB2
  • SQLite
  • more
Object Relational Mappers

  • SQLAlchemy
  • SQLObject
  • use object-oriented programming
  • no need to write SQL by hand
  • no SQL injection
NumPy

 • work with numeric arrays
 • MATLAB-like features
 • implemented in C
 • fast
 • interactive
 • 21 data types
Create Arrays

 >>> import numpy as np
 >>> a = np.array([[1, 2, 3], [4, 5, 6]])
 >>> a
 array([[1, 2, 3],
        [4, 5, 6]])
 >>> a.shape
 (2, 3)
 >>> a.dtype
 dtype('int32')
 >>> a.size
 6
Create Arrays

 >>> b = np.arange(10)
 >>> b
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 >>> c = np.array([[1, 2, 3], [4, 5, 6]],
                  dtype=np.float32)
 >>> c
 array([[ 1., 2., 3.],
        [ 4., 5., 6.]], dtype=float32)
Create Arrays

>>> np.arange(16, dtype=np.float).reshape(4, 4)
array([[ 0.,    1.,   2.,   3.],
       [ 4.,    5.,   6.,   7.],
       [ 8.,    9., 10., 11.],
       [ 12., 13., 14., 15.]])
>>> np.zeros((3, 4))
array([[ 0., 0., 0., 0.],
       [ 0., 0., 0., 0.],
       [ 0., 0., 0., 0.]])
Create Arrays

 >>> np.ones((3, 4))
 array([[ 1., 1., 1., 1.],
        [ 1., 1., 1., 1.],
        [ 1., 1., 1., 1.]])
 >>> np.zeros((3, 4)) + 5
 array([[ 5., 5., 5., 5.],
        [ 5., 5., 5., 5.],
        [ 5., 5., 5., 5.]])
Data Types

  • 21 data types
  • signed and unsigned integer (8, 16, 32, 64 bit)
  • floats (32, 64 an 96 bit)
  • complex (64, 128, 192 bit)
  • bool
  • string
  • character
  • objects
Data Types

>>> a = np.array([1, 2, 3], dtype=np.int64)
>>> a
array([1, 2, 3], dtype=int64)
>>> a.dtype
dtype('int64')
>>> a.dtype.type
<type 'numpy.int64'>
>>> a.dtype.char
'q'
>>> a.dtype.str
'<i8'
Slicing

 >>> one_d = np.arange(10)
 >>> one_d
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 >>> one_d[0]
 0
 >>> one_d[1]
 1
 >>> one_d[-1]
 9
 >>> one_d[2:5]
 array([2, 3, 4])
Slicing

 >>> two_d = np.arange(25).reshape(5, 5)
 >>> two_d
 array([[ 0, 1, 2, 3, 4],
        [ 5, 6, 7, 8, 9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24]])
 >>> two_d[0, 0]
 0
 >>> two_d[0]
 array([0, 1, 2, 3, 4])
Slicing

 >>> two_d[:,0]
 array([ 0, 5, 10, 15, 20])
 >>> two_d[1:-1,1:-1]
 array([[ 6, 7, 8],
        [11, 12, 13],
        [16, 17, 18]])


  • advanced selection (integers, booleans, strings)
  • "loop-less" programming
Broadcasting

>>> two_d
array([[ 0,     1,    2, 3, 4],
       [ 5,     6,    7, 8, 9],
       [10,   11,    12, 13, 14],
       [15,   16,    17, 18, 19],
       [20,   21,    22, 23, 24]])
>>> two_d +   5
array([[ 5,     6,    7,    8,    9],
       [10,   11,    12,   13,   14],
       [15,   16,    17,   18,   19],
       [20,   21,    22,   23,   24],
       [25,   26,    27,   28,   29]])
Broadcasting

>>> a = np.arange(8).reshape(2, 4)
>>> a
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
>>> b = np.arange(4)
>>> b
array([0, 1, 2, 3])
>>> a + b
array([[ 0, 2, 4, 6],
       [ 4, 6, 8, 10]])
Universal Functions
>>> np.sin(a)
array([[ 0.        , 0.84147098, 0.90929743, 0.14112001],
       [-0.7568025 , -0.95892427, -0.2794155 , 0.6569866 ]])
>>> np.sin.nin
1
>>> np.sin.nout
1
>>> np.sin.types
['f->f', 'd->d', 'g->g', 'F->F', 'D->D', 'G->G', 'O->O']
Matrices

  • always 2D

 >>> M = np.matrix('1 2 3; 4 5 6')
 >>> M
 matrix([[1, 2, 3],
         [4, 5, 6]])
 >>> M.I
 matrix([[-0.94444444, 0.44444444],
         [-0.11111111, 0.11111111],
         [ 0.72222222, -0.22222222]])
SciPy

  • big package
  • interpolation
  • linear algebra
  • FFT
  • statistics
  • optimization
  • much more
IPython

  • improved interactive shell
  • object introspection
  • system shell access
  • command system for adding functionality
  • allows interactive work with matplotlib ipython -pylab
Basic matplotlib-plotting

  • simple, yet powerful plotting package

In [1]: plot(random(100))
Out[1]: [<matplotlib.lines.Line2D object at 0x02A3EA90>]
Properties

  • colors
  • line styles
  • markers
  • legend
Properties
In   [1]:   x = arange(100)
In   [2]:   linear = x
In   [3]:   square = arange(0, 10, 0.1) ** 2
In   [4]:   lines = plot(x, linear, 'g:+', x, square, 'r--o')
In   [5]:   leg = legend(('linear', 'square'), loc='upper left')
Properties
Ticks and Formatters

In   [6]: major_locator = MultipleLocator(10)
In   [7]: major_formatter = FormatStrFormatter('%5.2f')
In   [8]: minor_locator = MultipleLocator(5)
In   [9]: ax = gca()
In   [10]: ax.xaxis.set_major_locator(major_locator)
In   [11]: ax.xaxis.set_minor_locator(minor_locator)
In   [12]: ax.xaxis.set_major_formatter(major_formatter)
In   [13]: draw()
Nice Plots
Nice Plots
Nice Plots
Pylab is no total replacement MATLAB (yet)

  • Pylab = NumPy + SciPy + IPython + matplotlib
  • no Simulink
  • documentation, especially for SciPy not as comprehensive but
    growing
  • no equivalent library for all toolboxes
  • smaller user-base but growing
  • array and matrix distinction can cause more verboseness
Pylab is no total replacement MATLAB (yet)

  • same functionality sometimes duplicated
  • finding the right library might take some time
  • IDEs not as polished, less support for profiling
Python is no Island

  • extensible in C/C++, Java, C#
  • SWIG, SIP, Boost.Python
  • .NET, COM
  • Cython
  • embedding
  • access DLLS / shared libraries
C/C++ integration with Cython

  • call C-functions

 cdef extern from "math.h":
     double sin(double)

 cdef double f(double x):
     return sin(x*x)


  • works also with C++
Glue Language

  • connect (heterogeneous) systems
  • generate input data
  • start and control processes
  • read output data
  • communicate over the network
  • ...
  • fast, few lines of code
Performance and Future Developments

  • multi-cores
  • multiprocessing (standard library)
  • GPUs
  • theano
  • pyCUDA
  • Clyther
More Scientific Libraries

  • RPy /RPy2 (use the R language from Python)
  • Scikits (times series and many others)
  • pandas (time series and statistical analysis)
  • SAGE (free open source alternative to Magma, Maple,
    Mathematica and Matlab.)
  • PyPi - 766 packages for science and engineering
What else?

  • standard library - batteries included
  • Python Package Index: > 12.000 packages
  • wrapper for GUI-Toolkits (wxPython, PyQt, Tkinter etc.)
  • web frameworks (Django, Zope, TurboGears, Pylons etc.)
  • access to all major database systems
  • useful library for nearly all purposes
Thank you! Questions?

  • Questions?
  • Next Steps:

       • Find out more about ActivePython Business Edition:
         http://www.activestate.com/activepython
       • Download ActivePython:
         http://www.activestate.com/activepython/downloads
       • Request Information:
         Business-Solutions@activestate.com or 1-866-510-2914
We've Got Your Universe Covered