The Mission
A Great Discovery¶
During one of our recent space explorations, we have discovered the existence of non-sentient alien lifeforms! Our mission team has already made some initial observations about their behaviour.
Initial Observations¶
- Each day they gather in groups of ten to feed and reproduce
- Excess individuals can not form a group.
- We suspect that any population with less than ten members can not sustain itself.
- Each individual eats 250g of food per day.
- They will produce one offspring per group per (earth) day if excess food is available.
- If the food is insufficient, one of the group will perish each day.
- The rates of population change seem not to be influenced by the amount of food surplus or shortage
- At the end of a day these groups get dissolved, and the next day starts with new groups of ten again.
Your Mission¶
Our task is to build a growth model simulation that can predict the population of our new (hopefully) friends under given circumstances.
Starting the Work on our Program¶
The Python programming language can be used in two ways:
- Writing a file with code in it, which then gets processed by the Python interpreter.
- Entering an interactive session (called REPL) with the interpreter and issuing the commands one-by-one with direct feedback.
In practise you will often use the first option for code you intend to keep and the second to experiment around. Many tools also allow you to also continue with an REPL after procesing a file.
Way 1: Running Python from a file¶
To write a Python file, any plain-text editor is sufficient (although having a dedicated tool for Python is extremely helpful).
Python files themselves are plain text files with the ending .py
.
To run them, most tools most tools have a dedicated button.
Alternatively you can pass them into the python command on the command line.
Way 2: REPL¶
The abbreviation stands for Read user input, Evaluate, Print result, Loop to the first step. It describes an interaction that feels like an text-chat with a computer.
Many tools offer a Python console window for that purpose or you can run the command python
(without any added parameters) on the command line.
ipython
There is also a tool called [ipython
][ipyrhon] available for the command line which is quite more comfortable when doing REPL from the command line.
Now you!
Here is a very first Python program to try out:
Try to run it as a REPL and by writing it into a file hello_world.py
and running the file.
For our current project we will start by creating a file growth_model.py
and add some initial output so the user knows what the program is about.
We will learn the details of how this works later on.
For now, we need to understand that print(…)
is a function that outputs a given value on the screen.
The "
around Hello World
denotes literal text as opposed to something that might be program code.
Key Points
- Python code can either be written in files or used interactively in a REPL
- Python files are plain text and end in
.py
- Writing code in files is good for storing and sharing code
- REPL is practical for experimentation