Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the recommended size of indentation in Python?

Is there any official rule/proposal on how should the Python code be indented?

like image 533
syntagma Avatar asked Mar 25 '13 19:03

syntagma


People also ask

How big is an indent in Python?

Note: Python uses 4 spaces as indentation by default. However, the number of spaces is up to you, but a minimum of 1 space has to be used.

Why does Python use 4 spaces?

It is easier to visually identify long nested code blocks with 4 spaces. Saves time when debugging. Agreed. For a language like C I'd rather use two, actually, but in C the visual clue provided by indentation is less important than it is in Python.

Does Python only accept 4 spaces indentation?

Python doesn't mind whether you use two spaces or four spaces (or any other number of spaces) as long as you are consistent. For example if you start off using four spaces for an indent, then you should always use four spaces.

What is indent size?

The indent size - is how many whitespaces will be put in the indented line start. Sometimes it is done with tabs, sometimes with spaces, depending on configuration.


2 Answers

From PEP 8 (Python's official style guide):

Use 4 spaces per indentation level.

like image 166
Andrew Clark Avatar answered Sep 23 '22 07:09

Andrew Clark


4 spaces, or hitting the tab button once. However, try to avoid the tab button. Sometimes it will give you indentation errors even when you indent correctly.

like image 41
Neelasha Bhattacharjee Avatar answered Sep 23 '22 07:09

Neelasha Bhattacharjee