If you're the only coder working on your source file and there are no coding standards that enforce a particular style, use whatever you're comfortable with. Personally (and in line with our coding standard), I use hard tabs so that whoever is looking at the code can use their own preference. Show
To make a change, you simply need to change all start-of-line spaces to ones that are twice as large. There are many ways to do this; in the Vim text editor, I can think of two: firstly:
This is a simple regular expression that looks for one or more pairs of spaces at the start of the line and replaces them with twice as many spaces as were found. It can be extended to do all files by opening vim with:
(or the equivalent), followed by (untested):
Alternatively:
Of course, many other tools will offer similar features: I would be surprised if something like I'm currently reading the Python introduction by google, and I'm intrigued as to why the standard spacing is recommended to be 4 but google uses 2:
Why would their internal style guideline recommend a standard of two spaces instead of the normal 4? Is it related to how using * and + are "overload" operators as the introduction says? Would a py script that uses 2 spaces instead of 4 run quicker, or am I on the wrong hunch?
https://developers.google.com/edu/python/introduction Learn about Indentation in Python. Indentation in Python Programming is simply the spaces at the beginning of a code line. Indentation in other languages like c, c++, etc., is just for readability, but in Python, the indentation is an essential and mandatory concept that should be followed when writing a python code; otherwise, the python interpreter throws IndentationError. To understand this topic, you should have some
knowledge of the following Python Programming topics: Indentation is the leading whitespace ( spaces and tabs )
before any statement in Python. The reason why indentation is important in python is that the indentation serves another purpose other than code readability. Python treats the statements with the same indentation level (statements with an equal number of whitespaces before them) as a single code block. So whereas in languages like c, c++, etc. a block of code is represented by Curly braces { }, in python a block is a group of statements that have the same Indentation level i.e same number of
leading whitespaces.
ExamplesExample 1: Below is an example code snippet with the correct indentation in python.
Output:
Explanation:
Example 2: Below is an example code snippet with correct indentation.
Output:
Explanation:
How to indent your python codeLet us walk through how to indent a python code by taking a simple example. Example: Check if a given number is even or odd and if it’s zero print neither even nor odd Let us discuss the approach step-wise:
Program:
Output: Explanation:
How to avoid python indentation errors
Wrong Indentation(Error):
With Correct Indentation:
Wrong Indentation(Error):
With Correct Indentation:
Wrong Indentation(Error):
With Correct Indentation:
Wrong Indentation(Error):
The above program will throw the “IndentationError: unindent does not match any outer indentation level” error because the last print statement, which is indented, does not match with any other indentation(no attaching outer statement). In the program, hence this will throw an error. Correct Indentation:
Here the last print statement’s indentation matches with the indentation of the print statement below if block. Hence here, the outer attaching statement is the if statement. Python Indentation Rules
Benefits of Indentation in Python
Disadvantages of indentation in Python
ConclusionWe have reached the end of the article. Python indentation is something that is a foundation concept for any new python programming, understanding how indentation works are the first step before you can start writing code in python. How many spaces do I 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.
Should a tab be 2 or 4 spaces?If you represent the indents using space characters (ASCII SP), then 2 spaces is 2 characters fewer than 4 spaces. If you allow TAB characters to be used, then (on Windows) a TAB indents by up to 4 spaces, so TAB characters result in fewer characters.
What does indent 4 mean in Python?Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.
How many spaces should be left for indentation?Standard paragraph indentation is about five spaces or one-quarter to one-half of an inch, depending on which style guide you follow. In online writing, if your software doesn't allow indentation, insert a line space to indicate a new paragraph.
Does Python prefer tabs or spaces?Tabs or Spaces? Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs. Python disallows mixing tabs and spaces for indentation.
Why does the code under the if need to be indented four spaces in Python?The two blocks of code in our example if-statement are both indented four spaces, which is a typical amount of indentation for Python. In most other programming languages, indentation is used only to help make the code look pretty. But in Python, it is required for indicating what block of code a statement belongs to.
|