PY-101 ยท Getting started with Python
Comments
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
A comment is text in your code that Python ignores. In Python a comment starts with # and runs to the end of the line. Use comments to explain why code does something, and to switch a line off while you debug.
- Write single-line comments with #
- Comment out code while debugging
- Know what a docstring is
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
# This line is a comment. Python skips it.price = 250 # price in rupeestax = price * 0.18 # 18% GST# print("debug:", tax)print("Total:", price + tax)"""A string on its own like this is also skipped."""
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Total: 295.0 ๐ฏ The program prints a debug line. Comment it out (do not delete it) so only Ready is printed.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
Put # at the start of the first line.
- The problemYou open code you wrote three months ago. One line multiplies by 0.18 and you have no idea why. A five-word comment would have saved you twenty minutes.
- The Python idea
# a comment on its own lineA comment is text in your code that Python ignores. - In real lifeWhen a bug appears, developers often comment out one line at a time to find which one causes it. It is faster than deleting and retyping.
Start with a question
You open code you wrote three months ago. One line multiplies by 0.18 and you have no idea why. A five-word comment would have saved you twenty minutes.
Code is read far more often than it is written. A short comment that explains a decision saves the next person, often you, an hour.
Explain it like I'm 10
A comment is a sticky note for people. Python sees the # and skips the rest of the line, the way you skip the notes in the margin of a book.
The technical explanation
Everything after # on a line is ignored. A comment can sit on its own line or after code on the same line.
Python has no special multi-line comment. Start each line with #. A string in triple quotes on its own line is ignored too, and when it is the first line of a function it becomes a docstring, the help text for that function.
Good comments say why, not what. # 18% GST is useful. # multiply price by 0.18 repeats the code.
Syntax
# a comment on its own line
x = 5 # a comment after code Real-world scenario
When a bug appears, developers often comment out one line at a time to find which one causes it. It is faster than deleting and retyping.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Variables โQuestions people ask
How do I comment multiple lines in Python?
Put # at the start of each line. Most editors do it for you: select the lines and press Ctrl+/ (Cmd+/ on a Mac).
Do comments slow down a Python program?
No. Comments are thrown away when Python reads the file, before anything runs.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner How do I comment multiple lines in Python?
Put # at the start of each line. Most editors do it for you: select the lines and press Ctrl+/ (Cmd+/ on a Mac).
beginner Do comments slow down a Python program?
No. Comments are thrown away when Python reads the file, before anything runs.
intermediate Which character starts a comment in Python?
#. Python comments start with #. // is used in C, Java and JavaScript.
intermediate What does Python do with a comment?
Ignores it. The interpreter skips comments completely.
Quick revision
Remember
- Write single-line comments with #
- Comment out code while debugging
- Know what a docstring is
- Comment code out to switch it off while you test, but delete it before you share. Old commented-out code confuses the next reader.
Syntax
# a comment on its own line
x = 5 # a comment after code Official documentation
- Lexical analysis: comments (docs.python.org)