Python for Lecture V03
-
Upload
gerhard-brunthaler -
Category
Documents
-
view
235 -
download
0
Transcript of Python for Lecture V03
-
8/8/2019 Python for Lecture V03
1/14
Gerhard Brunthaler - 1 -
Python for Lecture
V03
2010/11
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.
Inhalt
1 Installation ...........................................................................................................................22 First Trials............................................................................................................................2
2.1 Pylab...........................................................................................................................22.2 SciTe Editor ................................................................................................................32.3 Session 2 ....................................................................................................................42.4 Tutorials ......................................................................................................................6
3 Modules, Plotting, etc. .........................................................................................................73.1 Modules ......................................................................................................................73.2
Plotting........................................................................................................................8
3.3 Statements, control flow, indentation, advantage ...................................................... 113.4 The sys module and introspection.............................................................................12
Note: On my computer, I use the English version of the Windows operating system as well asall software in English versions if they are available. I am also used to describe and commentmy scientific work and software stuff in English language and thus I go on in that way here.
-
8/8/2019 Python for Lecture V03
2/14
Python for Lecture
- 2 -
1 Installation
I suggest to use Enthought Python for scientific programming work. Enthought is a companywhich 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 incident.
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.
2.1 Pylab
Clicking on Pylab opens up a black command window and after 30 seconds or a minute (bepatient) some text appears:
**********************************************************************Welcome to IPython. I will try to create a personal configuration directorywhere you can customize many aspects of IPython's functionality in:
C:\Documents and Settings\bru\_ipythonInitializing from configuration: C:\Prog\Python26EPD\lib\site-packages\IPython\serConfig
Successful installation!
Please read the sections 'Initial Configuration' and 'Quick Tips' in theIPython manual (there are both HTML and PDF versions supplied with thedistribution) to make sure that your system environment is properly configuredto take advantage of IPython's features.
Important note: the configuration system has changed! The old system isstill 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 fileif some of the new settings bother you.
Please press to start IPython.
-
8/8/2019 Python for Lecture V03
3/14
Python for Lecture
- 3 -
http://en.wikipedia.org/wiki/IPython: IPython is an interactive shell for the Python programming
language
after pressing return in the command window, one gets:
**********************************************************************
Enthought Python Distribution -- http://www.enthought.com
Python 2.6.6 |EPD 6.3-2 (32-bit)| (r266:84292, Sep 20 2010, 11:26:16) [MSC v.1500 32 bit (Intel)]Type "copyright", "credits" or "license" for more information.
IPython 0.10.1 -- An enhanced Interactive Python.? -> Introduction and overview of IPython's features.%quickref -> Quick reference.help -> Python's own help system.object? -> Details about 'object'. ?object also works, ?? prints more.
Welcome to pylab, a matplotlib-based Python environment.For more information, type 'help(pylab)'.
In [1]:
type: exit() in order to finish the IPython shell.
IPython is not a very convenient environment to work, so we try to find another one which
should be included in the distribution package.
2.2 SciTe Editor
In the Windows Programs folder in the start menue, the new folder Enthought has appeared.
Inside there are icons for Documentation, Python Manuals, SciTE and others.
http://en.wikipedia.org/wiki/SciTE: SciTE or SCIntilla based Text Editor is a cross-platform text
editor
In the Documentation folder you find at position 88 the link
http://www.scintilla.org/SciTEDoc.html to the SciTe documentation for detailed information!
Start SciTe and type:
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
-
8/8/2019 Python for Lecture V03
4/14
Python for Lecture
- 4 -
Congratulations, you have run your first Python script!
Exit code: 0 means that there was no error.
2.3 Session 2Here is the next small script file (program) to demonstrate a little more on the print statement
and on commenting with the hash sign #.
# prog_01.py# calculation and window closing# everything after the hash character '#' is considered as comment# [email protected]
# printing into output window:print "Hi"print 1 + 2print " " # create empty line# do not type any space in front of line - this would cause an error!
# 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# [email protected]
a = 2
b = 3c = a*bprint cprint " " # empty lineprint "a*b = ", cprint " "
print "formated output:"print "a = %g, b = %g gives c = a*b = %g" % (a,b,c)# for %-formating see "5.6.2. String Formatting Operations" in# http://docs.python.org/library/stdtypes.html#typesseq-strings
http://docs.python.org/library/stdtypes.html#typesseq-strings : see 5.6.2. String Formatting
Operations.
Conversion Meaning Notes 'd' Signed integer decimal.
-
8/8/2019 Python for Lecture V03
5/14
Python for Lecture
- 5 -
'i' Signed integer decimal.'o' Signed octal value. (1)'u' Obsolete type it is identical to 'd'. (7)'x' Signed hexadecimal (lowercase). (2)'X' Signed hexadecimal (uppercase). (2)'e' Floating point exponential format (lowercase). (3)
'E' Floating point exponential format (uppercase). (3)'f' Floating point decimal format. (3)'F' Floating point decimal format. (3)'g' Floating point format. Uses lowercase exponential format if exponent is less than -4
or not less than precision, decimal format otherwise. (4)'G' Floating point format. Uses uppercase exponential format if exponent is less than -4
or not less than precision, decimal format otherwise. (4)'c' Single character (accepts integer or single character string).'r' String (converts any Python object using repr()). (5)'s' String (converts any Python object using str()). (6)'%' No argument is converted, results in a '%' character in the result.
next program example:
# prog_03.py# more simple calculations# [email protected]
# printing into output window:print "Simple calculations:"print " 2/3 = ", 2/3, "- here an integer division has been performed!"print " 2.0/3 = ", 2.0/3, "- here a floating point operation has been performed!"print " (1+2j)*(2+3j) =", (1+2j)*(2+3j), "- complex numbers"
# 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)
# also the constant 'pi' is not yet defined
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# [email protected]
# importing a mathematic module:from pylab import * # include function e.g. as sin(0)
print "Functions from mudule:"print " sqrt(2) = ", sqrt(2)print " pi = ", piprint " sin(pi/2) = ", sin(pi/2)print " exp(1j*pi/2) = ", exp(1j*pi/2), " - equal to 0+1j within rounding errors"
With such modules Python programming gets a little more messy, but only with those one can
do powerful work.
-
8/8/2019 Python for Lecture V03
6/14
Python for Lecture
- 6 -
2.4 Tutorials
For those who cant 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.htmlor
http://diveintopython.org/.
-
8/8/2019 Python for Lecture V03
7/14
Python for Lecture
- 7 -
3 Modules, Plotting, etc.
3.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!
import pylab # include function e.g. as pylab.sin(0)# module pylab is a part of module 'matplotlib'
print "sin(pi/6) =", pylab.sin(pylab.pi/6)# work with complex numbers - be careful:print "square-root of -2 =", pylab.sqrt(-2) # gives "not a number"-output!print "square-root of -2+0j =", pylab.sqrt(-2+0j) # gives complex number
With
import pylab
the module named pylab gets linked to the program. It includes many mathematical functions.
If a module is included in that way, all functions (subroutines) of that module have to be called
by giving the module name together with the function name, e.g. pylab.sin or pylab.pi. This is
cumbersome, but ensures that even if two functions inside two different modules have the same
name, they will not be mixed up.Nevertheless, in most cases one likes to have a compact naming for functions. In that sense it
is possible to use
from pylab import *
which means that all function (because of *) from the module are imported directly with their
names without any prename, e.g. directly sin or pi. See the following example.
# prog_06_module_2.py
from pylab import * # include function e.g. as sin(0)
print "sin(pi/6) =", sin(pi/6)# work with complex numbers - be careful:print "square-root of -2+0j =", sqrt(-2+0j) # gives complex numberprint "exp(1) =", exp(1)
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
from pylab import pi,sin,exp # include function e.g. as sin(0)
print "sin(pi/6) =", sin(pi/6)
print "exp(1) =", exp(1)
A compromise between the options above is the import statement as in:
-
8/8/2019 Python for Lecture V03
8/14
Python for Lecture
- 8 -
# prog_07_module_3.py
import pylab as p # include function e.g. as p.sin(0)
pi = p.pi # here a new name is assigned to p.piprint "sin(pi/6) =", p.sin(pi/6)print "exp(1) =", p.exp(1)
Here to all pylab functions the prename p is assigned to have a short form, but still to reflect
from which module the functions are coming. With that form of import, one and the same
function can be important from different modules, like in:
# prog_07b_module_3b.py
import pylab as p # include function e.g. as p.sqrt (0)import numpy.lib.scimath as s # include function e.g. as s.sqrt (0)
# work with complex numbers - be careful:print "pylab square-root of -2 =", p.sqrt(-2) # nan (not a number)
print "scimath square-root of -2 =", s.sqrt(-2) # complex numberThe last example shows that the square root function for negative numbers behaves differently
in different modules, as these modules were written by different persons or groups and for
different purposes. This has disadvantages and makes a careful handling of functions from
different modules very important.
3.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).
Lets 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 matplotlibmatplotlib.use('TkAgg') # define backend before pylab import!
from pylab import * # include function e.g. as sin(0)
x = linspace(0,2*pi,50) # defines a number array from 0 to 2*pi with 50 pointsy = sin(x) # calculate the sin for all individual elements of the array
figure(1) # num=1plot(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!
-
8/8/2019 Python for Lecture V03
9/14
Python for Lecture
- 9 -
If the separate window has not opened up, dont 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 lets 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+Matplotlibover 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.
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 acommon namespace without any barrier in between.
-
8/8/2019 Python for Lecture V03
10/14
Python for Lecture
- 10 -
Using matplotlib in a python shell: http://matplotlib.sourceforge.net/users/shell.html:
By default, matplotlib defers drawing until the end of the script because drawing can be an
expensive operation, and you may not want to update the plot every time a single property is
changed, only once after all the properties have changed.
But when working from the python shell, you usually do want to update the plot with every
command, eg, after changing the xlabel(), or the marker style of a line. While this is simple inconcept, in practice it can be tricky, because matplotlib is a graphical user interface application
under the hood, and there are some tricks to make the applications work right in a python shell.
What is a backend: http://matplotlib.sourceforge.net/faq/installing_faq.html#what-is-a-backend:
A lot of documentation on the website and in the mailing lists refers to the backend and many
new users are confused by this term. matplotlib targets many different use cases and output
formats. Some people use matplotlib interactively from the python shell and have plotting
windows pop up when they type commands. Some people embed matplotlib into graphical userinterfaces like wxpython or pygtk to build rich applications. Others use matplotlib in batch scripts
to generate postscript images from some numerical simulations, and still others in web
application servers to dynamically serve up graphs.
To support all of these use cases, matplotlib can target different outputs, and each of these
capabililities is called a backend; the frontend is the user facing code, ie the plotting code,
whereas the backend does all the dirty work behind the scenes to make the figure. There are
two types of backends: user interface backends (for use in pygtk, wxpython, tkinter, qt, macosx,
or fltk) and hardcopy backends to make image files (PNG, SVG, PDF, PS).
There are a two primary ways to configure your backend. One is to set the backend parameter
in you matplotlibrc file (see Customizing matplotlib):backend : WXAgg # use wxpython with antigrain (agg) rendering
The other is to use the matplotlib use() directive:
import matplotlibmatplotlib.use('PS') # generate postscript output by default
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 matplotlibmatplotlib.use('TkAgg') # define backend before pylab import!
-
8/8/2019 Python for Lecture V03
11/14
Python for Lecture
- 11 -
# works also if the two lines above are absent!! - some default settings then??
from pylab import * # include function e.g. as sin(0)
help(linspace) # gives help on 'linspace'x = linspace(0,2*pi,50) # defines a number array from 0 to 2*pi with 50 pointsy = sin(x) # calculate the sin for all individual elements of the array
figure(1,(10,7.5),90) # num=1, figsize=(8,6), dpi=90clf(), hold(True)plt1 = plot(x,y,'*-', linewidth=3) # linewidth = 2setp(plt1, 'color', 'r', 'linewidth', 2.0) # set plot attributes like matlabsetp(plt1, 'markersize', 10, 'markerfacecolor', 'g', 'markeredgecolor', 'r')xlabel('x')ylabel('sin(x)')title('Example Plot 2')show()
The program plotting_with_matplotlib_2.py has been distributed within our online discussiongroup and shows many more plotting possibilities!
3.3 Statements, control flow, indentation, advantage
See e.g.: Python_(programming_language)#Statements_and_control_flow
A Python script consists of a series of statements and comments. Each statement is a
command that is recognized by the Python interpreter and executed. Statements are usually
one line long, though there are a few ways to make them extend over several lines
(http://www.dalkescientific.com/writings/NBN/python_intro/statements.html).
Python's statements include (among others):
The if statement, which conditionally executes a block of code, along with else and elif (a
contraction of else-if).
The for statement, which iterates over an iterable object, capturing each element to a local
variable for use by the attached block.
The while statement, which executes a block of code as long as its condition is true.
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
Control flow
The for statement controls the program flow in the following example. The statement block
inside the for loop is executed until all given values in the list have been taken. The important
-
8/8/2019 Python for Lecture V03
12/14
Python for Lecture
- 12 -
thing is that the block inside the for loop is not marked by braces or an end statement, but it is
rather distinguished by indentation.
# prog_11_control_flow_1.py# [email protected]
n_sum = 0for 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 loopprint " sum from 1 to %g: %g \n" % (n, n_sum) # sum of all
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 )
An example with the if-elif-else statement:
# prog_11b_control_flow_2.py# the 'if-elif-else statement'
n = int(raw_input("Please enter a positive or negative integer: "))if n
-
8/8/2019 Python for Lecture V03
13/14
Python for Lecture
- 13 -
(Introspections means in German language: Selbstbeobachtung, Innenschau, )
One module that provides insightful information about Python itself is the sys module. You
make use of a module by importing the module and referencing its contents (such as variables,
functions, and classes) using dot (.) notation.
The sys module contains a variety of variables and functions that reveal interesting details
about the current Python interpreter. Let's take a look at some of them. The first thing we'll do isimport the sys module and ask for sys.platform.
Again, we're going to run Python interactively and enter commands at the Python command
prompt. The first thing we'll do is import the sys module. Then we'll enter the sys.executable
variable, which contains the path to the Python interpreter:
If one runs Python interactively and enters commands at the Python command prompt, this
looks like
>>> import sys>>> sys.executable'/usr/local/bin/python'
We can do the same if we write a small program in the editor and then execute the file.
import sysprint 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_12_sys_module.py# [email protected]
# see also http://www.ibm.com/developerworks/library/l-pyint.html
import sysprint "computer platform: ", sys.platform # computer platform: win32, linux, ...print "python interpreter: ", sys.executable # python interpreter usedprint "phython version:", sys.version_info # python version# print sys.argv# print sys.path
print "" # empty lineprint "In order to activate the output window,\click with mouse into this window here" # this statement extends over two lines
raw_input("and then press return to continue!\n")
print sys.modules # this gives a long line containing actually a python dictionaryprint "the type of sys.modules is: ", type(sys.modules)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]
## print whole list#print L
## print all entries of dictionary sys.modules#for entry1, entry2 in sys.modules.items():
-
8/8/2019 Python for Lecture V03
14/14
Python for Lecture
- 14 -
#print " %s: %s " % (entry1, entry2)