Table of contents

Python

Python is a general-purpose programming language.

It has a large standard library (“batteries included”) and 3rd party libraries are distributed through the Python Package Index (PyPi).

Starting out

Which version?

Python 3 introduced a number of backwards incompatible changes, which can be confusing. When learning the language, make sure that the resources you use cover the same version you are using (2.7 or 3.x).

Working with Python Projects

pip is python’s package manager (similar to gem in ruby-land and npm for node.js). The convention is to list dependencies in a requirements.txt file.

By default libraries will be installed for the system python, which may require superuser access.

Instead, you can use virtualenv, which provides an isolated python environment for your project.

$ virtualenv env                       # Create a virtualenv directory, 'env'
$ source env/bin/activate              # Activate the virtualenv
(env)$ pip install -r requirements.txt # Install project dependencies

Python Test Frameworks

Python comes with a built in test framework, unittest.

Pytest and Nose are popular alternatives.

Additional Tools

Reference documentation

Learning materials

Writing idiomatic Python

The Zen of Python

$ python
Python 2.7.3 (default, Feb 18 2013, 13:30:57)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.24)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Code style

The authoritative style guide for python is PEP8.

You can automatically detect style violations with a tool such as Flake8 or Pylint.

This page was last reviewed on 16 November 2018. It needs to be reviewed again on 16 November 2019 .
This page was set to be reviewed before 16 November 2019. This might mean the content is out of date.