What is Indentation in Python in Hindi- पायथन में इंडेंटेशन क्या है?
- Python में Indentation का मतलब एक code line की शुरुआत में spaces या tabs जोड़ना ।
- अन्य भाषाओं (languages) में Indentation का उपयोग केवल कोड को साफ और पढ़ने योग्य बनाने के लिए किया जाता है, परंतु
- Python, में indentation की जरूरत होती है।
- Python में इसका उपयोग loops, functions, if-else statements, और अन्य code structures में किया जाता है।
- Python में indentation यह दर्शाता है कि कौन सी code lines एक ही block का हिस्सा हैं।
- यदि indentation सही नहीं है तो python एक error प्रदर्शित करेगा है और प्रोग्राम Run नही होगा।
Importance of Indentation in Python in Hindi - पाइथन में इंडेंटेशन का महत्व
Python में Indentation केवल code को अच्छा दिखाने के लिए नहीं है परंतु यह python syntax का आवश्यक भाग है। पायथन indentation का उपयोग यह दिखाने के लिए करता है कि code की कौन सी lines ब्लॉक से संबंधित हैं
जैसे`if`, `for`, `while`, या एक function के अंदर
जैसे`if`, `for`, `while`, या एक function के अंदर
यदि indentation गायब है या ग़लत है, तब Python, एक error देगा और प्रोग्राम Run नहीं होगा।
यह पायथन को यह जानने में मदद करता है कि कौन से code को एक साथ Run करना है। यह code को साफ, पढ़ने में आसान और अच्छे से व्यवस्थित बनाए रखने में भी मदद करता है।
Rules of Indentation in Python in Hindi - पाइथन में इंडेंटेशन के नियम
Python में Indentation के मुख्य नियम निम्न है।1) Indentation is required(इंडेंटेशन की जरुरत):
Python, को code के lines को एक समूह में रखने के लिए indentation की जरूरत होती है।
यदि आप indent करना भूल जाते हैं या गलत तरीके से indent करते हैं तब पाइथन एक एरर दिखाएगा और प्रोग्राम नहीं चलेगा।
2) First line should not be indented(पहले लाइन को इंडेंट नहीं किया जाना चाहिए):
आपके code का पहला लाइन बाई तरफ से शुरू होना चाहिए। यदि आप पहले लाइन की शुरुआत में space डालते है तब पाइथन error देगा।
3) How much to indent(कितना इंडेंट करें):
आपको indentation के पहले level को दिखाने के लिए line की शुरुआत में 4 spaces जोड़ना चाहिए।
आप tab का भी उपयोग कर सकते हैं परंतु spaces का उपयोग करना बेहतर है।
4) Be consistent(एक जैसा हो):
हमेशा एक block में समान संख्या में spaces का उपयोग करें। एक file में spaces और tabs को न मिलाए। इससे त्रुटियाँ या भ्रम उत्पन्न हो सकता है।
5) Indent after colon(colon के साथ Indent):
यदि एक line, colon के साथ समाप्त होता है जैसे if, for, while, def, या class, मे तब अगले लाइन को अवश्य ही indent होना चाहिए।
6) Indent ends the block(Indent, ब्लॉक को समाप्त करता है):
जब आप Indent देना बंद कर देते हैं Python, यह जान लेता है कि ब्लॉक समाप्त हो गया है।
अगला line, पिछला ब्लॉक का भाग नही है।
if, else, loop और function में इंडेंटेशन का उपयोग
if, else, for, while, और def (functions) जैसे ब्लॉक में indentation की आवश्यकता होती है।
if-else में Indentation
num = 7if num >= 5:
print("Greater than 5")
else:
print("Less than 5")
if और else के नीचे की लाइनें indent की गई है जो यह दिखाता है कि ये ब्लॉक से संबंधित है।
for loop में Indentation
for i in range(5):print(i)
print(i) line, indent की गई है जो यह दिखाता है कि यह for loop के अंदर है।
while loop में Indentation
count = 1while count <= 5:
print("Count is", count)
count += 1
दोनों print और count += 1 while block के अंदर indent की गई है ।
functions में Indentation
def greet():print("Hello")
print("Welcome")
दो print() lines, greet() function का भाग है इसलिए उन्हें इंडेंट किया गया है।
Python में कब Indentation Error उत्पन्न होता है।
Indentation Error के कुछ सामान्य कारण निम्न है।1) जब आप colon के बाद indent देना भूल जाते हैं।
if 5 > 3:
print("Yes") # Error – no indentation
2) जब आप spaces और tabs को मिला देते हैं।
if True:
print("Hello") # using spaces
print("HI") # using tab (Error)
3) जब आप अतरिक्त या कम spaces देते हैं।
if 7> 5:
print("Correct")
print("Wrong") # Error – extra space
4) जब गलती से पहले लाइन को indent किया जाता है।
print("Hello") # Error – first line should not be indented
सही इंडेंटेशन और गलत इंडेंटेशन का उदाहरण
Example of Correct Indentation
चलिए सरल if-else statement मेंIndentation का प्रयोग देखते हैं।
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You can vote")
Explanation:
if और else code के indent का उपयोग किया गया है (आमतौर पर 4 spaces देकर)
Example of wrong indentation (यह एरर देगा):
age = 18if age >= 18:
print("You are an adu
lt.") # No indentation here!
else:
print("You can vote.") # No indentation here!
Python एक IndentationError! देगा
0 टिप्पणियाँ