Lesson 7 +10 XP

Comments

Comments

Comments are notes you leave in your code for yourself and others. Python ignores them completely.

Single-line comments with #

Anything after a # on a line is a comment:

# This is a comment
print("Hello")  # This comment sits at the end of a line

Multi-line comments

Python has no built-in multi-line comment. People use two tricks:

Trick 1: a # on each line

# This is line one
# This is line two
# This is line three

Trick 2: a multi-line string

"""
This is a multi-line string.
Python ignores it when it's not assigned,
so it works like a comment.
"""

Why comment?

  • Explain the "why", not just the "what". The code shows what; comments say why.
  • Leave warnings about tricky parts.
  • Temporarily disable a line while debugging.

Keep them useful

Too many comments are as bad as none. Write clear code, then use comments where they add real value.

TL;DR

  • # starts a single-line comment.
  • Use # on each line or a triple-quoted string for multi-line notes.
  • Comments explain the "why" and are ignored by Python.