Computing (Python) Bootcamp
[Link]
Nat Butler; natbutler@[Link]
Today: 3 mini lectures, 20 minutes of me, 30 minutes of you
1 Intro to python, walk-through + coding breakout
2 Numerical Computing (numpy) + 2 coding breakouts
3 Scientific Computing (scipy) + 2 coding breakougs
Nat Butler Python Bootcamp 11 August 2014
What is Python?
A general-purpose, high-level programming language (wikipedia).
* initial version, late 80's: Guido van Rossum
* Python 2.0: 16 Oct 2000
current versions 2.7.*
* Python 3.0: 3 Dec 2008
mostly backwards compatible with 2.7, but no always.
Language core (CPython) is open source, written C.
All capabilities NOT built into language's core: module import
"scripting" language (i.e., not compiled):
dynamic typing, dynamic memory management
Many programming styles (e.g., functional, object-oriented)
Nat Butler Python Bootcamp 11 August 2014
Why Python
* Well-designed, well-supported language
* Readable
* Access to high and low-level programming
* Python as glue (language interoperability)
* Well documented
- Hierarchical modules, clean namespaces
- Data structures
- Many available libraries (e.g., numpy, scipy)
- Testing framework
→ Complete programming solution,
excellent for tiny and large projects
* Open source and completely free
* Runs natively on Windows, Mac, linux, you name it
Nat Butler Python Bootcamp 11 August 2014
Walk-through
Typing at the python prompt.
Windows Users:
Look for “PyLab” on your desktop.
- if not present, click on icon in lower left:
type “command”, click on “Command Prompt”
cd /python27/scripts
ipython –pylab
Others:
ipython --pylab
See, e.g., [Link]
Nat Butler Python Bootcamp 11 August 2014
Types of Languages
High level / Low level
(human readable / machine readable)
2+3 versus { read 010 from memory location 2001 into cpu,
read 011 from memory location 2002 into cpu,
add the numbers in the cpu,
101 stored in memory location 2003 }
C = A + B high level statement is translated to
machine level statement (e.g., by compiler)
Nat Butler Python Bootcamp 11 August 2014
Types of Languages
Interpreted Language:
Interpreter simulates a computer that
understands high-level language
(No machine language step)
Interpreter analyzes and executes the code
(each time it is run!)
→ slower
→ interactive/flexible programming
→ less dependent on hardware
Nat Butler Python Bootcamp 11 August 2014
Now Back to Python...
ipython - -pylab
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
Type "copyright", "credits" or "license" for more information.
IPython 0.12.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', use 'object??' for extra details.
Welcome to pylab, a matplotlibbased Python environment [backend: TkAgg].
For more information, type 'help(pylab)'.
In [1]:
Nat Butler Python Bootcamp 11 August 2014
Now Back to Python...
In [1]: ← Python prompt
(or >>>)
In [1]: print ("Hellow, world")
Hellow, world
← “Statements”
In [2]: print 2+3
5
In [3]: print "2+3=", 2+3
2+3= 5
Nat Butler Python Bootcamp 11 August 2014
Several Statements = A Function
In [4]: def hello():
...: print "Hello" ← define a function
...: print "Computers are Fun"
...:
(indents are part of it)
In [5]: Hit return 2x to exit
In [5]: hello()
Hello
Computers are Fun
In [6]:
Nat Butler Python Bootcamp 11 August 2014
Several Statements = A Function
Functions have parameters which go in the ()'s:
In [6]: def hello(person):
...: print "Hello", person
...: print "How are you?"
...:
In [7]: hello("Mike")
Hello Mike
How are you? Allows customization of
In [8]: function output.
Nat Butler Python Bootcamp 11 August 2014
Now Exit()
In [8]: exit()
And start again. Note:
Our functions are gone!
→ write modules or scripts
(plain text files we can “import” or run)
Nat Butler Python Bootcamp 11 August 2014
Another Function Example, [Link]
Scripts end in “.py”
In [1]: edit [Link]
← Comments #
# File: [Link]
# A simple program illustrating chaotic behavior
def main(): ← Function
print "This program illustrates a chaotic function"
x = input("Enter a number between 0 and 1: ") “main”
for i in range(10):
x = 3.9 * x * (1 - x)
print x
main()
(Will trigger execution)
Nat Butler Python Bootcamp 11 August 2014
Another Function Example, [Link]
In [1]: edit [Link]
Editing... done. Executing edited code...
This program illustrates a chaotic function
Enter a number between 0 and 1: 0.5
0.975
0.0950625
0.335499922266
0.869464925259
0.442633109113
A chaotic
0.962165255337 sequence of
0.141972779362 numbers.
0.4750843862
0.972578927537
0.104009713267
Nat Butler Python Bootcamp 11 August 2014
Inside [Link]
In [1]: edit [Link] Notes:
# File: [Link] Comments ignored
# A simple program illustrating chaotic behavior
def main():
print "This program illustrates a chaotic function"
print a message
x = input("Enter a number between 0 and 1: ")
for i in range(10):
x is a variable
x = 3.9 * x * (1 - x) “for” is loop construct
print x
“body” of loop
main()
Indentation!
Nat Butler Python Bootcamp 11 August 2014
Inside [Link]
x = 3.9 * x * (1 – x)
is an assignment statement
The part on the right-hand side of the “=“ is a
mathematical expression.
* is used to indicate multiplication
Once the value on the RHS is computed, it is
stored back into (assigned) into x
Nat Butler Python Bootcamp 11 August 2014
Now repeat...
In [2]: run [Link]
This program illustrates a chaotic function
Enter a number between 0 and 1: 0.51
0.97461
0.09650685681
0.340053805255
0.875227137667
In [4]: reload(chaos)
0.425897921116 This program illustrates a chaotic function
0.95358463943 Enter a number between 0 and 1: 0.53
0.172617802006 0.97149
0.557001496104 0.10801900161
0.962328234824 0.375768497915
0.141385152811 0.914809482169
0.303939064659
0.82508442757
In [3]: import chaos 0.562848448312
This program illustrates a chaotic function 0.959595282925
Enter a number between 0 and 1: 0.52 0.151211486062
0.97344 0.500551632926
0.10083280896 Out[4]: <module 'chaos' from '[Link]'>
0.353595659029
0.891406498885
0.377523715259
0.916498282736
0.298463803863 Chaotic output!
0.816594330422
0.584095316785
0.94741911301
Nat Butler Python Bootcamp 11 August 2014
Coding Breakout
1 Edit a version of [Link] yourself. Run it.
2 Alter the script file to take x as function
input, that is allow calls like main(0.5)
3 Reload it.
4 Run it for x = 0.5, 0.51, 0,52, 0.53, 0.54
5 Plot the final value from each run versus
the input x value.
(hint: use plot(a,b,'o') for each point, or
something fancier!)
Nat Butler Python Bootcamp 11 August 2014