Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 3 of 10 · Python Basics & Syntax
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
RuleDetail
Standard4 spaces per indentation level (PEP 8)
MixingNever mix tabs and spaces in the same file
Block scopeIndentation 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)
MixingTabs और spaces कभी mix न करें

Was this answer clear?