Important Notice:

Indentation in Python

Indentation in Python

3 views 4 min read

Indentation in Python (इंडेंटेशन इन पायथन) :-

Indentation (इंडेंटेशन) का अर्थ है Program की किसी Line की शुरुआत में Space (स्पेस) या Tab (टैब) देना।

Python में Indentation केवल Program को सुंदर (Readable) बनाने के लिए नहीं होती, बल्कि यह Code Block (कोड ब्लॉक) को पहचानने के लिए उपयोग की जाती है। Python इसी Indentation की सहायता से यह निर्धारित करता है कि कौन-से Statements एक ही Block का भाग हैं।

यदि Program में Indentation सही नहीं दी जाती, तो Python IndentationError उत्पन्न करता है और Program Execute नहीं होता।

Python अन्य कई Programming Languages (जैसे C, C++, Java) की तरह { } (Curly Braces) का उपयोग नहीं करता। इसके स्थान पर Python Code Block को निर्धारित करने के लिए Indentation का उपयोग करता है।

English

Indentation means adding spaces or a tab at the beginning of a line of code.

In Python, indentation is not only used to improve readability but also to define a block of code. Python uses indentation to determine which statements belong to the same block.

If indentation is incorrect or missing, Python raises an IndentationError, and the program does not execute.

Unlike languages such as C, C++, and Java, Python does not use curly braces { } to define code blocks. Instead, it uses indentation.

Why is Indentation Important? (इंडेंटेशन क्यों आवश्यक है?)
  • Code Block को पहचानने के लिए / To define a code block.
  • Program की Structure (संरचना) को स्पष्ट करने के लिए / To show the program structure.
  • Program को पढ़ने और समझने में आसान बनाने के लिए / To improve code readability.
  • Syntax Error से बचने के लिए / To avoid syntax errors.
  • Python Program को सही प्रकार से Execute करने के लिए / To execute the program correctly.

 

Indentation Rules (इंडेंटेशन के नियम) :-

Python में Indentation (Space या Tab) का उपयोग Code Block (कोड ब्लॉक) को निर्धारित (Define) करने के लिए किया जाता है। अन्य Programming Languages की तरह Python में {} (Curly Braces) का उपयोग नहीं किया जाता। यदि Indentation सही नहीं दी जाती, तो Program में IndentationError आ सकती है।

English:

In Python, Indentation (spaces or tabs) is used to define a code block. Unlike many programming languages, Python does not use curly braces {}. If indentation is missing or incorrect, Python raises an IndentationError.

Rule 1: Indentation is Mandatory After Colon (:)

Colon (:) के बाद Indentation देना अनिवार्य है।

जिस Statement के अंत में Colon (:) आता है, उसके बाद अगली Line में Indentation देना आवश्यक होता है।

English:

If a statement ends with a colon (:), the next line must be indented.

Valid Example:

if True:
    print("Python")

Invalid Example:

if True:
print("Python") 

Rule 2: All Statements in the Same Block Must Have Equal Indentation

एक ही Code Block की सभी Lines में समान Indentation होनी चाहिए।

एक ही Code Block के सभी Statements में समान संख्या में Spaces या समान Indentation Level होना चाहिए।

English:

All statements in the same code block must have the same indentation level.

Valid Example:

if True:
   print("Python")
   print("Programming")

Invalid Example:

if True:
    print("Python")
       print("Programming")

Rule 3: Use Four Spaces for Indentation

Indentation के लिए सामान्यतः 4 Spaces का उपयोग करें।

Python में Indentation के लिए सामान्यतः 4 Spaces का उपयोग किया जाता है। यह Python की Recommended Coding Style (PEP 8) है।

English:

Use 4 spaces for indentation. It is the recommended coding style in Python (PEP 8).

Example:

if True:
    print("Hello")

Rule 4: Do Not Mix Tabs and Spaces 

Tab और Spaces को एक साथ उपयोग नहीं करना चाहिए।

एक ही Program या Code Block में Tab और Spaces को मिलाकर उपयोग नहीं करना चाहिए।

English:

Do not mix tabs and spaces in the same program or code block.

Valid Example:

if True:
  print("Python") #space

Invalid Example:

एक Line में Tab तथा दूसरी Line में Spaces का उपयोग करना।

Rule 5: Indentation is Required After Control Statements

Control Statements के बाद Indentation आवश्यक है।

if, elif, else, for, while, try, except, finally, with आदि Statements के बाद Indentation देना अनिवार्य है।

English:

Indentation is mandatory after control statements such as if, elif, else, for, while, try, except, finally, and with.

Valid Example:

for i in range(3):
print(i)

Invalid Example:

for i in range(3):
   print(i)

Rule 6: Indentation is Required in Functions

Function के अंदर Indentation आवश्यक है।

def के बाद Function के सभी Statements को समान Indentation के साथ लिखना आवश्यक है।

English:

All statements inside a function must be indented.

Valid Example:

def show():
   print("Hello")

Invalid Example:

def show():

print("Hello")

Rule 7: Indentation is Required in Classes Class के अंदर Indentation आवश्यक है।

 

class के अंदर लिखे गए सभी Statements को Indentation के साथ लिखना चाहिए।

English:

All statements inside a class must be indented.

Valid Example:

class Student:
    pass

Invalid Example:

class Student:
pass

Rule 8: Nested Blocks Require Additional Indentation Nested Block में अतिरिक्त Indentation आवश्यक है। यदि किसी Block के अंदर दूसरा Block हो, तो दूसरे Block में पहले Block से अधिक Indentation दी जाती है।English:Nested blocks require one additional level of indentation.

 

Valid Example: if True:
    print("Level 1")
    if True:
        print("Level 2")
Invalid Example: if True:
    print("Level 1")
    if True:
    print("Level 2")
Rule 9: Incorrect Indentation Produces IndentationError गलत Indentation होने पर IndentationError आती है। यदि Indentation गलत हो या आवश्यक स्थान पर न दी जाए, तो Python Program Execute नहीं होगा और IndentationError प्रदर्शित होगी।English:If indentation is incorrect or missing, Python raises an IndentationError.Valid Example: if True:
    print("Python")
Invalid Example: if True:
print("Python")
Error: IndentationError: expected an indented block Rule 10: Blank Lines Do Not Need Indentation Blank Line में Indentation आवश्यक नहीं होती। Blank Line में Space या Tab देना आवश्यक नहीं होता।English:Blank lines do not require indentation.
Example: print("Python")
print("Programming")
PEP 8 :-

PEP 8 (Python Enhancement Proposal 8) Python की आधिकारिक Coding Style Guide है। इसमें Variable Name, Function Name, Indentation, Line Length, Spaces आदि लिखने के मानक (Standards) दिए गए हैं।उदाहरण के लिए, PEP 8 के अनुसार Python में Indentation के लिए सामान्यतः 4 Spaces का उपयोग करना चाहिए।EnglishPEP 8 (Python Enhancement Proposal 8) is the official style guide for writing Python code. It provides standards for naming variables, functions, indentation, line length, spacing, and other coding conventions.For example, PEP 8 recommends using 4 spaces for indentation.

Related Notes