Skip to content

Tuples

One Day in a nice Package

Let’s put all the code to simulate one day into its own function as well, in preparation for simulating multiple days later on. To simulate a day, we need the population and food available at the beginning of the day and the function then should return the population and remaining food at the end of the day. We will do the adding of food between the days, so it should not be part of the function.

Our new function definition should also be placed at the beginning of the file, so we can refer to it later.

def simulate_day(initial_population, initial_food):
    """ Simulate the passing of one day.

    Don't forget to feed the aliens between two simulation steps!

    Args:
        initial_population:
            The population at the beginning of the day.
        initial_food:
            The amount of food available at the end of the day.
    Returns:
        A 2-element tuple containing the population at the end of the day 
        and the remaining food at the end of the day.
    """
    excess_food = initial_food - initial_population * FOOD_PER_INDIVIDUAL
    number_groups = initial_population // INDIVIDUALS_PER_GROUP
    print(initial_population, "individuals,", initial_food, "food, leaves", excess_food, "food")
    print("There are", number_groups, "groups")
    will_grow = excess_food > 0
    will_shrink = excess_food < 0

    if will_grow:
        new_population = initial_population + number_groups  # Add one member per group
        print("Population grows to", new_population, "individuals")
    elif will_shrink:
        new_population = initial_population - number_groups  # Remove one member per group
        print("Population shrinks to", new_population, "individuals")
    else:
        new_population = initial_population
        print("Population is stable at", new_population, "individuals")
    if excess_food < 0:
        excess_food = 0

    return (new_population, excess_food)  # See note (1)
Something interesting is going on here: 1. To return multiple values at once we packed them into a tuple.

Note also that instead of using the variables current_population and current_food here, we instead use the parameters initial_population and initial_food. This is done to keep the function as self-contained as possible and to avoid unwanted side-effects.

What is a Tuple?

A tuple is a data type that can hold multiple distinct elements. There are more data types of this kind with slight differences regarding their properties and behaviour. You can refer to the data types overview for all the details.

With tuples the individual values inside it can be accessed by an index.

current_population = starting_population
current_food = food_per_day  # Don't forget to feed them on the first day 

first_day_result = simulate_day(current_population, current_food)
current_population = first_day_result[0]
current_food = first_day_result[1]

Alternatively, Python is clever enough to match the individual elements if the variable used to store the result is a tuple as well. This is called unpacking.

current_population = starting_population
current_food = food_per_day  # Don't forget to feed them on the first day 

(current_population, current_food) = simulate_day(current_population, current_food)  # This will assign both variables at once

Key Points

  • Tuples can be used to pack multiple values together in one variable.
  • The individual elements can be accessed by an index, which starts counting from 0.
  • To take the tuple apart, unpacking is also an option.
Code Checkpoint

This is the code that we have so far: