What is a comment?
A comment is a note you write inside your code for people to read. Python completely ignores it. A comment starts with the # symbol.
# This program greets the user
print("Welcome!") # this part is also a commentOutput
Welcome!
Only Welcome! is printed. Everything after # on a line is skipped.
Why write comments?
- To explain why you did something.
- To remind yourself what the code does when you come back weeks later.
- To help teammates understand your work.
Turning code off
You can put # in front of a line of code to switch it off for a moment without deleting it:
print("This line runs")
# print("This line does not run")Output
This line runs
Tip: Good comments explain the why. You don't need a comment like
# print helloaboveprint("hello")– the code already says that.