Python Comments

Python · Getting Started

Theory

A comment is text in your code meant for humans. In Python a comment starts with # and runs to the end of that line; the tokenizer throws it away before the code is compiled, so it has zero effect on how the program runs. You can put a comment on its own line or after code on the same line, like total = sum(sales) # add up all sales.

Python has no true multi-line comment syntax. A triple-quoted string such as """some text""" is actually a string value; if it is not assigned to anything it is simply evaluated and discarded. When it is the very first statement inside a module, function or class it becomes that object's docstring, which help() and tools can read through the __doc__ attribute. So use # for comments and triple quotes for documentation.

Why we use it

Comments explain why the code does something, which the code alone cannot say. They help teammates, and your future self, understand a tricky formula or a business rule, and # is the quickest way to temporarily disable a line while debugging.

When to use it

Comment the reason behind non-obvious logic, assumptions and TODOs. Do not comment what is already obvious (x = x + 1 # add one). For describing what a function does, write a docstring instead of a # block.

How it works

  1. Python reads a line of source code
  2. Is there a # outside a string? Yes: Drop everything from # to line end. No: Keep the whole line as code.
  3. Lone triple-quoted string first in a def/class? Yes: Store it as the docstring. No: Evaluate and discard it.
  4. Compile the remaining code to bytecode
  5. Run the code; comments never execute

Syntax

# single line
""" multi-line comment """

Example

# Calculate total sales
total = sum(sales)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic