Sunlit corridor of Paris' Palais Royal highlighting classic architecture.

Table of Contents

Quick question — if your code runs perfectly without comments, why bother writing them?

Here’s a scenario. You write 50 lines of Python today. Everything works, you’re happy. You come back to the same file three months later and stare at it thinking — what on earth was I doing here?

That’s what comments are for. They’re notes you leave for yourself — and for anyone else who might read your code. Python ignores them completely when running the program. They’re entirely for humans.

Single-Line Comments

Use the # symbol to write a single-line comment. Anything after # on that line is ignored by Python:

				
					# This prints a greeting
print("Hello, World!")

				
			

You can also put a comment at the end of a line of code — called an inline comment:

				
					age = 25   # storing the user's age
				
			

Use inline comments sparingly though. If the code is clear enough on its own, you don’t need to explain it. Good code should be somewhat self-explanatory — comments fill in the gaps, they don’t replace clarity.

Multi-Line Comments

When you need to write longer notes — explaining what a section does, leaving a to-do, describing a complex logic — use triple quotes:

				
					"""
This section calculates the total price.
It takes quantity and unit price as inputs
and returns the final amount after tax.
"""

				
			

Or with single quotes:

				
					'''
Same thing works with single triple quotes.
Python treats both the same way.
'''

				
			

Technically, these are string literals that Python evaluates and immediately discards — not ‘true’ comments like #. But in practice, they serve exactly the same purpose and are used widely for multi-line notes.

Commenting Out Code

Here’s a very practical use of comments that beginners discover early — temporarily disabling a line of code without deleting it:

				
					print("This line runs")
# print("This line is disabled")
print("This line runs too")

				
			

This is useful when you’re testing or debugging. Maybe one line is causing an error and you want to check if everything else works without it. Just comment it out, run the program, and uncomment it later.

VS Code makes this even easier — select any line and press Ctrl + / on Windows or Cmd + / on Mac to instantly toggle comments on and off. One of those shortcuts you’ll use almost every day.

What Makes a Good Comment?

Not all comments are created equal. Here’s a quick gut check:

  • Bad comment — states the obvious: # adding 1 to x (the code already says that)
  • Good comment — explains the why: # index starts at 1 here because we skip the header row

If the code clearly shows what is happening, a good comment explains why it’s happening. That’s the difference between a comment that helps and one that just adds noise.

Quick Recap

  • # is for single-line comments — Python ignores everything after it on that line
  • Triple quotes (“”” or ”’) are used for multi-line comments
  • You can comment out code temporarily while debugging
  • Ctrl + / (Windows) or Cmd + / (Mac) toggles comments in VS Code
  • Good comments explain why, not just what

Comments are one of those things that feel unnecessary when you’re just starting out — and absolutely essential once your programs get longer and more complex. Start the habit early. Your code will be cleaner, your debugging faster, and anyone reading your work (including future you) will have a much easier time.