← Back to Python Course | Chapter 1: Basics | Lesson 6 of 14

Python Comments

Comments आपके code में लोगों के पढ़ने के लिए छोड़े गए छोटे नोट्स हैं। Computer इन्हें अनदेखा कर देता है, ठीक वैसे ही जैसे किसी recipe पर लगी sticky note जो बताती है कि आपने अतिरिक्त चीनी क्यों डाली।
Syntax
python
# single-line comment

x = value  # inline comment

"""
multi-line comment
(docstring-style)
"""

Single-Line Comments

# एक single-line comment की शुरुआत दर्शाता है; interpreter उस character से line के अंत तक सब कुछ अनदेखा कर देता है। code के चलने पर असर डाले बिना अपने लिए छोटा नोट छोड़ने का यह सबसे तेज़ तरीका है।

उदाहरण: Single-Line Comments

python
# This is a comment
print("Comments are ignored by the interpreter")

Multi-Line Comments

Python में कोई समर्पित multi-line comment syntax नहीं है — developers या तो कई # lines को एक साथ रखते हैं, या बिना असाइन की गई triple-quoted string ("""...""") को block comment की तरह इस्तेमाल करते हैं, क्योंकि अकेला string statement चलने पर कोई असर नहीं डालता।

उदाहरण: Multi-Line Comments

python
# This is line one
# This is line two

"""
This triple-quoted string
acts as a block comment
"""
print("done")

Code का Documentation

अच्छे comments यह समझाते हैं कि कोई code *क्यों* मौजूद है — कोई पेचीदा workaround, कोई business rule, कोई अस्पष्ट edge case — बजाय इसके कि वही दोहराएँ जो code पहले से साफ़ दिखा रहा है कि वह *क्या* करता है।

यही फ़र्क comments को अगले पढ़ने वाले के लिए, यानी भविष्य के आपके लिए भी, सचमुच उपयोगी बनाता है।

उदाहरण: Documenting Code

python
# Using a set here because duplicate ids would break the billing report
unique_ids = set()
print(unique_ids)

Code को Comment Out करना

किसी line या block को # में लपेट देने से वह बिना मिटाए चलना बंद हो जाता है, जो debugging के दौरान यह अलग करने का आम तरीका है कि script का कौन सा हिस्सा समस्या पैदा कर रहा है।

ज़्यादातर editors पूरे चयन को एक साथ comment/uncomment करने के लिए keyboard shortcut देते हैं।

उदाहरण: Commenting Out Code

python
print("this runs")
# print("this is commented out and does not run")

Comments के सर्वोत्तम तरीके

जो comments पुराने पड़ जाते हैं वे बिना comment के होने से भी बदतर हैं, क्योंकि वे अगले पाठक को code के बारे में सक्रिय रूप से गुमराह करते हैं। उन्हें छोटा रखिए, जब भी वर्णित code बदले तो उन्हें अपडेट कीजिए, और जो अब कोई जानकारी नहीं जोड़ते उन्हें हटा दीजिए।

उदाहरण: Comment Best Practices

python
# Retry limit tuned after the 2024 outage postmortem
MAX_RETRIES = 5
print(MAX_RETRIES)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.