Python

Guidance and tips for using Python


What is Python

Python is a high-level, general-purpose programming language. It has a number of uses, some of which are statistical analysis and data visualisation. Python code has a strong emphasis on readability and formatting. Although the majority of analysts in DfE tend to use R or SQL, Python is also available as an option.


How to install Python

You can download Python (language) and PyCharm (IDE) from the DfE software center.


Best places to start

For getting set up, and help through any initial issues we recommend making use of the DfE Python community on Teams.

There is guidance on the Python Wiki for programmers and non-programmers. Each of the guides includes a list of interactive courses that you can work through to improve your skills.

The ONS Data Science Campus offers training courses on Reproducible Analytical Pipelines (RAP) in Python, but be aware that there is less Python expertise in the Department which might make QA of code more difficult.


Best practice

Python is different to other programming languages in that it has its own style guide, PEP-8. PEP-8 is one of many guidance documents known as Python Enhancement Proposals (PEPs) which govern the style of the language, any future changes or updates, and backwards compatibility features. PEP-20 outlines the guiding principles for Python’s design.

Python can be a little fussy about the layout of your code, especially whitespace, and your code may fail if it is not indented correctly.


How to work with Python

Libraries and packages

Python packages work slightly differently to R packages. You might hear reference to libraries in Python. A library is a collection of related packages that perform similar functions. One of the most popular libraries is called NumPy and it allows you to work with numerical data more easily.

To access the features of a library and use them within your code, you need to install the library. Using NumPy as the example, you can do this using the syntax

pip install numpy

PIP is the standard package installer for Python and so must be mentioned at the start of the line of code.

Once you have installed your library, you then need to import it so that you can use it within your code. To make your life easier, you can give it a shortened name so that you don’t have to write NumPy every time you want to use one of the features, like this:

import numpy as np

If you only install a library and then don’t import it, you won’t be able to use its features. You can either import a library as a whole, or you can just import certain functions from it if you know you won’t need to use them all.

NumPy contains an array function. If you want to use it, you would do so as follows:

arr = np.array( [[ 1, 2, 3],[ 4, 2, 5]] )

Note that you must use np. before array to get it to work, otherwise Python won’t know where to look for the array function. When you imported NumPy, if you gave it a different shortened name, you’d use that name instead.

Back to top