Python uses indentation to define code blocks. This is a key feature and differs greatly from many programming languages that use curly braces or other symbols.
Indentation increases readability and forces developers to write clean, organized code. Key points to note about code blocks in Python:
- Indentation level typically uses 4 spaces or a tab.
- Indentation consistency is crucial. Mixing spaces and tabs can lead to errors.
- Used in loops (
for
, while
), conditionals (if
, elif
, else
), and function definitions.
For example, an
if
statement followed by an indented block:
if x > 5:
print('x is greater than 5')
Python's strict indentation rules help maintain proper code structure and readability, making it easier to understand and debug.