AMATH Quiz 3
If statements
Determine the result of if-then statements by tracking the value of variables.
m
For loops
Write a for loop iterating over an array.
Ex:
for variable in np.arange(array): #Formula or command goes herenp.arange(x) creates an array with x numbers inside
Remember arrays start counting at 0, so np.arange(20) would start at 0 and end at 19
Write a for loop using range.
Ex:
for variable in range(range): #Formula or command goes hereIt should be the default when creating a for loop where a variable increases by 1 each time
Recognize when a for loop should be used
A for loop is an efficient way of repeatedly doing the same process on different parts of a variable
Trace the flow of a for loop by keeping track of the value of variables.
Create an array of zeros in Python using np.zeros()
np.zeros(x) creates an array with x amount of zeroes
Populate a 1D array using a for loop.
Ex:
for year in range(years + 1): if np.isin(year, count_years): model_pop[model_pop_index] = model_calculation #Think f(x) = function
Determine the result of a for loop with an if statement
Plotting data
Create a plot with markers from two arrays.
Ex:
fig, ax = plt.subplots()
ax.plot(count_years, model_pop, marker = 'o', linestyle = 'None', label = 'Model Population')
ax.plot(count_years, count_pop, marker = 'o', linestyle = 'None', label = 'Count Population')
ax.set_xlabel('Years after 1958')
ax.set_ylabel('Number of rabbits')
ax.set_title('Rabbits in Australia: Count vs. Model Comparison')
ax.legend()
fig.savefig('Rabbits.png')Determine when to use a plot with markers.
When you don’t have enough data for a graph to be actually smooth
If you have a line, you should be able to point at any spot and get accurate information, so you should use markers only when that’s not possible
Markers show what data you do have
Python functions and anonymous functions
Recognize how using functions improve modularity and reusability
Functions provide an efficient way to find different information from the same variables without changing them every time
Create and call python functions.
Ex:
def f(x):
y = x**2 + 3
return y
# Use the function f
f_output = f(2)
print("The function f says that f(2) =", f_output)Recognize variables that are defined globally vs. locally.
A variable INSIDE a function is local while a variable OUTSIDE a function is global.
Create and use anonymous functions.
Ex:
f = lambda x: x**2 + 3 print(f(2))Ex:
add_two_numbers = lambda x, y: x+y a = 2 b = 3 print(add_two_numbers(a, b))