COMMENT STATEMENT IN PYTHON

COMMENT STATEMENT IN PYTHON

Gocourse


Comment Statement In Python

When writing Python programs, comments are like notes you leave for yourself or others. They don’t affect how the program runs because Python simply ignores them. Instead, they serve three important purposes:

  • Making code easier to understand
  • Explaining tricky parts of the logic
  • Temporarily disabling (skipping) certain lines while testing

What Are Comments in Python?

A comment is a piece of text in your code that Python doesn’t execute.
They’re written by starting the line with the # symbol.

Example: Single-Line Comment

# This program prints a greeting
print("Hello, World!")


Here, the first line is ignored by Python. Only the print() statement runs.

Comments at the End of a Line

Comments can also appear after code on the same line:

print("Hello, Python!")  # This is an inline comment

Python executes only the print("Hello, Python!") part, but skips the text after #.

Using Comments to Disable Code

Sometimes you don’t want a line to run (maybe for testing). Just add # in front of it:

#print("This will not run")
print("This line will run")


Multi-Line Comments in Python


Unlike some languages (like Java or C), Python doesn’t have a special syntax for block comments.
But you can achieve multi-line commenting in two ways:






More topic in Python

Our website uses cookies to enhance your experience. Learn More
Accept !