Learn with Yasir

Share Your Feedback

Python Indentation


Learn Python Indentation

In Python, indentation refers to the use of whitespace (spaces or tabs) to denote block-level structure in the code. Python uses indentation to define the scope of code blocks, such as:

  • Function definitions
  • Loops (for, while)
  • Conditional statements (if, elif, else)
  • Exception handling (try, except)

In Python, indentation is mandatory and is used to determine the grouping of statements. The number of spaces used for indentation is not fixed, but it’s standard to use 4 spaces for each level of indentation. Read more: Indentation - PEP 8 – Style Guide for Python Code

Here’s an example:

if True:
    print("Hello")  # This line is indented with 4 spaces
    print("World")  # This line is also indented with 4 spaces

In this example, the two print statements are indented with 4 spaces, indicating that they are part of the if block.

Python’s indentation system has several benefits, including:

  • Improved readability: Indentation makes the code structure clear and easy to read.
  • Reduced errors: Indentation helps avoid errors caused by mismatched braces or parentheses.
  • Simplified syntax: Python’s indentation system eliminates the need for explicit block delimiters like braces or keywords.

Another example, consider the following code snippet:

if True:
    print("True")
else:
    print("False")

Task: 15 Please correct the following Python code:

if True:
  print("True")
    print("False")

Error message: IndentationError: unexpected indent

See also: