Comments

Comments అంటే ఏమిటి?

Python code లో మనకు అర్థం కావడానికి రాసే వివరణలను Comments అంటారు. Python interpreter comments ను execute చేయదు. అందువల్ల comments program output పై ఎలాంటి ప్రభావం చూపవు.

Comments ఎందుకు ఉపయోగిస్తాము?

  • Code ఏమి చేస్తుందో గుర్తుంచుకోవడానికి
  • ఇతరులు code ను సులభంగా అర్థం చేసుకోవడానికి
  • Code లోని ముఖ్యమైన భాగాలను వివరించడానికి
  • తాత్కాలికంగా ఒక code line ను execute కాకుండా చేయడానికి

Python లో Comment ఎలా రాయాలి?

Python లో single-line comment కోసం line ప్రారంభంలో # symbol ఉపయోగిస్తాము. # తర్వాత ఉన్న text ను Python execute చేయదు.

Comment ఉపయోగించే ప్రదేశాలు

Comment ను ఒక ప్రత్యేక line గా లేదా code line చివరలో కూడా రాయవచ్చు. కానీ comment స్పష్టంగా, చిన్నగా, ఉపయోగకరంగా ఉండాలి.

ముఖ్యమైన గమనిక

Python లో ప్రత్యేకమైన multi-line comment syntax లేదు. అనేక lines కు comment రాయాలంటే ప్రతి line ప్రారంభంలో # వాడాలి. Triple quotes తో రాసిన text నిజమైన comment కాదు; అది string. ఈ lesson లో comments కోసం # ను ఉపయోగించడం సరైన పద్ధతి.

Simple Explanation

ఒక పుస్తకంలో ముఖ్యమైన విషయాలను గుర్తుంచుకోవడానికి పక్కన చిన్న note రాసినట్లే, Python code లో వివరణ కోసం comments రాస్తాము.

ఉదాహరణకు # This line displays a message అని రాస్తే, Python ఆ line ను పట్టించుకోదు. తర్వాతి line లో ఉన్న print() మాత్రమే execute అవుతుంది.

Comment రాయడానికి # symbol ను ఉపయోగించండి. Python interpreter # తర్వాతి text ను ignore చేస్తుంది.

Real-Life Example

ఒక recipe లో ముందుగా నీటిని మరిగించాలి అని చిన్న note రాసి ఉంటే, అది recipe ని execute చేసే machine కోసం కాదు; recipe చదివే వ్యక్తికి సహాయం చేయడానికి. అదే విధంగా Python comments కూడా program చదివే మనుషులకు సహాయం చేస్తాయి, Python interpreter కోసం కాదు.

Code Examples

Single-line Comment

# This program displays a welcome message
print('Welcome to Python')

మొదటి line ఒక comment. అది Python చేత execute చేయబడదు. రెండవ line లోని print() మాత్రమే execute అవుతుంది మరియు welcome message ను చూపిస్తుంది.

Welcome to Python

Inline Comment

print('Python is easy')  # Display a message
# The next line displays another message
print('Keep practicing')

మొదటి line లో print() code తర్వాత inline comment ఉంది. Python code భాగాన్ని execute చేసి, # తర్వాతి text ను ignore చేస్తుంది. రెండవ line పూర్తిగా comment కాబట్టి execute కాదు. మూడవ line మళ్లీ message ను display చేస్తుంది.

Python is easy
Keep practicing

Line-by-Line Explanation

# This program displays a welcome message
ఈ line ఒక comment. ఇది program ఏమి చేస్తుందో మనకు చెబుతుంది. Python interpreter ఈ line ను execute చేయదు.
print(‘Welcome to Python’)
ఈ line print() function ను ఉపయోగిస్తుంది. Python దీన్ని execute చేసి Welcome to Python అనే text ను screen పై చూపిస్తుంది.
print(‘Python is easy’) # Display a message
ఈ line లో print() message ను display చేస్తుంది. # తర్వాత ఉన్న Display a message భాగం inline comment, కాబట్టి Python దాన్ని ignore చేస్తుంది.
# The next line displays another message
ఈ line పూర్తిగా comment. ఇది తరువాతి line ఏమి చేస్తుందో వివరిస్తుంది. Python ఈ line ను execute చేయదు.
print(‘Keep practicing’)
ఈ line print() function ద్వారా Keep practicing అనే text ను screen పై చూపిస్తుంది.

Try It

Try this Python code:

Example

# Add a comment explaining the next line
print('I am learning Python')

# Add another comment here
print('Comments make code clear')

Try It Yourself »

Practice

Practice: పై code లో ప్రతి print() line కు ముందు ఆ line ఏమి చేస్తుందో వివరించే ఒక comment రాయండి. Code యొక్క output మార్చకూడదు.

Hint: ప్రతి comment ను # symbol తో ప్రారంభించండి. ఉదాహరణ: # Display a message

Practice: ఒక print() statement ను inline comment తో రాయండి. Inline comment లో ఆ statement యొక్క పని వివరించండి.

Hint: print(‘Your message’) # Explain the line

Common Mistakes

Comment ముందు # symbol పెట్టకపోవడం.
Comment line ప్రారంభంలో # పెట్టండి. ఉదాహరణ: # This is a comment
Comment ను code లాగా execute అవుతుందని అనుకోవడం.
Comments Python interpreter చేత execute కావని గుర్తుంచుకోండి. అవి మనుషులకు వివరణ కోసం మాత్రమే.
Comment ను string తో కలిపి రాయడం లేదా quotes తప్పుగా ఉపయోగించడం.
సాధారణ comment కోసం # ఉపయోగించండి. ఉదాహరణ: # Display the name
Inline comment లో # ముందు code ను తప్పుగా రాయడం.
మొదట valid Python code రాసి, ఆ తర్వాత ఖాళీ స్థలం ఇచ్చి # మరియు comment రాయండి.
ప్రతి line కి చాలా పొడవైన లేదా అర్థం లేని comments రాయడం.
Code యొక్క ఉద్దేశాన్ని స్పష్టంగా చెప్పే చిన్న comments రాయండి.

Interview Questions

Python లో comment అంటే ఏమిటి?

Answer: Program ను చదివే వారికి వివరణ ఇవ్వడానికి రాసే text ను comment అంటారు. Python interpreter comments ను execute చేయదు.

Python లో single-line comment ఎలా రాస్తారు?

Answer: Line ప్రారంభంలో # symbol ఉపయోగిస్తారు. ఉదాహరణ: # This is a comment

Comment ను ఎందుకు ఉపయోగిస్తారు?

Answer: Code ను అర్థం చేసుకోవడానికి, code గురించి ఇతరులకు వివరించడానికి, ముఖ్యమైన విషయాలను గుర్తుంచుకోవడానికి comments ఉపయోగిస్తారు.

Inline comment అంటే ఏమిటి?

Answer: Code line చివరలో రాసే comment ను inline comment అంటారు. ఉదాహరణ: print(‘Hello’) # Display greeting

Python లో # తర్వాత ఉన్న text కు ఏమవుతుంది?

Answer: Python interpreter # తర్వాత ఉన్న text ను ignore చేస్తుంది; దాన్ని execute చేయదు.

Exam Questions

క్రింది వాటిలో సరైన Python comment ఏది? A) // comment B) # comment C) /* comment */

Answer:

సరైన సమాధానం B) # comment

Comments program output పై ప్రభావం చూపుతాయా?

Answer:

లేదు. Comments execute కావు కాబట్టి program output పై ప్రభావం చూపవు.

క్రింది code output ఏమిటి?
# print(‘A’)
print(‘B’)

Answer:

Output B. మొదటి line comment కాబట్టి execute కాదు.

Inline comment కు ఒక ఉదాహరణ రాయండి.

Answer:

print(‘Hello’) # Display a greeting

Python లో multi-line comment రాయడానికి సాధారణ పద్ధతి ఏమిటి?

Answer:

ప్రతి line ప్రారంభంలో # symbol రాయాలి. Python లో ప్రత్యేకమైన multi-line comment syntax లేదు.

Quick Revision

  • Comment అనేది code గురించి మనుషులకు వివరణ ఇచ్చే text.
  • Python లో comment రాయడానికి # symbol ఉపయోగిస్తారు.
  • Python interpreter comments ను execute చేయదు.
  • Comment ను ప్రత్యేక line గా లేదా code line చివరలో inline comment గా రాయవచ్చు.
  • చాలా lines కు comment రాయాలంటే ప్రతి line ముందు # ఉపయోగించాలి.
  • Comments code ను చదవడం మరియు అర్థం చేసుకోవడం సులభం చేస్తాయి.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top