Ad space (banner)
🐍Python Lessons
Lesson 18 / 69

Writing Comments

This lesson covers how to write comments in Python, so you can leave code that's easy to understand when you come back to it later. It's written for beginners searching "Python how to comment code".

A comment is a note for humans that has no effect on how the program runs. Anything after a # is a comment. For multi-line notes, triple double-quotes are also commonly used. Comments exist to make code easier to understand later — for yourself or for someone else.

The sample code uses a single-line comment to explain a variable, and a multi-line comment (triple-quoted) for a longer note. Writing down "why this logic is needed" is enormously helpful when you or a teammate revisits the code months later.

A common beginner mistake is over-commenting, to the point where the comments actually make the code harder to read. What the code does is often clear just from reading the code itself, so comments are most effective when they explain "why," not "what." A """ comment used to describe a function is also called a "docstring."

In real development, whether or not comments exist makes a huge difference on a team. Docstrings are often picked up automatically by documentation tools, and how well someone writes comments is genuinely considered part of their professional coding skill.

Python
OUTPUT

💡 The Python engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)