Lists
Gather all the Data¶
Lets do some statistics! To collect a bundle of values we do not need individual variables. While a tuple already has a fixed size upon creation, we can use a list instead, since it can become larger and smaller as we go along.
population_over_time = [] # This is an empty list
for current_day in range(START_DAY, START_DAY + simulation_duration):
print("Start of day", current_day)
(current_population, current_food) = simulate_day(current_population, current_food)
current_food = current_food + food_per_day
population_over_time.append(current_population) # Put the new data point into our list
print("Population over time:", population_over_time)
You can access the elements of a list via an index, as with tuples.
Also, lists can be used as a data source in for
-loops, like a range(…)
.
A basic Evaluation¶
There are some nice built-in functions that we can use for some basic statistics. Many of those accept a list as input.
# Calculate some statistical values
gathered_values = len(population_over_time) # Counts the elements in a list
lowest_population = min(population_over_time)
highest_population = max(population_over_time)
average_population = sum(population_over_time) / gathered_values
print("We gathered", gathered_values, "data points")
print("Minimum:", lowest_population, "individuals")
print("Maximum:", highest_population, "individuals")
print("Average:", average_population, "individuals")
Key Points
- Lists can bundle up multiple values
- The size of a list is not fixed and may change as the program progresses