MTH 337: Week2

Plotting

In [1]:
# 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
Populating the interactive namespace from numpy and matplotlib
In [2]:
# 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)
Out[2]:
[<matplotlib.lines.Line2D at 0x7ff871144d50>]
In [3]:
# Create an array containing three numbers randomly selected from [0, 1)
random.rand(3)
Out[3]:
array([ 0.06543486,  0.23614282,  0.60206762])

Lists

In [4]:
# Create a list using surrounding square brackets
mylist = ['abc', 42, 3.14]
print mylist
['abc', 42, 3.14]
In [5]:
# Empty lists can be created
mylist = []
print mylist
[]
In [6]:
# appends alters an existing list by adding an item to the end
mylist.append('a')
print mylist
['a']
In [7]:
mylist.append('b')
print mylist
['a', 'b']
In [8]:
# Adding two lists "concatenates" them, joining the lists together
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list1 + list2
Out[8]:
[1, 2, 3, 'a', 'b', 'c']
In [9]:
# Lists can be extended using the "+=" operator
list1 += list2
print list1
[1, 2, 3, 'a', 'b', 'c']
In [10]:
# Lists can also be extended using "extend"
list1.extend(list2)
print list1
[1, 2, 3, 'a', 'b', 'c', 'a', 'b', 'c']
In [11]:
# Plotting takes either lists or arrays
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plot(x,y)
Out[11]:
[<matplotlib.lines.Line2D at 0x7ff871032bd0>]

"range" generates a list of integers

In [12]:
# range(n) returns the list [0, 1, 2, ..., n - 1]
range(5)
Out[12]:
[0, 1, 2, 3, 4]
In [13]:
# range(start, stop) returns the list [start, start + 1, ..., stop - 1]
range(1, 5)
Out[13]:
[1, 2, 3, 4]
In [14]:
# range(start, stop, step) returns a list of integers with an interval of "step" 
range(2, 11, 2)
Out[14]:
[2, 4, 6, 8, 10]

Indexing accesses list elements

In [15]:
mylist = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print mylist
['a', 'b', 'c', 'd', 'e', 'f', 'g']
In [16]:
# List elements are accessed by position using square brackets, starting at zero
# So, the first element is at index 0
mylist[0]
Out[16]:
'a'
In [17]:
mylist[1]
Out[17]:
'b'
In [18]:
mylist[6]
Out[18]:
'g'
In [19]:
# Negative indices count backwards from the end of the list
mylist[-1]
Out[19]:
'g'
In [20]:
mylist[-2]
Out[20]:
'f'

Modifying list elements

In [21]:
# List elements can be modified using the "=" assignment operator
mylist[0] = 'z'
print mylist
['z', 'b', 'c', 'd', 'e', 'f', 'g']
In [22]:
# Modify the list back to its original state
mylist[0] = 'a'
print mylist
['a', 'b', 'c', 'd', 'e', 'f', 'g']

Slicing returns a selection from a list

In [23]:
# list[start:stop] returns all elements from start up to (not including) stop
mylist[1:4]
Out[23]:
['b', 'c', 'd']
In [24]:
# list[start:stop:step] returns elements separated by "step"
mylist[0:7:2]
Out[24]:
['a', 'c', 'e', 'g']
In [25]:
# Missing out "start" selects from the start of the list
mylist[:3]
Out[25]:
['a', 'b', 'c']
In [26]:
# Missing out "end" selects to the end of the list
mylist[3:]
Out[26]:
['d', 'e', 'f', 'g']
In [27]:
# One way to select every second element from the whole list
mylist[::2]
Out[27]:
['a', 'c', 'e', 'g']
In [28]:
# This is one way to reverse a list (select everything with a step of -1)
mylist[::-1]
Out[28]:
['g', 'f', 'e', 'd', 'c', 'b', 'a']

Slicing creates a new list

In [29]:
list2 = mylist[1:4]
print list2
['b', 'c', 'd']
In [30]:
# Modifying the slice doesn't modify the original list
list2[0] = 'ha ha'
print list2
print mylist
['ha ha', 'c', 'd']
['a', 'b', 'c', 'd', 'e', 'f', 'g']

For loops execute the code in the loop once for every element

In [31]:
# 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
abc
42
3.14

The difference between "print" and "return"

In [32]:
# "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
In [33]:
# 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)
The list is [0, 1, 2, 3, 4]
Out[33]:
[0, 1, 2, 3, 4]