How do you manage indentations in python?
Learn about Indentation in Python. Show
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 do you maintain indentation in Python?To indicate a block of code in Python, you must indent each line of the block by the same whitespace. The two lines of code in the while loop are both indented four spaces. It is required for indicating what block of code a statement belongs to.
How do I reduce multiple indents in Python?SHIFT + TAB will do unindent for the selected text in Eclipse. Show activity on this post. Use Preferences-Pydev-Editor settings uncheck change tabs to spaces.
|