Python Comments: Single-Line, Inline and Multi-Line
What Is a Comment?
A comment is a note in your code for people, not for Python. It starts with a hash sign #. Python ignores everything from the # to the end of the line:
# This program greets the user
print("Hello, Sunny!")Hello, Sunny!
The first line doesn't print anything. Python skips it completely.
Think of comments as sticky notes on your code. They explain what's going on to whoever reads it next, and that's often you, a few weeks later, who has forgotten the details.
Why write comments?
- To explain. Code says what happens; a comment can say why.
- To remember. Notes help you understand your own old programs.
- To share. Other people can read your code faster.
- To test. You can switch a line off by turning it into a comment. You'll learn this in the last topic of this chapter.
Comments can say anything
Python doesn't check spelling, grammar or language inside a comment:
# Store the price in rupees
price = 50
# Hindi, English, emojis: all fine here 🙂
print(price)50
Mind the
Without the #, Python tries to run your note as code, and stops:
This is a note
print("Hi")SyntaxError: invalid syntax
A comment starts with # and is a note for people. Python skips it.