Setting up a small Project
Organization is Everything¶
When starting an new project, be it programming or otherwise, it is a good idea to first create a new folder in which all files for the project are placed. In the following, this will be called the top-level or root folder.
Create a new folder in a location where you prefer to have your projects (e.g. your desktop, or documents or home folder…) and call it
In this folder we will put all our files for this example project.
The main thing¶
Python code can be written into one or more files and organized in so called modules.
Python files end with
Each program, be it in one or more files, will need a well-defined starting point.
By convention the file containing this starting point is called
This could be your current structure now:
The double underscores
Surrounding the name of something (files, code elements, …) with double underscores
__ usually holds a special meaning:
This is important for the internal working of Python itself.
You should not lightly name things with
__ and be extra careful when using these things.
With the project folder and the
__main__.py file set up, we are ready to get started.
Sometimes, all you want to do is to have a small file with a few lines of code in it instead of a whole project.
These files are called scripts and instead of a project folder on their own, most developers tend to sort them into a collective folder or put them next to the data files the program works with.
These scripts then also tend to get a more descriptive name, like
Interacting with Python¶
To run any Python code on your computer you need to use an interpreter program. It translates the code into the machine language that your computer uses and also translates the answers back into something you can read. The interpreter program is also called python.
The Python interpreter can be used in two ways:
- Writing a file with code in it, which then gets processed by the 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
To run them, most tools have a dedicated button, that looks like a play-button.
Alternatively you can pass them into the python command on the command line.
Don’t forget to save any changes before running the file or Python will not see the current version.
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.
There is also a tool called ipython available for the command line which is quite more comfortable than the regular
python command when doing REPL.
Here is a very first Python program to try out:
Try to run it as a REPL and by writing it into a file called
hello_world.py and running the file.
Starting the Work on our Program¶
For our current project we will start by creating a project folder called
Inside we add the file
Let’s also add a line of code inside this main file so we can see if everything is working as intended:
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 Alien Growth Model denotes literal text as opposed to something that might be program code.
Try to run the project in your
If you are using the command line, pay attention that your working directory should be the one above your project folder.
- Python code can either be written in files or used interactively in a REPL
- Python files are plain text and end in
- Writing code in files is good for storing and sharing code
- The REPL is practical for experimentation
- Python projects tend to be in their own folder and have a
__main__.pyfile that acts as the starting point
This is the code that we have so far: