Python

How to format Python code using black?

Introduction

You can learn everything you need to know about formatting your code with black. The sample code provided is giving you a sense of how to do it right, but the example below breaks it down even further so that you can easily understand what’s happening. This blog post will explain what Python’s standard syntax does and why this method is more efficient. It will also cover the difference between indentation and spacing, as well as how whitespace affects readability in presented code samples. Do you want to know more about best practices and formatting in python coding please checkout Python Django training in Kochi.

What is Python Standard Syntax?

Google’s Python Style Guide recommends that you use 4 spaces to indent and no tabs. This is the Python Standard Syntax. Many people choose to not follow this standard, but it’s recommended by Google and PEP-8 so you should use it.

How do I format my code using black?

This article will try to keep it simple as possible. If you want more detail be sure to read PEP-8 – Style Guide for Python Code .

First, open up a new project in vscode and press ctrl + shift + p , then type in: Format Document . (The bolded part means that it’s a command.)

It will ask you what type of formatting you want to do. For this example, enter: “Lines of Code” + 1. The ctrl + shift + p key combination should now be highlighted in blue.

Next, select “Lines of Code” from the dropdown menu and enter the number that you would like to format your code to (1).

Finally, press OK. Your lines will now be numbered for you with 3 spaces between them. This is what the standard syntax does in Python: It adds 4 spaces before and after each line (instead of 2), but one space between each line (instead of 3).

Why is this better than the default method?

  • Formatting Code with Spaces

Most people use 2 or 3 spaces to indent code. This will have a negative effect on readability because each line will be aligned under the previous line. When you have multiple lines of code, indenting them in this way makes it difficult to see where one statement ends and another begins. Often you’ll find that people will use tabs as well, which makes it even more difficult to read.

In addition, spacing can be intimidating for new coders because they don’t know how much is enough and how much is too much. If you have a lot of lines of code it can be difficult to use variable names that are more than one or two words. With the default method you could use variables like “num” for an integer, but when you’re using 4 spaces that’s too long. You’ll need to abbreviate it to get it to fit under the previous line. Instead, you can use a more descriptive name like “total_hours” and make your code easier to read.

  • Formatting Code with Tabs

Tabs are fine if they are used correctly, but most coders don’t know how to properly format their code with tabs. If you’re using tabs you need to put one tab between each new line. Try it out with this example:

def take_abs_of(value):

if value > 0:

return “positive”

else:

return “negative”

assert abs(take_abs_of(10)) == 1

assert abs(take_abs_of(-10)) == 9

total = 10 * 6

assert total == 60 * 4

This example uses 4 spaces to indent the code, but when it’s formatted using tabs the result is different. What happened to those extra 4 spaces? The tab characters are only inserting the spaces in front of the numbers and nothing is being inserted after them. This is not the proper way to format code with tabs. The proper way to do this example would be:

def take_abs_of(value):

if value > 0:

return “positive”

else:

return “negative”

assert abs(take_abs_of(10)) == 1

assert abs(take_abs_of(-10)) == 9

total = 10 * 6

assert total == 60 * 4

  • Solid Space Between Each Line

With the default method you want to use as many spaces as you can without lines overlapping and having what’s on the right end of a line start at an unknown location. This is what good spacing will look like in your code. The first line started with a space before it because the indentation was 4 spaces. Then there was a space between the assignment operator and the variable name. Finally, there was a space between the equals sign and the total variable name. Using this method you can add as many spaces as necessary to make sure that variables are written correctly in your code.

  • Spacing Can Be Intimidating for New Coders

The spacing method that you use can often intimidate new coders because they want to know how many spaces are used for each indentation level. If you were to ask a group of new coders to add 3 spaces indentation to the following code they would get it wrong.

total = 10 * 6

assert total == 60 * 4

But, with this method, you know that there is a space before the assignment operator and 3 spaces after it. The new coder can also use an equals sign as long as there is a space after it as well.

Without using this method (the Python Standard Syntax) new coders don’t know when to start adding in extra spaces for indentation levels. Instead of the line being indented under the previous line, it’s indented under the next line. If a new line starts with “def” then you don’t know whether to add 2 or 3 spaces from the previous line to make sure that everything is aligned properly.

You can see how this would make new coders more confused than they already are if they have to worry about what’s right and wrong when using this method. This is why Python Standard Syntax is recommended by Google and PEP-8. It gives you an automatic way to automatically format your code with the proper amount of spaces between each level of indentation.

The reason that we recommend Black over PyIndent is because it gives you more control over the output of your code. The default method used by almost every other indentation tool in the world is to inject spaces after every line. If you want to use another method of indentation then research it and write an implementation for PyIndent or else derive from an existing format and use that instead.

Install and use black

The following commands will install black in your system and using black commang you can format python files easily.

pip install black

black filename.py

Conclusion

Google’s Python Style Guide recommends that you use 4 spaces to indent and no tabs. Many people choose not to follow this standard, but it’s recommended by Google and PEP-8 so you should use it. This blog post explains what Python’s standard syntax does and why this method is more efficient. Tabs are fine if used correctly, but most coders don’t know how to format their code with tabs. If you’re using tabs you need to put one tab between each new line. If you want to learn more about Python you can checkout best Python full stack training institute in Kerala.

Author: STEPS