Python For Lecture V05
Python For Lecture V05
Python For Lecture V05
by Gerhard.Brunthaler@jku.at
2010/11
Content
1 Installation ...........................................................................................................................3
2 First Trials............................................................................................................................3
2.1 Pylab and IPython .......................................................................................................3
2.2 IDLE............................................................................................................................4
2.3 SciTe Editor ................................................................................................................5
3 Session 2.............................................................................................................................6
3.1 Short programs ...........................................................................................................6
3.2 Tutorials ......................................................................................................................8
4 Modules, Plotting, etc. .........................................................................................................8
4.1 Modules ......................................................................................................................8
4.2 Plotting ........................................................................................................................9
5 Statements, control flow, indentation, advantage...............................................................13
5.1 for loop ......................................................................................................................14
5.2 Indentation ................................................................................................................14
5.3 if – elif – else .............................................................................................................14
5.4 while..........................................................................................................................15
5.5 try..............................................................................................................................15
5.6 Advantages of Python: ..............................................................................................16
6 Numbers, Strings, Boolean................................................................................................16
6.1 Everything is an Object .............................................................................................16
6.2 Number formats ........................................................................................................16
6.3 Strings.......................................................................................................................18
6.4 Truth Value, Boolean Operations ..............................................................................18
6.5 Declaring variables....................................................................................................19
7 Datatypes ..........................................................................................................................20
7.1 Lists ..........................................................................................................................20
7.2 Tuples .......................................................................................................................21
7.3 Dictionaries ...............................................................................................................22
8 Functions...........................................................................................................................23
8.1 One return value .......................................................................................................23
8.2 No return value..........................................................................................................24
8.3 Multiple return values ................................................................................................24
8.4 Default argument values ...........................................................................................25
8.5 Lambda expressions .................................................................................................26
8.6 Built-in functions........................................................................................................26
9 Important built-in modules .................................................................................................27
9.1 The sys module.........................................................................................................28
9.2 The os module ..........................................................................................................29
10 Pitfalls ...........................................................................................................................31
10.1 The object pitfall ........................................................................................................31
10.2 More pitfalls...............................................................................................................33
11 Introspection .................................................................................................................33
12 Appendix.......................................................................................................................33
Foreword
I have heard from several students that it is difficult to find a programming language which can
be used to solve exercises in physics, for plotting scientific diagrams and for simulations. It
seems to me that Python is very suitable to perform this tasks. An important feature is also that
it is a GPL -compatible language (GPL = General Public License). So it can be used free of
charge by students and any other persons in contrast to packages like Matlab, which I use a lot
for my scientific work at the university institute.
Python is a general purpose programming language. It is interpreted and dynamically typed and
is very suited for interactive work and quick prototyping, while being powerful enough to write
large applications in.
Note: On my computer, I use the English version of the Windows operating system as well as
all software in English versions if they are available. I am also used to describe and comment
my scientific work and software stuff in simple English language and thus I go on in that way
here.
-2-
Python for Lectures
1 Installation
I suggest to use Enthought Python for scientific programming work. Enthought is a company
which offers a programming package around python. It is free for personal and academic use,
but companies have to pay for it. Before that, I installed pure Python and then added packages
to it. It took me days to get a useful system up. The Enthought package works at once and you
can indeed start to work on Python itself.
Download from end of page: http://www.enthought.com/products/getepd.php , free use for
academic and students!
Installation on Windows XP. I create usually a system restore point before larger installations or
changes, in order to be able to set my computer back to a state before that procedure.
Click installer, choose installation folder …, choose to put icons onto desktop and install!
2 First Trials
After installation, there are two new Icons on the tesktop: PyLab and Mayavi. PyLab opens the
an interactive shell “IPython”, whereas Mayavi is an 3D scientific data visualization and plotting
package. First we have a look on PyLab.
**********************************************************************
Welcome to IPython. I will try to create a personal configuration directory
where you can customize many aspects of IPython's functionality in:
Successful installation!
Please read the sections 'Initial Configuration' and 'Quick Tips' in the
IPython manual (there are both HTML and PDF versions supplied with the
distribution) to make sure that your system environment is properly configured
to take advantage of IPython's features.
Important note: the configuration system has changed! The old system is
still in place, but its setting may be partly overridden by the settings in
"~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
if some of the new settings bother you.
-3-
Python for Lectures
Python 2.6.6 |EPD 6.3-2 (32-bit)| (r266:84292, Sep 20 2010, 11:26:16) [MSC v.150
0 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
In [1]:
2.2 IDLE
IDLE is the simplest integrated development environment (IDE) for Python, which is bundled in
each release of the programming language since version 2.3.
In the Windows “Programs” folder in the start menue, the new folder “Enthought” has appeared.
Inside there are icons for “Documentation”, “Python Manuals”, “IDLE” and others. Start IDLE by
double-clicking the corresponding icon. You should get a window (with white background
color?) with the following text:
Python 2.6.6 |EPD 6.3-2 (32-bit)| (r266:84292, Sep 20 2010, 11:26:16) [MSC v.1500 32
bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************
IDLE 2.6.6
>>>
-4-
Python for Lectures
>>> 1 + 1
by pressing return at the end of the line, you should get
2
and further
>>> x = 1
>>> y = 2
>>> x + y
3
As you see, IDLE can be used for simple calculations, but also for more advanced work.
Close IDLE by entering “exit()” into the next line, or by clicking into the upper right closing button
of the active Windows window.
I personally find IDLE and IPython not very convenient environments to work with, so we try
another one included in the Enthought distribution package.
print “Hi”
into the first line of the editor, save the file as “printhi.py” to a convenient folder; the file
extension is important in order that the editor recognizes the text as Python code and performs
a corresponding text highlighting after saving.
Note: When working on a Linux system, it is usually necessary to include: #!/usr/bin/python
into the first line, so that the Python interpreter is addressed. It is not clear to me, whether this
is also necessary when working with the SciTe editor.
Press F5-key (or go to Menue “Tools” – “Go”) to execute the python code!
An attached window opens up and prints the output as:
>pythonw -u "printhi.py"
Hi
>Exit code: 0
-5-
Python for Lectures
3 Session 2
# prog_01.py
# calculation and window closing
# everything after the hash character '#' is considered as comment
# Gerhard.Brunthaler@jku.at
# if one works within an Editor, IDE or so, the output window stays open
# and you can see the result. But if you run the python script by double-
# clicking the file in the folder, under Windows the python interpreter
# (if correctly installed) may be called directly to perform the script.
# In that case it closes the output window immediately after finishing
# and you cannot read the result.
# In such a case you can add e.g. an 'raw_input' command in order
# that the python interpreter waits for an input before finishing.
# For activation, remove the two leading hash-characters from next line:
##raw_input("Press return to close this window ... ")
I could also send the script files separately, but I think that this would have a smaller learning
effect in that the participants are probably less active.
# prog_02.py
# simple calculation and formated output
# Gerhard.Brunthaler@jku.at
a = 2
b = 3
c = a*b
print c
print " " # empty line
print "a*b = ", c
print " "
-6-
Python for Lectures
# but functions like sin(x), sqrt(x) are not defined in the base module
# you have to import modules!
# the next statement leads to an error message:
sin(0.5)
In order to work with mathematical function one needs to add a module to the script. This is
shown in the next example.
# prog_04.py
# more calculations, module import
# Gerhard.Brunthaler@jku.at
With such modules Python programming gets a little more messy, but only with those one can
do powerful work.
-7-
Python for Lectures
3.2 Tutorials
For those who can’t wait for any longer, just go by yourself for one of the many tutorials and
introductions on Python like http://docs.python.org/tutorial/introduction.html or
http://diveintopython.org/.
A good introduction into Python can be found in the online tutorial http://www.network-
theory.co.uk/docs/pytut/index.html by Guido van Rossum, the creator of the Python language
himself. This tutorial can also be ordered as a book.
4.1 Modules
Working with modules – see next example.
#!/usr/bin/python
# prog_05_module_1.py
# works with Python 2.6.2
# the first line above is necessary if working on a Linux system!
-8-
Python for Lectures
Instead of importing all functions from the module, one can import a certain set by listing them
up, as in:
# prog_06b_module_2b.py
4.2 Plotting
There are different possibilities for plotting under Python. The most promising one is presently
“Matplotlib”. It allows calculations and plotting in a very similar way as with Matlab. If you work
with the Enthought Python package, matplotlib should be ready for use. If not, you have to
download matplotlib from http://matplotlib.sourceforge.net/ and install it according to the
documentation there (I did this some time ago, but for now I work with the Enthought package).
Let’s start with a simple program to demonstrate the simplicity of plotting:
# prog_09_plotting_1.py
# Python version and matplotlib version have to match in order to work properly!
import matplotlib
matplotlib.use('TkAgg') # define backend before pylab import!
-9-
Python for Lectures
figure(1) # num=1
plot(x,y)
xlabel('x')
ylabel('sin(x)')
title('Example Plot 1')
show()
A separate window with the plot of a sin-curve should open up if everything is installed correctly.
If that is the case, please stare for a while onto the image in order to appreciate your success! It
should look the image, which is partly shown below.
Fig.: Cut-out of screenshot of image produced by “prog_09_plotting_1.py”. In order to see the full
image, run the program by yourself!
If the separate window has not opened up, don’t worry and solve the problems by googleing for
the matplotlib installation problems and start a discussion in our work group!
In order that you are able to continue your work with Python, you have first to close the separate
plot window (at least on my Win-XP installation with Enthought Python and SciTe).
Now let’s consider some details of the modules used above.
Here some sentences from http://en.wikipedia.org/wiki/Matplotlib:
Matplotlib is a plotting library for the Python programming language and its NumPy numerical
mathematics extension. It provides an object-oriented API which allows plots to be embedded
into applications using generic GUI (i.e. graphical user interface) toolkits, like wxPython, Qt, or
GTK. There is also a procedural "pylab" interface based on a state machine (like OpenGL),
designed to closely resemble that of MATLAB.
Matplotlib is written and maintained primarily by John Hunter, and is distributed under a BSD-
style license.
The package pylab combines pyplot with numpy into a single namespace.
Comparison with MATLAB
The pylab interface makes Matplotlib easy to learn for experienced MATLAB users, resulting in
a viable alternative for many MATLAB users as a teaching tool for numerical mathematics and
signal processing.
Some of the advantages of Python+NumPy+Matplotlib over MATLAB include:
* Based on Python, a full-featured modern object-oriented programming language suitable
for large-scale software development
* Suitable for fast scripting, including CGI scripts
* Free, open source, no license servers
* Native SVG support
(i.e. http://en.wikipedia.org/wiki/SVG: SVG means “Scalable Vector Graphics”.)
http://en.wikipedia.org/wiki/NumPy:
NumPy is an extension to the Python programming language, adding support for large, multi-
dimensional arrays and matrices, along with a large library of high-level mathematical functions
to operate on these arrays. The ancestor of NumPy, Numeric, was originally created by Jim
Hugunin. NumPy is open source and has many contributors.
- 10 -
Python for Lectures
Since the standard Python implementation is an interpreter, mathematical algorithms often run
much slower than compiled equivalents, such as those written in C. NumPy addresses this
problem for many numerical algorithms by providing multidimensional arrays and lots of
functions and operators that operate on arrays. Thus any algorithm that can be expressed
primarily as operations on arrays and matrices can run almost as fast as the equivalent C code.
This means that “pyplot” is a module which includes the plotting capabilities, “numpy” adds the
capability to work with number arrays and matrices. Normally numpy is a separate module.
Pylab is a part of matplotlib and combines pyplot and numpy into one module with a common
namespace. Normally each module has its separate namespace, i.e. it has its own memory for
function names and variables, which can not be seen directly by the calling program. But with
modulename.function (like pylab.sqrt) one has access from the calling program to the module
namespace. In that Pylab combines pyplot and numpy, the two modules work hand in hand in a
common namespace without any barrier in between.
import matplotlib
matplotlib.use('PS') # generate postscript output by default
- 11 -
Python for Lectures
If you use the use directive, this must be done before importing matplotlib.pyplot or
matplotlib.pylab.
If you are unsure what to do, and just want to get cranking, just set your backend to TkAgg. This
will do the right thing for 95% of the users. It gives you the option of running your scripts in
batch or working interactively from the python shell, with the least amount of hassles, and is
smart enough to do the right thing when you ask for postscript, or pdf, or other image formats.
…
Here is a somewhat more advanced version of the plotting example from above:
# prog_10_plotting_2.py
# Python version and matplotlib version have to match in order to work properly!
import matplotlib
matplotlib.use('TkAgg') # define backend before pylab import!
# works also if the two lines above are absent!! - some default settings then??
Fig.: Cut-out of image produced by “prog_10_plotting_2.py”. In order to see the full image, run … !
Fig.: Small cut-out of image produced by “plotting_with_matplotlib_2.py” and saved into pdf-file. In
order to see … !
See also the Webpage “NumPy for MATLAB users” in order to learn more on NumPy
possiblities: http://mathesaurus.sourceforge.net/matlab-numpy.html
Pyplot tutorial: http://matplotlib.sourceforge.net/users/pyplot_tutorial.html
- 12 -
Python for Lectures
Plot figure with white background and fill area with plot (no space for axes, etc.), switch axis off
(in order not to see any ticks showing in from the borders) and write figtext instead of title:
fig1 = figure(1, figsize=(10.0, 10.0)) # figure with size defined in inch
fig1.set_facecolor('w')
fig1.subplots_adjust(left=0.0, bottom=0.0, right=1.0, top=1.0)
...
axis('off'); grid('off')
figtext(0.5, 0.04, t_str, horizontalalignment='center', \
verticalalignment='center', fontsize=16)
- 13 -
Python for Lectures
Such statements are marked by certain keywords. It turns that Python itself is a remarkably
simple programming language – it has a very limited number of keywords.
Python has twenty-eight keywords:
and continue else for import not raise
assert def except from in or return
break del exec global is pass try
class elif finally if lambda print while
n_sum = 0
for n in [1, 2, 3, 4, 5]: # numbers given in list
n_sum = n_sum + n
print " last number: ", n # last value of n after leaving loop
print " sum from 1 to %g: %g \n" % (n, n_sum) # sum of all
5.2 Indentation
Python uses whitespace indentation, rather than curly braces or keywords, to delimit blocks.
An increase in indentation comes after certain statements; a decrease in indentation signifies
the end of the current block.
(see: Myths about Indentation: http://www.secnetix.de/olli/Python/block_indentation.hawk )
- 14 -
Python for Lectures
5.4 while
The while statement:
# prog_11c_control_flow_3.py
# create table of square numbers with 'while'-loop
x = 1.0
while x < 6.0:
print x, '\t', x**2 # the string '\t' represents a tab character
x = x + 1.0
5.5 try
Even if a statement or expression is syntactically correct, it may cause an error when an attempt
is made to execute it. Errors detected during execution are called exceptions and are not
unconditionally fatal. They can be handled by the “try”-command. See the following example.
def is_number(s): # function defintion
try:
float(s)
return True
except ValueError:
return False
Although there is a better way to test whether a string can be converted into a number or not.
Use the isdigit() function for string objects.
a = "03523"
a.isdigit()
True
Hint
Sometimes it is necessary to put an already written program block into a loop (or generally
speaking into a control-flow statement) and thus to indent the whole block at once. This can
be done by hand, line by line. But it is desirable that a text editor supports that duty directly
(this is indeed a matter of the text editor or the IDE and is not an ability of the Python interpreter
itself, although it requires it).
- 15 -
Python for Lectures
In SciTe the task can be handled by highlighting the block of lines to be indented and just
pressing the Tab-key on the keyboard (at least in windows version SciTe 1.74).
- 16 -
Python for Lectures
>>> type(x)
<type 'complex'>
However, some expressions may be confusing since in the current version of python, using the /
operator on two integers will return another integer, using floor division. For example, 5/2 will
give you 2. You have to specify one of the operands as a float to get true division, e.g. 5/2. or
5./2 (the dot specifies you want to work with float) to have 2.5. This behavior is declined and will
disappear in a future python release.
>>> 5/2
2
>>> 5/2.
2.5
>>> 5./2
2.5
>>> float(1)/2 # can also be used to give an explicit hint to the pitfall
0.5
float also accepts the strings “nan” and “inf” with an optional prefix “+” or “-” for Not a Number
(NaN) and positive or negative infinity.
>>> a, b = float('nan'), float('-inf')
>>> print a, b
nan -inf
Some more basic mathematical functions along with the number types.
>>> pow(2,3) # 2^3
8
>>> 2**3 # 2^3
8
>>> x = 1.234
>>> import math # math module is needed
>>> math.floor(x)
1.0
>>> math.ceil(x)
2.0
Unfortunately it is a little bit complicated to get the fractional part of a floating point number.
>>> math.modf(x,1) # modulo function with rest
(0.23399999999999999, 1.0) # results in a tuple
>>> fx, fi = math.modf(x) # get both parts separately
>>> fx # fractional part alone
0.23399999999999999
http://docs.python.org/library/stdtypes.html#typesnumeric
- 17 -
Python for Lectures
6.3 Strings
In order to execute some examples again start IDLE (see chap. 2.2).
IDLE 2.6.6
>>> 'text'
'text'
>>> "this is a string"
'this is a string'
>>> >>>
Single quotes (‘) and double quotes (“) define strings the same way.
The one sort can contain the other without being interpreted or changed.
>>> "Python's"
"Python's"
>>> 'This is in "double quotes"'
'This is in "double quotes"'
A raw string (r’text’) is a special string, where backslash commands are not interpreted and
executed. It is e.g. useful to contain DOS-like path definitons (like r’c:\dir\file’).
>>> print r'c:\dir\file'
c:\dir\file
>>> print 'text with \n command sequence'
text with
command sequence
In contrast, in a string defined like 'text with \n command sequence' contains the backslash
character and is interpreted as new line command.
For more information see e.g.:
http://docs.python.org/tutorial/introduction.html#strings
Any object can be tested for truth value, for use in an if or while condition or as operand of the
Boolean operations below. The following values are considered false:
• None
• False
• zero of any numeric type, for example, 0, 0L, 0.0, 0j.
• any empty sequence, for example, '', (), [].
• any empty mapping, for example, {}.
- 18 -
Python for Lectures
b = bool(0)
print b
type(b)
Comparison operations are supported by all objects. They all have the same priority (which is
higher than that of the Boolean operations). Comparisons can be chained arbitrarily; for
example, x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in
both cases z is not evaluated at all when x < y is found to be false).
This table summarizes the comparison operations:
Operation Meaning
< strictly less than
<= less than or equal
> strictly greater than
>= greater than or equal
== equal
!= not equal
is object identity
is not negated object identity
For more details see:
http://docs.python.org/library/stdtypes.html#typesnumeric
http://docs.python.org/release/2.5.2/lib/boolean.html
- 19 -
Python for Lectures
7 Datatypes
7.1 Lists
A list is something like an array or a set of data stored with one variable name. In order to
execute examples we work again with IDLE (see chap. 2.2).
IDLE 2.6.6
>>> a = [1, 2, 3, 4]
>>> a
[1, 2, 3, 4]
>>> a[0]
1
>>> a[1]
2
>>>
- 20 -
Python for Lectures
“a” is defined here as a list with the four elements “1, 2, 3, 4”. The first element is retrieved by
“a[0]”.
Note that Python starts always counting with number “0” for the first element!
This might be somewhat unusual and inconvenient for physicists, which are more used to start
counting of natural objects with “1”. On the other hand, computer scientists work with bytes
filled with all combinations from “000” to “111” in the binary system. Translated into the decimal
system this corresponds to the numbers from 0 to 7. Thus for binary oriented work it is more
obvious to start counting at “0”.
With the function “type” you can test the type of any variable.
>>> type(a)
<type 'list'>
But a list can handle any object (and that’s everything in Python), not just numbers. Here we
create a list of strings:
>>> b = ["a", "word", "b", "example"]
>>> b[3]
'example'
>>> b[1:3]
['word', 'b']
>>> b[1:4]
['word', 'b', 'example']
With “1:3” all elements from the first index, here number 1 (i.e. the second), up to, but not
including the last index (up to number 2, which is the third element, because the counting starts
at zero).
>>> b[-1]
'example'
If the index of a list is negative (i.e. “b[-1]”) counting starts from the end with “-1” corresponding
to the last one.
Here somewhat more on Python counting. The “range” function creates a list with very much
the same notation as the “0:3” retrieving of elements from a list.
>>> range(3)
[0, 1, 2]
It also starts counting at “0” and ends one before the index “3” given as argument. Equivalently,
the function can be simply seen as producing a list with 3 elements starting at 0.
For many more details see:
http://diveintopython.org/native_data_types/lists.html
http://docs.python.org/tutorial/introduction.html#lists
There are three built-in functions that are very useful when used with lists: filter(), map(), and
reduce(), see: http://docs.python.org/tutorial/datastructures.html#functional-programming-tools
7.2 Tuples
A tuple is an immutable list. It cannot be changed in any way once it is created. Retrieving
elements from a tuple is much the same way as it is with lists.
>>> b = ("a", "word", "b", "example")
>>> b
('a', 'word', 'b', 'example')
- 21 -
Python for Lectures
>>> b[1]
'word'
>>>
>>> type(b)
<type 'tuple'>
Tuples are faster than lists. Tuples can be converted into lists and vice-versa.
For more details see: http://diveintopython.org/native_data_types/tuples.html
7.3 Dictionaries
Unlike lists, which are indexed by a range of numbers, dictionaries are indexed by keys, which
can be any immutable type; strings and numbers can always be keys.
It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement
that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}.
Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs
to the dictionary; this is also the way dictionaries are written on output.
>>> tel.keys()
['Eugen', 'Alex', 'Tom']
>>> tel.values()
[9605, 9600, 9604]
>>> 'Tom' in tel
True
>>> del tel['Tom']
>>> 'Tom' in tel
False
>>>
The keys() method of a dictionary object (i.e. “tel.keys()” in the example above) returns a list of
all the keys used in the dictionary, in arbitrary order (if you want it sorted, just apply the sorted()
function to it). To check whether a single key is in the dictionary, use the in keyword. It is also
possible to delete a key:value pair with del. If you store using a key that is already in use, the
old value associated with that key is forgotten.
For many more details see:
http://docs.python.org/tutorial/datastructures.html#dictionaries
http://diveintopython.org/native_data_types/index.html#odbchelper.dict
- 22 -
Python for Lectures
8 Functions
Functions are very useful in order to execute program parts from different positions. The
keyword def introduces a function definition.
import matplotlib
from pylab import *
axis('on'); grid('on')
title('Parametric curve: circle with velocity vectors')
xlabel('x'); ylabel('y')
- 23 -
Python for Lectures
savefig('prog_14_Curve_05.png', dpi=100)
savefig('prog_14_Curve_05.svg', dpi=100) # scalable vector graphic
Fig.8.1: Part of resulting figure of “prog_12_Curve_05.py”. The red lines represent velocity vectors.
# multiple-returns.py
a, b, c = 0, 0, 0 # multiple assignment for shortness
def getabc():
a = "Hello"
b = "World"
c = "!"
return a,b,c # defines a tuple on the fly
def gettuple():
a,b,c = 1,2,3 # Notice the similarities between this and getabc?
return (a,b,c) # defines a tuple by will
- 24 -
Python for Lectures
def getlist():
a,b,c = (3,4),(4,5),(5,6)
return [a,b,c] # defines a list
These all work, as amazing as it seems.
So multiple assignment is actually quite easy.
a,b,c = getabc()
d,e,f = gettuple()
g,h,i = getlist()
def f(arg=i):
print arg
i = 6
f()
will print
5
Important warning: The default value is evaluated only once at the function definition. This
makes a difference when the default is a mutable object such as a list, dictionary, or instances
of most classes. For example, the following function accumulates the arguments passed to it on
subsequent calls:
def f(a, L=[]):
L.append(a)
return L
- 25 -
Python for Lectures
print f(1)
print f(2)
print f(3)
If you don’t want the default to be shared between subsequent calls, you can write the function
like this instead:
# prog_14_lambda_expression.py
def f(x):
return x*2
print f(3)
y = lambda x: x*2
print y(3)
The lambda function accomplishes the same thing as the normal function above it. Note the
abbreviated syntax in the lambda expression: there are no parentheses around the argument
list, and the return keyword is missing (it is implied, since the entire function can only be one
expression). Also, the function has no name, but it can be called through the variable it is
assigned to. (http://diveintopython.org/power_of_introspection/lambda_functions.html )
- 26 -
Python for Lectures
map(function, sequence) calls function(item) for each of the sequence’s items and returns a list
of the return values. For example, to compute some cubes:
>>> def cube(x): return x*x*x
...
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
More than one sequence may be passed; the function must then have as many arguments as
there are sequences and is called with the corresponding item from each sequence (or None if
some sequence is shorter than another). For example:
>>> seq = range(8)
>>> def add(x, y): return x+y
...
>>> map(add, seq, seq)
[0, 2, 4, 6, 8, 10, 12, 14]
reduce(function, sequence) returns a single value constructed by calling the binary function
function on the first two items of the sequence, then on the result and the next item, and so on.
For example, to compute the sum of the numbers 1 through 10:
>>> def add(x,y): return x+y
...
>>> reduce(add, range(1, 11))
55
If there’s only one item in the sequence, its value is returned; if the sequence is empty, an
exception is raised.
See also: http://docs.python.org/tutorial/datastructures.html#functional-programming-tools
- 27 -
Python for Lectures
For more extensive support of multimedia, see Python Imaging Library (PIL) and the Snack
Sound Toolkit, among others.
See http://docs.python.org/tutorial/stdlib.html
and http://effbot.org/librarybook/
Because Python comes with a large amount of modules included, it is often stated as “Batteries
included”.
We can do the same if we write a small program in the editor and then execute the file.
import sys
print sys.platform
This prints
win32
(or any other computer platform, we are working on) into the output window.
The following program gives additional information on the sys module and shows access to
different data types.
# prog_15_sys_module.py
# Gerhard.Brunthaler@jku.at
# see also http://www.ibm.com/developerworks/library/l-pyint.html
import sys
print "computer platform: ", sys.platform # computer platform: win32, linux, ...
print "python interpreter: ", sys.executable # python interpreter used
print "phython version:", sys.version_info # python version
# print sys.argv
# print sys.path
- 28 -
Python for Lectures
print ""
raw_input("Press return to continue!")
print "'dictionary.items()' converts the content into a list"
L = sys.modules.items()
print "the list L contains %g items" % len(L)
print "printing the first 3 items (each being a tuple itself):"
for nn in range(3):
print L[nn]
In order to get the name of the currently executed (interpreted) python file:
import os,sys
py_file = sys.argv[0] # list of system arguments for python interpreter
for file in os.listdir(""): # list current working directory if path string is empty
print file
results in
prog_01.py
prog_02.py
...
On Windows one can e.g. give the path string like "C:\\temp" to get the directory listing of “temp”
on “C:”. Note that you have to use “\\” in a string in order to produce a single “\” in the command
output. A single backslash in the string would here be interpreted as the tabulator command
“\t”.
for file in os.listdir("C:\\temp"): # note the “\\” under Windows
print file
Using the os module to create and remove a directory and to change the working directory:
import os
- 29 -
Python for Lectures
# go down
os.chdir("test")
print "2", os.getcwd()
# go back up
os.chdir(os.pardir)
print "3", os.getcwd()
Using the os module to create and remove multiple directory levels and create a text file
meanwhile:
# prog_16_os_2b.py
# http://effbot.org/librarybook/os.htm
# Using the os module to create and remove multiple directory levels
# File: os-example-6.py
import os
Note that removedirs removes all empty directories along the given path, starting with the last
directory in the given path name. In contrast, the mkdir and rmdir functions can only handle a
single directory level.
import os
import time
file = "prog_01.py"
def dump(st):
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
print "- size:", size, "bytes"
print "- owner:", uid, gid
print "- created:", time.ctime(ctime)
print "- last accessed:", time.ctime(atime)
- 30 -
Python for Lectures
import os
if os.name == "nt":
command = "dir"
else:
command = "ls -l"
os.system(command)
10 Pitfalls
- 31 -
Python for Lectures
10017632
Here the number “3” is assigned to variable “a”. So the number “3” is generated as an object
with a certain (more or less arbitrary?) integer identity number. The variable “a” gets the same
identity number as it is equal to number “3”.
>>> b = a
>>> id(b)
10017632
>>> print b
3
With the assignment “b = a”, variable “b” gets the same id-number as “3” and “a”. And if you
print b, you get “3” as an output.
>>> a = 2
>>> id(2)
10017644
>>> id(a)
10017644
>>> id(b)
10017632
Now we changed the assignment of “a” to the object “2”, and indeed both have an new, identical
id-number. The id-number of “b” from above has not been changed, as it points to the object
“3”, which is an immutable object and still exists in the workspace. Everything works as
expected.
But this is not always the case. Lists are objects that can be changed at place (mutable
objects) and therefore unwanted effects may occur.
See the example from above:
>>> a = [1, 2]
>>> b = a
>>> id(a)
18496008
>>> a.append(3) # the number 3 is appended to the list
>>> id(a)
18496008 # the object id is not changed!
>>> print b # b “has changed” to [1, 2, 3] as well!
[1, 2, 3]
Here “a” “b” and the list “[1, 2]” all have the same object id after the first two lines. Then by
a.append, the list is changed under the same object identity. So everything which refers to that
object now refers to the changed list – so does the variable “b”.
For more explanation on that see “Python Objects” at http://effbot.org/zone/python-objects.htm
and “Sharing mutable objects” at http://www.python.org/doc/essays/ppt/hp-training/sld034.htm.
To overcome the problem above, instead of “b = a” it is necessary to create a separate object
for “b” with the same content. This can be achieved in different ways:
>>> a = [1, 2]
>>> id(a)
18540104
>>> b = a[:] # slicing: alle elements of “a” are taken and assigned to “b”
>>> id(b) # “b” is now a new, independent object
13231288
>>> c = list(a) # creates a new list out of “a”
>>> id(c) # “c” is a new object
18522440
>>> print c
[1, 2]
>>>
- 32 -
Python for Lectures
>>> import copy # the copy module is built to copy lists and objects, etc.
>>> # it possesses different copy methods
>>> d = copy.copy(a)
>>> d
[1, 2]
>>> id(d) # “d” has a new id-number, i.e. is assigned to a new list object
18522160
>>> id(a) # id of “a” is still the same as at beginning!
18540104
A tuble is immutable and thus copy does not create a new object:
# prog_17_copy_1.py
11 Introspection
Guide to Python introspection: http://www.ibm.com/developerworks/library/l-pyint.html
(Introspections means in German language: Selbstbeobachtung, Innenschau, … )
to be continued ...
12 Appendix
Details, examples and applications are collected here.
- 33 -
Python for Lectures
#! /usr/bin/env python
import matplotlib
matplotlib.use("PS")
from pylab import *
rc("font", family="serif")
rc("font", size=10)
width = 4.5
height = 1.4
- 34 -
Python for Lectures
rc("figure.subplot", left=(22/72.27)/width)
rc("figure.subplot", right=(width-10/72.27)/width)
rc("figure.subplot", bottom=(14/72.27)/height)
rc("figure.subplot", top=(height-7/72.27)/height)
figure(figsize=(width, height))
x = load("x.dat")
plot(x[:,0], x[:,1], "k-")
savefig("figure.eps")
- 35 -
Python for Lectures
- 36 -
Python for Lectures
else:
stat = os.stat(d)
created = os.stat(d).st_mtime
asciiTime = time.asctime( time.gmtime( created ) )
print d, "is a file (created", asciiTime, ")"
startDir = "/"
directories = [startDir]
while len(directories)>0:
directory = directories.pop()
for name in os.listdir(directory):
fullpath = os.path.join(directory,name)
if os.path.isfile(fullpath):
print fullpath # That's a file. Do something with it.
elif os.path.isdir(fullpath):
directories.append(fullpath) # It's a directory, store it.
- 37 -
Python for Lectures
http://pymedia.org/tut/src/dump_video.py.html:
#! /bin/env python
import sys, os
import pymedia.muxer as muxer
import pymedia.video.vcodec as vcodec
import pygame
http://stackoverflow.com/questions/1480431/most-used-python-module-for-video-processing:
I used this script to convert a movie to a numpy array + binary store:
"""Takes a MPEG movie and produces a numpy record file with a numpy array."""
import os
filename = 'walking'
if not(os.path.isfile(filename + '.npy')): # do nothing if files exists
N_frame = 42 # number of frames we want to store
os.system('ffmpeg -i WALK.MOV.qt -f image2 foo-%03d.png')
# convert them to numpy
from numpy import zeros, save, mean
from pylab import imread
- 38 -
Python for Lectures
13 Other stuff
This is a deposit of infos, materials, interesting details that might be worth to have a look at or to
incorporate it into the main part of this script.
Advantages of Python:
Here is some motivation, why to go on and learn Python!
Why python? http://www.linuxjournal.com/article/3882
Python as a First Language: http://mcsp.wartburg.edu/zelle/python/python-first.html
Java vs. Python: http://hathawaymix.org/Weblog/2004-06-16
Number arrays: I thought that only pylab can work with arrays, but also scimath can do it:
# prog_08_module_4.py
- 39 -
Python for Lectures
x2 = p.arange(0,3,1)
y21 = x2**2
print y21
x3 = [0., 1., 4.] # creates a list
y31 = x3**2
print y31
More:
What are scipy, numpy, matplotlib ?
Python is a general purpose programming language. It is interpreted and dynamically typed
and is very suited for interactive work and quick prototyping, while being powerful enough to
write large applications in.
Numpy is a language extension that defines the numerical array and matrix type and basic
operations on them.
Scipy is another language extension that uses numpy to do advanced math, signal processing,
optimization, statistics and much more.
Matplotlib is a language extension to facilitate plotting.
http://matplotlib.sourceforge.net/
http://www.scipy.org/PyLab
http://www.scipy.org/Getting_Started
Plotting with Pylab: http://www.daniweb.com/code/snippet216915.html
Nice, important:
- 40 -
Python for Lectures
http://compwiki.physics.utoronto.ca/Physics+with+Pylab
14 Version changes
V04:
New enumeration of chapters!!!
In the first part of the new version of the script, important changes are highlighted in green color
in order to help finding these parts having worked with the previous version.
Changes:
2.2 IDLE
3.2 Tutorials: Tutorial info added at end of chapter.
4.24.2 Plotting: Cut-out of images
Hint on “NumPy for MATLAB users” added at end of chapter.
5.3 Hint on indentation with SciTe editor
6 Numbers, Strings, Boolean – completely new part
7 Datatypes – completely new part
8 Functions – completely new part
9 The sys module and introspection – not changed
V05:
Table of content is shifted in front of foreword – is not correct but more practical,
if script is used online.
9 Important built-in modules: sys, os
10 Pitfalls
11 Introspection: not yet realized
12 Appendix: contains details, special topics
13 Other stuff: might be useful
14 This version info here
- 41 -