How to write code that is clean and easy to read? That is the question you 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 you spend more time reading code than writing it.
Although Python is often more readable than other programming languages because of its minimalistic syntax, syntax itself is insufficient. It is the way you write code that affects general readability. That is why you need to follow common conventions concerning programming style so other programmers can read your code easily.
You may ask where 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 the same programmer wrote them. This document guarantees that even a beginner will quickly understand the code written by any other developer.
PEPs
Before going further, let’s talk about PEPs for a moment. PEP stands for Python Enhancement Proposal. It is a document providing guidelines and conventions for writing code in Python. There are different types of PEPs. The most useful ones for beginners are informational PEPs. PEPs of this kind typically describe commonly accepted guidelines or conventions for 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.
Now 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 this.
Avoid extra spaces
Sometimes, you may want to add spaces even if you don't need them. However, this will reduce the readability of your code.
Avoid extra spaces within parentheses.
# Good
print('Hello!')
# Bad
print( 'Hello!' )
Avoid an extra space before an opening parenthesis.
# Good
print('some text')
# Bad
print ('some text')Quotes
As was mentioned, you can use either single or double quotes to define strings. Please choose the option you like most and consistently use it in your code. The only recommendation in PEP 8 is that if a string contains single or double quotes, you should use the other type to define the string to avoid backslashes.
# Good
print("It's a good string!")
# Bad and harder to read
print('It\'s a bad string!')
In the latter string, the backslash is an escape character used to indicate that the following single quote is not the end of the string. You will learn more about escape characters later. According to PEP 8, avoiding backslashes improves the readability of the code. So, even though the example with backslashes works, the first example is easier to read.
What’s next?
Later, you will learn many things about Python and become a skillful programmer, but following the code style will always remain important. Do not worry, though: you do not need to learn all the conventions at once; open them from time to time after learning something new. We will also include parts of these conventions in this course.
Summary
In this topic, we learned what PEP 8 is, where to find it and how to use it. Let's briefly go through the main points we discussed:
The length of a line of code shouldn't exceed 79 characters.
Extra spaces should be avoided within parentheses and before an opening parenthesis.
Quotes should be used consistently or to avoid backslashes.