1/16
If statements; For loops; Plotting data; Python functions and anonymous functions
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Write a for loop over an array.
for variable in np.arange(array):
#Formula or command goes hereWhere does np.arange(x) start and end?
It starts at 0 and ends at x-1
Write a for loop using range .
for variable in range(range):
#Formula or command goes hereWhich is preferable when creating a for loop that increases by 1 each time? range or np.arange() ?
range is preferred
What does a for loop do?
It is an efficient way of repeatedly doing the same process on different parts of a variable
Create an array of 6 zeros in Python using np.zeros()
N = np.zeros(6)What does np.zeros(x) do?
It creates an array with x amount of zeroes
Populate a 1D array using a for loop.
for year in range(years + 1):
if np.isin(year, count_years):
model_pop[model_pop_index] = model_calculation #Think f(x) = function. model_pop is the arrayCreate a plot with markers from two arrays.
fig, ax = plt.subplots()
ax.plot(x, y, marker = 'o', linestyle = 'None')
ax.plot(x, z, marker = 'o', linestyle = 'None')What do markers do?
They show the data you have
When should you ONLY use markers on a plot?
When you don’t have enough data for a graph to smoothly connect from one point to the next.
How do functions improve modularity and reusability?
They provide an efficient way to find different information using the same variables without changing them every time.
Create and call python functions.
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)When is a variable a LOCAL variable?
When it is INSIDE a function.
When is a variable a GLOBAL variable?
When it is OUTSIDE of a function
Solve for f(x) = x² + 3 when x = 2 using anonymous functions.
f = lambda x: x**2 + 3
print(f(2))Solve for x + y when x = a and y = b
add_two_numbers = lambda x, y: x+y
a = 2
b = 3
print(add_two_numbers(a, b))