Coding/Learn/python/comments
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnComments & Documentation
4 / 32 0%
Lesson 04 of 32
4 mins read

Comments & Documentation

PreviousNext Lesson
In Plain English

Code tells the computer HOW to do something. Comments tell humans WHY you did it. Any text on a line following a hash symbol (#) is a comment, and Python will ignore it completely when executing your code.

The Mental Model

"Comments are like sticky notes on your monitor or notes in the margin of a book. They help future you remember what you were thinking at 2 AM."

Deep Dive: How It Works

Single-line comments start with the hash character (#). Everything from that # to the end of the line is skipped by the Python interpreter.

You can also write inline comments on the same line as code: price = 100 # base price before tax.

Multi-line notes or docstrings use triple quotes (""" or '''). While technically multi-line string literals, when unassigned they serve as clean multi-line documentation.

Syntax Blueprint

# Single line comment
x = 10  # Inline comment
"""
Multi-line explanation
"""

Use # for quick notes and triple quotes for multi-line documentation.

PointCore Rules to Remember

Point star
Zero Runtime Cost: Comments do not slow down your program; Python discards them instantly.
Point star
Explain the Why: Good comments explain why unusual logic exists, not just what is obvious.
Point star
Debugging with Comments: Temporarily comment out a line of code with # to test without deleting it.

Common Beginner Traps & How to Fix Them

Mistake:Over-commenting the obvious

Why it happens: Writing x = 1 # sets x to 1 adds visual noise without helpful context.

How to fix: Comment on intent, business rules, or tricky edge cases.

Pro Tip: In ZenCompiler editor, select any lines and press Ctrl + / (or Cmd + / on Mac) to toggle comments instantly.

Live Interactive Example

Hit Run Code to see it live
Documenting a Calculation with Comments
Python 3.12
1
2
3
4
5
6
Output Console
Click "Run Code" to view the rendered output.
How it works: Python skips lines starting with # and executes the math calculations cleanly.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Add a helpful comment and print output

  • PointAdd a comment describing your code on line 1 starting with #.
  • PointOn line 2, print "System check passed".
1
2
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates