Theory: PEP 8

How to write code that is clean and easy to read?

That is the question that we bump into when moving from simple single-line programs to more complicated ones. In the beginning, it may seem unimportant, but in real life, programming is a process that involves a lot of people that work together, so we spend more time reading code than writing it.

Although Python is often more readable than other programming languages because of its minimalistic syntax, just syntax itself is not enough. It is the way we write code that affects general readability. That is why we need to follow common conventions about programming style, so other programmers could read your code easily.

we may ask, where do these conventions come from? There is a document that is called PEP 8. The key idea of it is to use the same code style for all python projects as if they were written by the same programmer. This document guarantees that even a beginner will easily understand the code, written by any other developer.

PEPs

Before going further, let’s talk about PEP for a moment. PEP stands for Python Enhancement Proposal. There are different types of PEP and the most useful one for beginners is the informational PEP. PEPs of this kind typically describe commonly accepted guidelines or conventions about the language, so they can be very helpful. Besides PEP 8, which is an official style guide, another great PEP to look at is the Zen of Python.

Since we know what PEP 8 is, let’s read a bit from it.

The length of a line

Do not use more than 79 characters in a line of code. Shorter lines look better in code editors. During this course, we will learn several ways to achieve it.

Avoid extra spaces

Sometimes we may add some spaces even if we don't really need it. This will reduce the readability of our code.

  • Avoid extra spaces within parentheses.

Good:

print('Hello!')

Bad:

print'Hello!' )
  • Avoid an extra space before an open parenthesis.

Good:

print('some text')

Bad:

print ('some text')

Quotes

As it was already mentioned, we can use either single or double quotes to define strings. Choose one that we like the most and consistently use it in our code. The only recommendation in PEP 8 is that if a string contains single or double quotes, we should use the other one to avoid backslashes.

Good:

print("It's a good string!")

Bad and harder to read:

print('It\'s a bad string!')

As we can see, the first example is easier to read.

Comments

Popular posts from this blog

Covid-19 Analysis and Visualization using Plotly Express

Artificial Intelligence (Problem Solving)

Linear Regression