mylist = ['abc', 42, 3.14] # Create a list with surrounding square brackets
mylist
mylist = [] # Create an empty list
mylist
mylist.append('a') # "append" adds an item to the end of a list
print(mylist)
mylist.append('b')
print(mylist)
a = 1
while a <= 5000:
b = 1
while b <= 5000:
# test if (a, b) form PPT
b += 1
a += 1
for item in ['abc', 42, 3.14]: # "for" loops iterate over the items in some sequence
print(item)
for i in range(5): # range(n) starts at zeo and ends at n - 1
print(i)
for i in range(1, 5): # range(m, n) starts at m and ends at n - 1
print(i)
for a in range(1, 5001): # Something like this will be needed for Exercise 3 in Report 1
# Blah
for i in range(2, 11, 2): # The third argument to range can be used to specify a step size between integers
print(i)
%matplotlib inline # Generate graphs in notebook rather than separate window
import matplotlib.pyplot as plt # Standard way to import plotting functions
Create two lists and plot them:
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,4,9,16,25,36,49,64,81,100]
plt.plot(x, y, 'r.', ms=20)
Plotting with square markers
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,4,9,16,25,36,49,64,81,100]
plt.plot(x, y, 'rs', ms=20)
The default plot is 'b-' i.e. blue lines connecting points in the sequence
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,4,9,16,25,36,49,64,81,100]
plt.plot(x, y) # No options given, so defaults are used
Use the help system to explore what a function does and what the keyword options are.
? plt.plot
Add a title, x label and y label. These are all strings.
plt.plot([1,2,3,4,5], [1,4,9,16,25], 'ro')
plt.title("This is the title")
plt.xlabel('x')
plt.ylabel('y')
plt.plot([1,2,3,4,5], [1,4,9,16,25], 'ro')
plt.title("This is the title")
plt.xlabel('x')
plt.ylabel('y');
Change the figure size to 8 units wide, 4 units high using plt.figure(figsize=(width, height)).
plt.figure(figsize=(8,4))
plt.plot([1,2,3,4,5], [1,4,9,16,25], 'ro')
plt.title("This is the title")
plt.xlabel('x')
plt.ylabel('y');