Python

Python is a high-level, interpreted programming language that is easy to learn and use. It's known for its simple syntax and readability. It can be used for a wide range of tasks, including web development, data analysis, artificial intelligence, and scientific computing. PEP-8 is the standard for coding conventions in the Python language and must be followed.

Observance should also be made of PEP-20 The Zen of Python.

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!

Consistency is important

A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important.

From the GDS Way.

Linting and Enforcing styles

VS Code integrates well with PEP-8 requirements. It's necessary to install the VS Code Python Extension. Other IDEs are equally capable of handling linting and reformatting.

It may also be expedient to enforce maintainability and coding standards through the use of a pre-commit hook such as this one.

It is recommended to use Flake8 as a combined linter and formatter. It is also advised to adopt a max line length of 120 characters in line with the GDS configuration, in a variance from the PEP-8 convention.

Preferred standards

Binary operators and line breaks

Line breaks before binary operators (Knuth's style)

# Correct:# easy to match operators with operandsincome = (gross_wages          + taxable_interest          + (dividends - qualified_dividends)          - ira_deduction          - student_loan_interest)

Indentation

No single approach for hanging indents is enforced above others, per the PEP-8 guidance. However, the adopted pattern should be internally consistent within any particular project.

Quotes

Prefer single quotes for strings.

Whitespace

If operators with different priority are used, add whitespace around the operators with the lowest priority.

# Correct:i = i + 1submitted += 1x = x*2 - 1hypot2 = x*x + y*yc = (a+b) * (a-b)