Functions Review
How cell size affects the surface area to volume ratio.
The size of living cells is limited by the surface area to volume ratio. The following function calculates the ratio of a rectangular solid given its width, height, and length.
def surface_area_to_volume_ratio(width, height, length): surface_area = 2 * ((width * height) + (width * length) + (height * length)) volume = width * height * length return surface_area / volume
There is a program called sa_explorer.py in the
example program repository
that you can run to see how different cell sizes affect the surface area
to volume ratio.
Exponential growth with functions.
If you start with one bacterium, how many do you have after a certain number of generations? If you assume that each bacterium divides every generation, and none of them die, this program will print out the generations.
num_generations = 100 num_bacteria = 1 for generation in range(1, num_generations + 1): print(f"Generation # {generation} population = {num_bacteria}") num_bacteria *= 2
It turns out that the number of bacteria after n generations
is 2 ** n. Therefore, we can have a function to calculate the
population quickly using this definition.
def population(generation): return 2 ** generation
Functions quiz on Monday, April 27th.
We will have a quiz over functions on Monday, April 27th. It will be multiple choice with 30 questions and will count 15 points. Here are the things you need to study: