5mm.
Numerical Python
Hans Petter Langtangen Simula Research Laboratory
- Dept. of Informatics, Univ. of Oslo
May 2010
Numerical Python – p.1/397Intro to Python programming
Intro to Python programming – p.2/397Make sure you have the software
Python version 2.5 Numerical Python (numpy) Gnuplot program, Python Gnuplot module SciTools For multi-language programming: gcc, g++, g77 For GUI programming: Tcl/Tk, Pmw Some Python modules are handy: IPython, Epydoc, ...
Intro to Python programming – p.3/397Material associated with these slides
These slides have a companion book: Scripting in Computational Science, 3rd edition, Texts in Computational Science and Engineering, Springer, 2008 All examples can be downloaded as a tarfile
http://folk.uio.no/hpl/scripting/TCSE3-3rd-examples.tar.gz
Software associated with the book and slides: SciTools
http://code.google.com/p/scitools/
Intro to Python programming – p.4/397Installing TCSE3-3rd-examples.tar.gz
Pack TCSE3-3rd-examples.tar.gz out in a directory and let scripting be an environment variable pointing to the top directory:
tar xvzf TCSE3-3rd-examples.tar.gz export scripting=‘pwd‘
All paths in these slides are given relative to scripting, e.g.,
src/py/intro/hw.py is reached as
$scripting/src/py/intro/hw.py
Intro to Python programming – p.5/397Scientific Hello World script
All computer languages intros start with a program that prints "Hello, World!" to the screen Scientific computing extension: read a number, compute its sine value, and print out The script, called hw.py, should be run like this:
python hw.py 3.4
- r just (Unix)
./hw.py 3.4
Output:
Hello, World! sin(3.4)=-0.255541102027
Intro to Python programming – p.6/397Purpose of this script
Demonstrate how to get input from the command line how to call a math function like sin(x) how to work with variables how to print text and numbers
Intro to Python programming – p.7/397The code
File hw.py:
#!/usr/bin/env python # load system and math module: import sys, math # extract the 1st command-line argument: r = float(sys.argv[1]) s = math.sin(r) print "Hello, World! sin(" + str(r) + ")=" + str(s)
Make the file executable (on Unix):
chmod a+rx hw.py
Intro to Python programming – p.8/397