Technical Roadmap

Indentation in Python

Every programming language has a set of rules called Syntax. If you don't follow these rules, the computer won't understand your logic. Python's syntax is famous for being clean, readable, and very close to the English language.

The Power of Indentation

In languages like C, C++ or Java, we use curly brackets { } to define a block of code.
In Python, we don't use curly brackets; instead, we use Indentation. Indentation means empty spaces at the beginning of a line to define or group a block of code.

Usually, we use one "Tab" or four spaces for indenting a block.
If you don't indent your code correctly, Python will throw an IndentationError.

Python
# In Python, we use a colon (:) and spaces
if True:
    print("This line is indented")
    print("It belongs to the 'if' block")

print("This line is not indented")
print("It is outside the block")
Output
This line is indented
It belongs to the 'if' block
This line is not indented
It is outside the block
💡 Logicoria Tip

Indentation isn't just for making the code look "pretty" - It tells Python exactly which lines of code belong together. Don't mix tab or spaces in your program as it can cause a Tab Error.

Interview / Exam Tip

  • Python uses Indentation (empty spaces) to group a block of code together. If the spaces are wrong, the logic is wrong.
  • Unlike Java or C++, we do not use curly brackets { } to start or end a block of code.
  • There is no need to use a semicolon ; at the end of a line. Python knows the line is finished when you hit the 'Enter' key.