This does two things for us:
%pylab inline
# Quiz 1: question 6 returns True
6 != 5 or 5 > 4 and 4 == 3
# Because "and" is higher than "or", this is evaluated as True or (True and False)
True or True and False
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
plot(x, y)
# "xlabel" adds a label to the x-axis
xlabel('x')
# "ylabel" adds a label to the y-axis
ylabel('y')
# "title" adds a title at the top of the graph
title('y = x*x')
This is more efficient for long iterations
# "xrange" works just like range in a "for" loop
for i in xrange(5):
print i
# "xrange" takes the same parameters as "range"
# So, we can specify a start, step and step
for i in xrange(2, 10, 2):
print i
# "break" terminates a loop completely and jumps to the following code
# Note that strings are also containers - they contain a sequence of characters
# So, we can loop over all the characters in a string
for char in 'abcde':
if char == 'c':
print "Letter c found"
break
# "continue" just terminates the current iteration, and continues the loop at the next item
# The even numbers are not printed, because the "print" statement is never reached
for x in xrange(10):
if x % 2 == 0:
continue
print x
# We can use "continue" to ensure that the loop still runs to the end
i = 0
total = 0
for char in 'abcdcjbssajhchjcvjavjce':
if char == 'c':
print "Letter c found at position", i
total += 1
i += 1
continue
i += 1
print total, "c's found"
$x_{t + 1} = b(1 - x_t)x_t$
# x is the initial population
# b is the maximum growth rate
# t is the number of years to run
def mayfly(x, b, t):
for i in range(t):
print x
x = b*(1 - x)* x
# With b = 1.5, the population converges to a single stable state
mayfly(.5, 1.5, 20)
# With b = 2.5, the population still converges to a stable state, but at a higher population
mayfly(.5, 2.5, 20)
def plot_mayfly(x, b, t):
for i in range(t):
plot(i, x, 'ro')
x = b*(1 - x)* x
plot_mayfly(.5, 1.5, 20)
plot_mayfly(.5, 2.5, 20)