Python is an easy to use programming language. It requires special software to run Python programmes, known as the Python Environment. This section will cover installing this environment and how to start using it.
Extra: See here for why Python is used.
The next section will teach how to programme in Python.
Installation
Linux
Use your package manager/software centre to install ‘python’ (ensure that you install Python version 3, as on some systems, like Debian and Ubuntu, it is called ‘python3’ rather than ‘python’).
In a terminal (a command line/shell, look for Konsole, GNOME Terminal, or a programme named console or Terminal in your programmes menu) and in it type (excluding the $ symbol and the space after it):
$ python --version
Then press [ENTER] – this process is known as executing a command. It will print the default version of Python for your system, you should see the first number being a 3.
Python 3.3.2
If you do not, try typing the following instead:
$ python3 --version
If you get:
bash: python3: command not found
You haven’t installed Python 3 in any form, try installing the ‘python3’ package from your package manager (that package is available in general on the major Linux distros on which Python 3 is not the default Python version) and then using the commands above check that the installation succeeded.
Remember the command that was the command for Python 3 (python
or python3
), as in future when you are told to run the command python
, substitute it for python3
if you need to do so for your system.
Note: Alternatively always use python3
in the place of using the python
command.
e.g. Instead of:
$ python --something
Type the following instead:
$ python3 --something
Windows®
For Windows download an installer for Python 3 at www.python.org/download.
Run the installer; When you are installing and see the following screen:
Change the setting (click on the x) of ‘Add python.exe to Path’ and from the drop down menu that appears select ‘Will be installed on local hard drive’:
Resulting in:
Checking that it works
I assume that you can use a plain text editor (see here).
In your text editor of choice enter:
print("Hello World!")
Save it as file called “hello.py” – note the file extension.
Executing the Python programme
Open up a terminal/command line:
Linux
Open your terminal or console – look for look for Konsole, GNOME Terminal, or a programme named console or Terminal in your programmes menu (what it is depends on your system configuration and Linux distribution).
Windows
Open the command prompt – press the [Start Key] + [R], then type ‘cmd’ and press [ENTER].
Running the programme
Note the location that you saved your file, for me – on Linux – “~/Programming/Python”. The path must be either relative to your user folder (Linux), or absolute (Windows or Linux). An example location for Windows could be “C:\Users\Invincitech\Programming\Python”.
Aside: The way you refer to the location of the hello.py file can be done other ways, but for the simplicity of this tutorial the above restrictions on paths apply. If you wish you can refer to the directory/folder in another way – with no impact on the tutorial.
Type and then press [ENTER] (execute the command)
cd "[directory/folder containing file]"
For me:
cd "~/Programming/Python"
Then execute
python hello.py
This tells Python to run the hello.py
Python programme, this works for any Python programme, just substitute hello.py for the name of your programme (called a script – as the code is not converted into a digital language in advance it being run, instead it is converted as it is run).
When the script is run the following will outputted on your command prompt/terminal.
Hello World!
If the above all went correctly, you have successfully set up your Python environment and can now run Python scripts without any trouble.
Why output “Hello World!” ?
It is the most common programme that is a new programmer’s (or a new one in a specific programming language) first programme. The programme introduces the new programme to the look and feel of the language – not so much in Python though, it is only one line.
The practice of using a programme that outputs “Hello World!” is derived from a programming book called ‘The C Programming Language’ (by Kernighan and Richie), in which the first programme in it was the C programming language equivalent of what hello.py did. The simple programme has since then become the classical way to start programming in many language.
Extra: The book received a large circulation among C programmers, thereby popularising the programme and the technique of ‘Hello World!’ as a C programmer’s first programme. C was very popular at the time of publication, and it is still a major programming language, the practice as a result became widespread and affected the future teaching of languages – the learner’s first programme is the “Hello World!” programme – (though for various reasons it didn’t affect all languages).
Explanation of print…
Delegated to the next section (alongside related content).
Terminology to know
- Run a Python script – in a terminal/command line execute
python [script.py]
(in the script’s directory, or by naming the script by its location –"~/Programming/[script.py]"
) - Open the Python interpreter – in a terminal/command line execute the command
python
.