Indentation


Python indentation

Python uses blocks for indentation rather than using curly braces.
Both tabs and spaces can be given for indentation, but the standard rule for the python indentation is four spaces.

Indentation in a programming language refers to the space left before starting a line of code. In other programming languages, indentation is mainly used for readability, but it plays a significant role in python. Python utilizes indentation to indicate a block of code.

Here are some more things to know about python indentation:

  • Python gives an error if you skip using an indentation wherever necessary.
  • Even though the best practice for indentation is to use four spaces, you can set your choices. However, a minimum of 1 space must be there.
  • In a given block of code, it is essential to use the same number of spaces; else, python will give an error.
  • A block of code starts with indentation and ends as soon as there comes the first unindented line.

In python, enforcing indentation makes the code cleaner and neater, and it also makes the entire language much consistent and similar. In line continuation, it can be ignored, but it is a better practice to follow to enhance the code's readability.

EXAMPLE CODE

x=1 if x==1: #indentation is given print(" indentation is given and x is 1")

OUTPUT

Image Not Found