Python Comments

Single-line comments and docstrings.

✍️ 3 exercises + code challenge in this lesson

Comments explain your code and are ignored by Python.

  • A comment starts with # and runs to the end of the line.
  • For multi-line notes, use several # lines, or a triple-quoted string """...""".
  • A triple-quoted string right after a function definition is a docstring: the function's documentation.

Key Takeaways: Python Comments

Mastering Python Comments helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Python Comments Code Example

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

"""
This text is not assigned to anything,
so Python ignores it.
"""

def area(r):
    """Return the area of a circle with radius r."""
    return 3.14159 * r * r

print(area(2))
print(area.__doc__)
Free sign-in required · Google login available

Python Comments Practice Exercises

Check what you learned in “Python Comments”.

/ 3 solved
Fill in the blank

Write a comment.

 This is a comment
✓ Correct! Not quite. Try again.
Multiple choice

What is a docstring?

✓ Correct! Not quite. Try again.
Code challenge

Comment out the second line so only "Keep" is printed.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services