Skip to content Skip to sidebar Skip to footer

Why Is Python Strict About Indentation?

This question is coming from this Stack Overflow question. Why is Python is designed to use correct indentation either space or tab? In this way, is it purposefully to serve any be

Solution 1:

Indentation is essential in Python; it replaces curly braces, semi-colons, etc. in C-like syntax.

Consider this code snippet, for example:

deff(x):
    if x == 0:
        print("x is zero")
        print("where am i?")

If it weren't for indentation, we would have no way of indicating what code block the second print statement belongs to. Is it under the if statement, the function f or just by itself in the document?

Solution 2:

There is no set width of a tab character, so Python has no way of knowing how many spaces to count a tab as.

Post a Comment for "Why Is Python Strict About Indentation?"