rabbit_pair_count = [1,1]
next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]
rabbit_pair_count.append(next_month)
print(rabbit_pair_count)
num_times_to_run = 4
for i in range(num_times_to_run):
next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]
rabbit_pair_count.append(next_month)
print(rabbit_pair_count)
k = 3
num_times_to_run = 4
rabbit_pair_count = [1,1]
for i in range(num_times_to_run):
next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]*k
rabbit_pair_count.append(next_month)
print(rabbit_pair_count)
k = 3
num_times_to_run = 4
m = 4
rabbit_pair_count = [1,1]
for i in range(num_times_to_run):
if m <= len(rabbit_pair_count):
next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]*k - \
rabbit_pair_count[-m]
else:
next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]*k
rabbit_pair_count.append(next_month)
print(rabbit_pair_count)
def rabbit_population(n_months):
"""
Summary line: Generates the rabbit population for a set number of months
Extended description: This example was generated in class
It doesn't take into account mortality
We assume each pair of rabbits has a pair of offspring 2 months after they are born
Parameters:
n_months (int): Number of months to generate data for
Return:
list: list of numbers of rabbit pairs for each month
"""
assert n_months > 0, "Please specify months > 0"
if n_months == 1:
return [1]
rabbit_pair_count = [1,1]
for i in range(n_months-len(rabbit_pair_count)):
next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]
rabbit_pair_count.append(next_month)
return rabbit_pair_count
rabbit_counts_5 = rabbit_population(5)
print(rabbit_counts_5)
rabbit_population(-2)
help(rabbit_population)
help(print)
def rabbit_population2(n_months,m,k=1):
"""
Summary line: Generates the rabbit population for a set number of months
Extended description: This example was generated in class
It allows for rabbit mortality
We assume each pair of rabbits has a pair of offspring 2 months after they are born
unless otherwise specified
Parameters:
n_months (int): Number of months to generate data for
m (int): number of months the rabbits live
k (int): number of pairs of rabbits produced by each pair in a month
Return:
list: list of numbers of rabbit pairs for each month
"""
assert n_months > 0, "Please specify months > 0"
assert k > 0, "k must be > 0"
if n_months == 1:
return [1]
rabbit_pair_count = [1,1]
for i in range(n_months-len(rabbit_pair_count)):
next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]*k - \
rabbit_pair_count[-m]
rabbit_pair_count.append(next_month)
if next_month == 0:
break
return rabbit_pair_count
pop_m2_k6 = rabbit_population2(100,2,3)
pop_m2_k4 = rabbit_population2(100,2,4)
rabbit_population2(100,2,0)
import matplotlib.pyplot as plt
months = list(range(1,len(pop_m2_k4)+1))
plt.plot(months,pop_m2_k4,pop_m2_k6)
plt.ylabel('Number of Rabbit Pairs')
help(plt.plot)
import pandas as pd
rabbit_df = pd.DataFrame(
{'generation':months,
'rabbit_count1':pop_m2_k4,
'rabbit_count2':pop_m2_k6
}
)
print(rabbit_df[0:5])
rabbit_df2 = pd.DataFrame(
{
'rabbit_count1':pop_m2_k4,
'rabbit_count2':pop_m2_k6
}
)
rabbit_df2.index = months
print(rabbit_df2[0:5])
rabbit_df2.columns
rabbit_df2.loc[10:15]
rabbit_df2.iloc[10:15]
rabbit_df2.rabbit_count1.loc[10:15]
rabbit_df2['rabbit_count1'].loc[10:15]
rabbit_df2.loc[ rabbit_df2['rabbit_count1'] < 50 ]
import plotnine as p9
p9.ggplot(data = rabbit_df2,
mapping = p9.aes(x = 'rabbit_df2.index',
y = 'rabbit_count1')) + \
p9.geom_point()
for i in range(10000000000000):
print(i)
type(range(5))
a=[0,1,2,3]
type(a)
N = 10**12
for i in range(N):
if i>=10:
break
print(i)