Comments in Python

Lines in your code that Python never runs โ€” written for humans, to explain intent, leave notes, or switch off code while debugging.

Overview

This topic covers how to write comments in Python. Comments are lines in code that are not executed by the Python interpreter โ€” they exist purely for humans to read, to explain what the code does, add notes, or temporarily disable code.

Key Concepts

Syntax

# comments

A bare comment โ€” just marks that comments are being demonstrated.

# this is a comment

A descriptive single-line comment explaining code or intent.

#

An empty comment (just a # with nothing after it) โ€” technically valid but has no content.

Important Points

Things to Remember

Quick Revision

In Python, anything after # on a line is a comment and is ignored during execution. Comments help explain code, make it more readable, and can be used to disable a line temporarily while testing. Python only supports single-line comments natively.