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

AlanDice.py

#!/usr/bin/python
# AlanDice package
# Contents:
#   add_dice
#     Given the list of numbers on the faces of two dice, return the
#     number of ways each sum can be created. Uses "transform" and "convolve"
#   transform
#     Converts the list of numbers to the right form for convolve.
# doctest is used to check the documentation.

import scipy as S

def transform( x ):
    """
    This function transforms a list of dice faces
    into a list of counts (of how often each value appears on a face).
    All the faces should have integers in (0,1,...)
    on them.    

    >>> transform( [ 1, 2 , 3 , 4 ] )
    [0, 1, 1, 1, 1]
    
    Note the first answer is zero because the die has no zeroes

    >>> transform( [ 0, 2 , 3 , 4 ] )
    [1, 0, 1, 1, 1]
   
    >>> transform( [ 1, 1, 2, 5, 5, 5] )
    [0, 2, 1, 0, 0, 3]

    >>> transform( [ 3, 3, 0, 1] )
    [1, 1, 0, 2]
    """
    M = max(x)
    # Initialize the counts to zero
    answer=[]
    for m in range(M+1): answer.append(0)
    for face in x:
        answer[face] += 1
    return answer

def add_dice( x, y ):
    """
    Given the FACES of two dice,
    returns the count of the number of ways of making each SUM.

    >>> add_dice( [1, 2, 3, 4], [1, 2, 3, 4] )
    array([0, 0, 1, 2, 3, 4, 3, 2, 1])

    The answer should not depend on the order of the faces.
    
    >>> add_dice( [4, 2, 1, 3], [4, 2, 3, 1] )
    array([0, 0, 1, 2, 3, 4, 3, 2, 1])

    >>> add_dice( [1, 2, 2, 3], [1, 3, 3, 5] )
    array([0, 0, 1, 2, 3, 4, 3, 2, 1])
    >>> add_dice( [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6] ) # doctest:+ELLIPSIS
    array([0, 0, 1, 2, 3, 4, 5, 6, ... 3, 2, 1])

    You can use this function to check what happens when you
    label the dice with different numbers.
    >>> add_dice([1,3,3,5,5,7], [ 1,2,2,3,3,4] )
    array([0, 0, 1, 2, 4, 5, 6, 6, 5, 4, 2, 1])
    
    >>> add_dice([1,2,2,3,3,4], [1,3,4,5,6,8]) # doctest:+ELLIPSIS
    array([0, 0, 1, ... 1])
    """
    return S.convolve( transform( x) , transform( y ) )

if __name__ == '__main__':
    import doctest
    verbose=1 ## you can change this to verbose=0 to make doctest report
    ## only the FAILED tests.
    if(verbose):
        doctest.testmod(None,None,None,True)
    else:
        doctest.testmod()

David MacKay / python resources /

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