Simple python examples

What you want What it is called Examples
Outputprintprinting1.py
randomwalk4.py
Loopsforloop1.py
dowhile.py
Command-line inputsys.argv commandline1.py
commandline2.py
randomwalk4.py
Function definitiondef fact.py
randomwalk4.py
randomwalk5.py
Reading in lines from standard inputsys.stdin reading1.py
Substitution using regular expressionsre reading1.py
length of a listlencommandline2.py
Convert string to integerintcommandline2.py
find something in listfind1.py
Sort a listsortsort1.py
Split a stringsplit1.py
Reading lines from a fileopen, readlinesreading2.py
Random numbersrandom.random()randomwalk4.py
randomwalk5.py
Automatic execution__name__, __main__randomwalk4.py
randomwalk5.py
Plotting graphsGnuplot randomwalk5.py
Plotting histogramspylab.hist histo1.py
Automated testingdoctest
(explanation)
DocExample.py
DocExample2.py
AlanDice.py
AlanDice2.py
decomposeSquares.py
Recursion decomposeSquares.py

sort1.py

##
## python/examples/sort1.py
##                                         David MacKay

### How to sort an array and keep track of which
### item went where.

a=[15,13,17,11]

### First make a new list containing the items, and indices. 
c=list( [ a[i],i ] for i in range (len(a)) )

### c is:
### [[15, 0], [13, 1], [17, 2], [11, 3]]

c.sort()
c
### [[11, 3], [13, 1], [15, 0], [17, 2]]

Comments = """
Note that by default, sort will, if asked to compare
two sets, use the first element in the set.

For a good introduction to sort, see
http://www.amk.ca/python/howto/sorting/sorting.html.
"""

David MacKay / python resources /

David MacKay / Computational Physics wiki (UCAM only) / C++ resources /