All Courses
Python Basics

Python Comments

1. What is a Comment?

  • A part of the code that is not executed when the script runs — completely ignored
  • Purpose: add descriptive text to help programmers understand the code
  • Useful when:
  • Others will read your code
  • You return to your own code after days/weeks/months
  • A section of code is complex and needs clarification

2. Types of Comments in Python

Single-Line Comment — #

  • Use the pound symbol / number sign #
  • Everything to the right of # on that line is a comment
  • Convention: leave 1 space between # and the comment text
# This is a single-line comment

print("hello")  # This is an inline comment (2 spaces before #)

Inline comments (same line as code) → use 2 spaces before #

  • You can also use # to comment out a line of code so it doesn't run:
# print("this won't run")
print("this will run")

Multi-Line Comment — """ or '''

  • Use triple quotation marks (single or double) to wrap the comment
  • Can span as many lines as needed
  • Both """ and ''' work the same way
"""
This is a
multi-line comment
using double quotes
"""

'''
This is also a
multi-line comment
using single quotes
'''

3. When to Use (and NOT Use) Comments

✅ Use comments when... ❌ Don't use comments when...
Code is complex or non-obvious The code is already self-explanatory
Explaining why something is done Just restating what the code does
Multiple people will read the code It would state the obvious

Bad comment example:

print("hello world")  # prints hello world

Anyone reading code already knows what print() does — this adds no value.

Good comment principle: Only add a comment if it genuinely aids understanding.


4. Cheat Sheet

Type Syntax Scope
Single-line # comment Rest of that line only
Inline code # comment After the code on same line
Multi-line """ comment """ or ''' comment ''' Everything inside the quotes

5. Key Takeaways & Recap

  1. Comments are ignored at runtime — they never affect program behavior
  2. # for single-line, """ or ''' for multi-line
  3. Convention: 1 space after #; 2 spaces before an inline #
  4. Comments should add value — avoid stating the obvious
  5. Good comments explain why, not just what