Interview question
How does Python's indentation-based syntax work, and why does it matter? Python का indentation-based syntax कैसे काम करता है, और यह क्यों important है?
Answer
Python uses indentation (whitespace) instead of braces {} to define code blocks. Consistent indentation is mandatory - inconsistent indentation raises an IndentationError.
def check_age(age):
if age >= 18:
print('Adult')
else:
print('Minor')
# Inconsistent indentation causes an error
def broken():
if True:
print('A')
print('B') # IndentationError - mismatched spaces| Rule | Detail |
|---|---|
| Standard | 4 spaces per indentation level (PEP 8) |
| Mixing | Never mix tabs and spaces in the same file |
| Block scope | Indentation defines if/for/while/def/class bodies |
Python braces {} की जगह indentation (whitespace) use करके code blocks define करता है। Consistent indentation ज़रूरी है - inconsistent होने पर IndentationError आता है।
def check_age(age):
if age >= 18:
print('Adult')
else:
print('Minor')
def broken():
if True:
print('A')
print('B') # IndentationError| Rule | विवरण |
|---|---|
| Standard | हर level पर 4 spaces (PEP 8) |
| Mixing | Tabs और spaces कभी mix न करें |
Was this answer clear?