Junior — Middle
Why is it important to use indentation when writing code, and what role does it play in its structure?
sobes.tech AI
Answer from AI
In Python, indentation plays a key role in the structure of the code, as it defines code blocks (for example, the body of functions, loops, conditional statements). Unlike many languages where blocks are enclosed in curly braces, in Python, indentation is syntactically significant.
Proper use of indentation:
- Ensures code readability.
- Defines nesting and the belonging of statements to blocks.
- Without correct indentation, code will raise an
IndentationError.
Example:
if True:
print("This is inside the if block")
print("This is outside the if block")
Here, indentation indicates that the first print statement belongs to the condition, while the second does not.