# Import NumPy and Matplotlib functions for plotting
# We used %pylab in class. This generates graphs in a separate window.
# %pylab inline generates graphs in the notebook itself
%pylab inline
# x and y are arrays of 200 uniform random numbers from the interval [0, 1)
x = random.rand(200)
y = random.rand(200)
# Plot y against x
# The 'r.' format string means use 'red dots'
# The ms=10.5 keyword argument sets the marker size to 10.5
plot(x, y, 'r.', ms=10.5)
# Create an array containing three numbers randomly selected from [0, 1)
random.rand(3)
# Create a list using surrounding square brackets
mylist = ['abc', 42, 3.14]
print mylist
# Empty lists can be created
mylist = []
print mylist
# appends alters an existing list by adding an item to the end
mylist.append('a')
print mylist
mylist.append('b')
print mylist
# Adding two lists "concatenates" them, joining the lists together
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list1 + list2
# Lists can be extended using the "+=" operator
list1 += list2
print list1
# Lists can also be extended using "extend"
list1.extend(list2)
print list1
# Plotting takes either lists or arrays
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plot(x,y)
# range(n) returns the list [0, 1, 2, ..., n - 1]
range(5)
# range(start, stop) returns the list [start, start + 1, ..., stop - 1]
range(1, 5)
# range(start, stop, step) returns a list of integers with an interval of "step"
range(2, 11, 2)
mylist = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print mylist
# List elements are accessed by position using square brackets, starting at zero
# So, the first element is at index 0
mylist[0]
mylist[1]
mylist[6]
# Negative indices count backwards from the end of the list
mylist[-1]
mylist[-2]
# List elements can be modified using the "=" assignment operator
mylist[0] = 'z'
print mylist
# Modify the list back to its original state
mylist[0] = 'a'
print mylist
# list[start:stop] returns all elements from start up to (not including) stop
mylist[1:4]
# list[start:stop:step] returns elements separated by "step"
mylist[0:7:2]
# Missing out "start" selects from the start of the list
mylist[:3]
# Missing out "end" selects to the end of the list
mylist[3:]
# One way to select every second element from the whole list
mylist[::2]
# This is one way to reverse a list (select everything with a step of -1)
mylist[::-1]
list2 = mylist[1:4]
print list2
# Modifying the slice doesn't modify the original list
list2[0] = 'ha ha'
print list2
print mylist
# The variable "item" gets bound to each element of the list in turn.
# The code inside the "for" loop is executed once for each item.
mylist = ['abc', 42, 3.14]
for item in mylist:
print item
# "print" causes a text string to be printed in the notebook
# "return" is the value actually returned by the function
def myrange(n):
mylist = range(n)
print "The list is", mylist
return mylist
# Note that the list is printed using the string '"The list is", mylist'
# But, the output of the function is the value after the "return" statement
myrange(5)