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.
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")
if True:
print("Python")
एक ही 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")
if True:
print("Python")
print("Programming")
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")
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
एक Line में Tab तथा दूसरी Line में Spaces का उपयोग करना।
Rule 5: Indentation is Required After Control StatementsControl 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)
for i in range(3):
print(i)
Function के अंदर Indentation आवश्यक है।
def के बाद Function के सभी Statements को समान Indentation के साथ लिखना आवश्यक है।
English:All statements inside a function must be indented.
Valid Example:def show():
print("Hello")
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
class Student:
pass
Valid Example: if True: