Learning goals
- Write comments in Python code.
- Know that Python does not run comments.
- Write comments that explain why, not only what.
A comment is a note in your code. It is information for the reader. Python ignores it, so it is not run.
Everything after a hashtag # is a comment:
# This line is a comment. Python skips it.
otto.forward(50)
You can write a comment on its own line. You can also write it after an instruction:
otto.forward(50) # Otto walks 50 steps
Python only ignores the part after the #. The instruction before it still runs.
otto.forward(50)
# otto.left(90)
otto.forward(50)
Here, Otto does not turn. He walks 100 steps in a straight line.
Do not only repeat the code. Tell why you do something, or how it works.
# Bad: repeats the code
otto.left(90) # turn left 90
# Good: tells why
otto.left(90) # Turn a quarter so the next side of the square can start
Keep comments short and clear. Do not comment on every line. Write them where the reader might get lost.
# comment on its own line
otto.forward(50) # comment after an instruction
| Idea | What it means |
|---|---|
# |
Starts a comment. Everything after it is ignored. |
| Comment | A note for the reader. It is not run. |