Comments are notes written in the source code for humans to read; the Python interpreter ignores them. They explain what the code does, why a decision was made, or what still needs to be done. Good comments make programs easier to understand, debug and maintain, especially when working in a team or returning to your own code months later.
Key terms
- Comment
- a note in code ignored by the interpreter
- Single-line comment
- text after # on a line
- Inline comment
- a comment on the same line as code
- Docstring
- a triple-quoted description of a module, function or class
- Commenting out
- disabling code by turning it into a comment
- Readability
- how easily code can be understood
Summary
- Comments are ignored by Python but help humans.
- # starts a single-line or inline comment.
- Docstrings describe modules, functions and classes and can be read with help().
- Explain why, keep comments current, and use # to comment out code while debugging.
In everyday life: Software teams at Ethiopian tech companies and start-ups rely on clear comments so that new developers can understand and maintain existing code.
Check yourself
- Which symbol starts a Python comment?
- How is a docstring different from a normal comment?
- Why can outdated comments be harmful?
Practice questions
1. Which symbol starts a single-line comment in Python?
- A. //
- B. #
- C. /*
- D. --
Show answer
Answer: #
Python ignores everything after # on a line. // is floor division in Python, and /* and -- are comment markers in other languages.
On WeLearn
The full lesson: reading notes, worked examples, class activities and the Unit 6 quiz
Study Comments in Python on WeLearnTeach Comments in Python with WeLearn
We will bring a demo to your school: a lesson on your interactive TV, a virtual lab and a classroom game, with your own teachers.
