Comments in Python in Hindi

इस tutorial में, हम Python Comments के बारे में चर्चा करेंगे और सीखेंगे कि Python प्रोग्रामिंग में Single Line Comments और Multi Line Comments का उपयोग कैसे किया जाता है।

Programming करते समय, यह बेहद महत्वपूर्ण होता है कि हमारा कोड साफ़ (clean) और आसानी से समझने योग्य (easily understandable) हो।

इसके लिए हम दो मुख्य कार्य करते हैं:

  1. Programs में variables और functions को meaningful नाम देना।
  2. Programs में Comments का उपयोग करना।

अब आइए समझते हैं कि Python प्रोग्राम्स में Comments का उपयोग क्यों किया जाता है और यह programmers के लिए कैसे फायदेमंद होता है।


Python Programs में Comments का Use क्यों जरूरी है?

जब हम Python programming से real life applications बनाते है, तो उनमें कई अलग-अलग टास्क होते हैं। 

हम इन tasks के लिए code को सोच-समझकर लिखते हैं, लेकिन कई बार ऐसा होता है कि code का logic बहुत जटिल (complex) हो जाता है।

अब, अगर कुछ समय बाद हमें अपने ही कोड में बदलाव (changes) करने हों, तो उसे समझने में बहुत समय लग सकता है, क्योंकि हम खुद भूल सकते हैं कि इसमें कौन-सा लॉजिक इस्तेमाल किया था।

इसके अलावा, अगर कोई दूसरा programmer हमारे program code को देखे या उसमें कुछ changes करे, तो उसके लिए तो हमारे code के logic को समझना बहुत ज्यादा मुश्किल हो सकता है।

इसी समस्या से बचने के लिए, हम अपने Python प्रोग्राम्स में Comments का उपयोग करते हैं, ताकि कोड को आसानी से समझा और मैनेज किया जा सके।


Python Comments क्या है?

Python programs के source code के बीच-बीच में जब हम simple english में description text notes या human-readable explanation लिखते हैं, तो उन्हें comments कहा जाता है।

Comments की वजह से programmers को ये याद रहता है या समझने में आसानी होती है की किसी program code का logic क्या था या यह code काम कैसे करता है।

जब हम अपने Python programs को interpret (compile) करते हैं, तो Python interpreter उस programs में मौजूद comments को पूरी तरह से ignore कर देता है।

इसका मतलब है कि Comments non-executable statements होते हैं, और इनसे programs के output में कोई फर्क नहीं पड़ता है।

Python प्रोग्रामिंग में आप Comments को दो प्रकार से उपयोग कर सकते हैं:

  1. Single-Line Comments
  2. Multi-Line Comments

अब आइए, इन दोनों प्रकार के Comments का उपयोग करना सीखें।


Single Line Comment in Python

Python में Single-Line Comment की शुरुआत हैश (#) सिंबल से होती है। यानी, hash symbol के बाद जो भी statement लिखा जाएगा, उसे Python interpreter comment मानेगा और उसे एक्सीक्यूट (execute) नहीं करेगा।

Python Single Line Comment Syntax:

# Anything written here is ignored by Python

हैश (#) सिंबल के बाद स्पेस देना अनिवार्य नहीं है, लेकिन स्पेस देने से कमेंट की रीडेबिलिटी (readability) बेहतर हो जाती है।

आप अपने Python प्रोग्राम्स में कहीं भी Comments का उपयोग कर सकते हैं, जैसा कि नीचे दिए गए example programs में दिखाया गया है।

Python Single Line Comment Example 1:

#First Python Program
print("Hindi Me Tutorials")

Output:

Hindi Me Tutorials

Python Single Line Comment Example 2:

#variable initialization
a=5
b=7

c=a+b  #Addition

#Print Total
print(c)

Output

12

जब आप किसी कोड लाइन के अंत में ही कमेंट लिखते हैं, तो उसे Inline Comment कहा जाता है। जैसा कि आप ऊपर दिए गए प्रोग्राम में लाइन 5 पर #Addition देख सकते हैं।

हालांकि, ज्यादातर programmers inline comments पसंद नहीं करते, क्योंकि ये कोड को कम पढ़ने योग्य (less readable) बना सकते हैं।

इसलिए, मैं आपको सुझाव दूंगा कि कमेंट्स को हमेशा कोड से पहले (ऊपर) लिखें, ताकि कोड साफ़ और समझने में आसान रहे।


Multiline Comment in Python

अधिकांश programming languages (जैसे C, Java आदि) में Single-Line Comments और Multi-Line Comments के लिए अलग-अलग syntax होते हैं।

लेकिन Python में ऐसा नहीं है, यानी Python में Multi-Line Comments के लिए कोई अलग syntax नहीं दिया गया है।

इसका मतलब यह नहीं है कि Python में Multi-Line Comments नहीं बनाए जा सकते। ऐसा करने के दो तरीके हैं।

पहला तरीका ये की आप उन सभी lines के आगे hash (#) symbol लगा दें जिन्हें आप commented करना चाहते हो।

Python Multiline Comment Example 1:

# Comment first line
# Comment second line
# Comment third line
print("Simple Addition")
a=10
b=20
c=a+b
print(c)

Output:

Simple Addition
30

Python में Multi-Line Comments का उपयोग करने का दूसरा तरीका (जुगाड़) यह है कि उन सभी लाइनों को, जिन्हें आप कमेंट करना चाहते हैं, तीन Single Quotes (”’) या तीन Double Quotes (“””) के बीच लिख दें।

Python Multiline Comment Example 2:

"""
Comment first line
Comment second line
Comment third line
"""
print("Simple Addition")
a=10
b=20
c=a+b
print(c)

Output:

Simple Addition
30

जब आप किसी भी मल्टीपल लाइन्स को तीन Single Quotes (”’) या तीन Double Quotes (“””) के बीच लिखते हैं, तो Python इसे एक string मानता है।

लेकिन, क्योंकि हमने इस string को किसी variable में assign नहीं किया, इसलिए Python इसे ignore कर देता है और यह प्रोग्राम के आउटपुट में दिखाई नहीं देता।


What’s Next: इस tutorial में हमने Python में Comments का उपयोग करना सीखा। Next Tutorial में हम Python Variables के बारे में चर्चा करेंगे।