Exploring some statements

In [1]:
a = 34 
b = 34
print(a + b)
68

In [2]:
a + b
Out[2]:
68
In [4]:
c = 100
c + b
Out[4]:
134

Refactoring our fifo code to get rid of use of global variable

In [6]:
q = []
In [14]:
def length(q):
    """Returns number of waiting customers"""
    return len(q)

def show(q):
    """SHOW queue: print list of waiting customers. Customer waiting longest
    are shown at the end of the list."""
    print("Customers waiting are:")
    print(q)

    
def add(q, name):
    """Customer with name 'name' joining the queue"""
    q.append(name)


def next_ (q):
    """Returns name of next customer to serve, removes customer from queue"""
    return q.pop(0)
In [19]:
# main programme starts here
add(q, "Hawke")
add(q, "Fangohr")
show(q)
print("Length of queue is {}".format(length(q)))
print("Next customer is {}".format(next_(q)))
#print("Next customer is {}".format(next_(q)))
print("Length of queue is {}".format(length(q)))
Customers waiting are:
['Fangohr', 'Hawke', 'Fangohr', 'Hawke', 'Fangohr', 'Hawke', 'Fangohr']
Length of queue is 7
Next customer is Fangohr
Length of queue is 6

Onto an example with an equation

In [23]:
import math


def mexhat(t, sigma=1):
    """Computes Mexican hat shape, see
    http://en.wikipedia.org/wiki/Mexican_hat_wavelet for
    equation (13 Dec 2011)"""
    c = 2. / math.sqrt(3 * sigma) * math.pi ** 0.25
    return c * (1 - t ** 2 / sigma ** 2) * \
        math.exp(-t ** 2 / (2 * sigma ** 2))

The mexhat function implements the equation $$ f(t) = c\left(1 - \frac{t^2}{\sigma^2}\right) \exp\left(-\frac{t^2}{2\sigma^2}\right) $$

In [21]:
import numpy as np

def create_xs_ys(tmin, tmax, N):
    xs = np.linspace(tmin, tmax, N)
    ys = [mexhat(x) for x in xs]
    return xs, ys
In [30]:
xs, ys = create_xs_ys(-5, 5, 1000)
In [28]:
%matplotlib inline

import pylab

def plot(xs, ys):
    pylab.plot(xs, ys)
    pylab.xlabel('t')
    pylab.ylabel('f(t)')
    
In [31]:
plot(xs, ys)
In []: