Introduction to Python 3
Chang Y. Chung
May, 2015
Shh
I Python is . . .
1 / 22
Shh
I Python is . . .
I slow.
1 / 22
Python is slow
I A tight loop like below runs 10 to 100 (or more) times
slower than C or java.
1 total = 0
2 f o r i in range(1000):
3 f o r j in range(1000):
4 t o t a l += i # how many times this statement runs?
5
6 print total
7 # 499950000000
2 / 22
Python is slow
I A tight loop like below runs 10 to 100 (or more) times
slower than C or java.
1 total = 0
2 f o r i in range(1000):
3 f o r j in range(1000):
4 t o t a l += i # how many times this statement runs?
5
6 print total
7 # 499950000000
I Although you can re-write the above and make it run
almost, but not quite, as fast.
1 p r i n t sum([1000 * i f o r i in xrange(1000)])
2 # 499950000000
2 / 22
Why is Python slow
I Interpreted, not compiled.
I Almost no automatic optimization.
I High-level, versatile programming constructs tend to be
larger, more complicated, and slower.
I A simple piece of code may have a huge performance
implication. e.g. range(1000) creates and returns a
1000-element list every time it is called.
3 / 22
Why Python is not slow
I Faster programming constructs (e.g., xrange() vs. range(),
comprehension vs. for loop)
I Modules written in C (e.g., cPickle vs. pickle)
I NumPy and SciPy for scientific computation.
I Python/C API ([Link]
I Cython ([Link] takes Python code and
generates efficient C code.
I PyPy Just-In-Time (JIT) compiler. ([Link]
4 / 22
Implementations
I The reference implemention (in C) is called CPython,
which Guido van Rossum authored, starting in 1989
I Guido is also known as Benevolent Dictator For Life
(BDFL. See [Link]
5 / 22
Implementations (cont.)
I There are other implementations as well.
I IronPython (.NET CLR [Link]
I Jython (Java VM [Link]
I pyjs (JavaScript [Link]
I Skulpt (web browser [Link]
I CodeSkulptor (web browser
[Link]
6 / 22
Python 2 or 3?
I Python 3.0 (2008) broke backward compatibility.
B Can’t use 2 modules in 3 and vice versa.
I "2 is legacy, 3 is the present and future."
([Link]
B 3.4 is expected in early 2014.
B 2.0 was released in 2000.
B 2.7 (2010) will be the last 2.x branch.
I Many of 3’s major futures have been backported to 2.6
and 2.7, but not all.
I Other implementations in general still lack support for
Python 3.
7 / 22
Editors and IDE’s
I EMACS comes with [Link] (24.2 and up) and
[Link] (newer). See
([Link]
I VIM configuration links at [Link]
I IDLE ([Link]
I (Semi-) commercial editors, e.g., Komodo, PyCharm,
Sublime, . . .
I IPython ([Link] and IPython notebook.
I And many others. See [Link]
8 / 22
IPython and IPython Notebook
I A comprehensive environemnt for interactive and
exploratory computing.
I A “new killer app” back in 2011. 1.0 released in 2013.
Stable version 2.0 in April 2014.
I One of the six core packages of SciPy stack.
9 / 22
PyPI and pip
I Python Package Index (PyPI) is the repository of software
for Python at [Link]
I As of a day in Jan 2014, it has about 38,800 packages.
I Python Indexing Project (pip)
([Link] is the standard tool
for installing packages (or modules) from PyPI.
I Some examples of using pip. At the shell prompt:
1 $ pip
2 $ pip list
3 $ pip i n s t a l l SomePackage
4 $ pip i n s t a l l --user SomePackage
5 $ pip i n s t a l l --upgrade SomePackage
6 $ pip uninstall
I Once a package is successfully installed, then you can
import the module within your script.
10 / 22
Installing SciPy Stack
I It is possible to install all the packages one by one (and
all the dependencies). It could turn out to be tricky.
I An alternative is to download and install free or
commercial distributions. Some names are: Anaconda,
Enthought Canopy, Python(x,y), WinPython, . . .
I See [Link]
I Check out [Link] ([Link] for
playing with SciPy stack on the cloud, without local
installation.
11 / 22
Quiz
I Choose the best one that fits each description:
1. Standard module supporting object (de-)serialization,
which is written in C.
2. Compiler that turns Python source into efficient C code.
3. Software tool for installing / managing packages.
4. Benevolent Dictator For Life.
5. Provides a rich architecture for interactive (scientific)
computing. Version 1.0 was released in 2013.
comprehension cPickle CPython Cython Guido van
Rossum IPython Niklaus Wirth Pickle pip Sublime
xrange() Yukihiro Matsumoto
12 / 22
NumPy
I Provides the ndarray object.
I implements an efficient homogeneous
ndarray
multidimensional array.
I Element-wise and vectorized matrix operations are
provided.
I Lots of modules use / built on NumPy.
I Documentation at [Link]
13 / 22
SciPy
I Collection of mathematical algorithms and utility
functions built on NumPy.
I Organized into subpackages: cluster, constants, fftpack,
integrate, interpolate, io, linalg (linear algebra), ndimage
(N-dimentional image processing), odr (orthogonal
distance regression), optimize, signal (signal processing),
sparse (sparce matrices), spatial, special (functions),
stats, weave (C/C++ integration)
I Documentation at [Link]
14 / 22
Matplotlib
I Provides comprehensive 2D and simple 3D plotting.
I Simple plot, Subplots (multiple axes), Histograms, Path,
Simple 3D plot (surface, wireframe, scatter, bar),
Streamlines (of a vector field), Ellipses, Bar charts, Pie
charts, Filled (curves and polygons), Financial charts,
Polar plots, . . . , including TeX expressions support
(internal or external) and Sketch plots (XKCD style)
I Screenshots are (with source code) at
[Link]
I Documentation at
[Link]
15 / 22
pandas
I “Python Data Analysis Library” (Release 0.12 as of 2013).
I Series, DataFrame , and Panel objects
I reading/writing data to and from: CSV, text file, Excel,
SQL db, and fast HDF5 (scientific data file formats and
libraries developed at NCSA), JSON, HTML Table, STATA.
I Labeling columns, iteration, Hierarchical Indexing,
Transformation, Selection, Missing Data, Merge, Grouping
(or split-apply-combine), Reshaping (or pivoting), Time
Series, I/O tools, R interface (via rpy2).
I Documentation at [Link]
I Wes McKinney, “10-minute tour of pandas”
([Link] or workshop
([Link]
16 / 22
Learning Resources
I Websites:
B Main website [Link] and SciPy site
[Link]
B Official Python Tutorial
[Link]
B Google’s Python Class (2 day class materials including
video and exercises)
[Link]
17 / 22
Learning Resources
I Three advanced level tutorial videos:
B technical (old)
[Link]
B idioms (new)
[Link]
B functional style
[Link]
B 2000+ videos at [Link]
18 / 22
Learning Resources
I Books:
B Mark Lutz (2013) Learning Python 5th ed (1,400 plus
pages).
B “6 Free E-Books” mentioned on
[Link]
B Matthew Russell (2013) “Mining the Social Web” 2nd
edition is out. Example code files (in IPython Notebook file
.ipynb format) are at [Link]
19 / 22
Learning Resources
I Any cool computer language has:
B Zen (read and memorize!)
[Link]
B Koans (unit testing) [Link]
B Challenges (old) [Link]
I Need more challenges?
B Try the Project Euler [Link]
20 / 22
Learning Resources
I MOOC’s using Python extensively:
B “Introduction to Computer Science and Programming
Using Python” (edX, [Link]
B “Introduction to Interactive Programming in Python”
(Coursera, [Link]
B “Coding the Matrix: Linear Algebra through Computer
Science Applications” (Coursera,
[Link]
21 / 22
Learning Resources
I Twitter:
B "teaching python 140 character at a time":
[Link]
I Gallery
B IPython Notebook gallery (including social data)
[Link]
22 / 22