Lesson 9 of 70 – Python Comments
12.6%

Python Comments

Comments are notes written inside a Python program to explain the code. They help programmers understand what the code does and make programs easier to read and maintain.

Note: Python ignores comments during normal program execution. A single-line comment begins with the # symbol.
What is a Comment?

A comment is explanatory text written inside source code. Comments are intended for programmers rather than the Python interpreter.

Example:
# This is a Python comment
print("Hello Python")
Output:
Hello Python

The comment is ignored during execution, while the print() statement is executed.

Why Use Comments?

Comments improve the readability and maintainability of a program.

Comments can be used to:

  • Explain the purpose of code.
  • Describe important logic.
  • Document assumptions.
  • Make programs easier to understand.
  • Help other developers understand your code.
  • Temporarily prevent a line from executing during testing.
Single-Line Comments

A single-line comment begins with the # symbol.

Example:
# Display student name
print("Rahul")
Output:
Rahul

Everything after # on that line is treated as a comment, unless the symbol appears inside a string.

Comments Before Code

A comment can be written before a statement to explain what the statement does.

Example:
# Store the student's age
age = 20

# Display the age
print(age)
Output:
20
Inline Comments

A comment can also be written after a Python statement on the same line.

Example:
age = 20  # Student age
print(age)  # Display age
Output:
20

Inline comments should be used when they add useful information that is not already obvious from the code.

Multiple Comment Lines

Python does not have a special multi-line comment operator. To write a comment across several lines, place # at the beginning of each line.

Example:
# This program stores student information
# It stores the student's name and age
# Finally, it displays the information

name = "Amit"
age = 21

print(name, age)
Output:
Amit 21
Triple-Quoted Strings

You may see triple-quoted strings used to write text across multiple lines.

Example:
"""
This text is written
across multiple lines.
"""
Important: Triple-quoted text is technically a Python string literal, not a dedicated multi-line comment syntax.

For normal comments, using # on each line is clearer and more accurate.

Docstrings

Triple-quoted strings have an important use in Python called docstrings. A docstring documents a module, function, class, or method.

Example:
def welcome():
    """Display a welcome message."""
    print("Welcome to Python")

welcome()
Output:
Welcome to Python

Unlike ordinary comments, docstrings can be accessed by Python tools and programs.

Temporarily Disabling Code

During testing, you can place # before a line to prevent that line from executing.

Example:
print("Line 1")
# print("Line 2")
print("Line 3")
Output:
Line 1
Line 3

The second print() statement does not execute because it has been commented out.

Commenting Variables

Comments can explain variables when their purpose is not immediately clear.

Example:
# Total marks obtained by the student
marks = 450

# Maximum possible marks
total_marks = 500

print(marks)
print(total_marks)
Output:
450
500
Comments in Conditions

Comments can be used to explain the purpose of conditions and business rules.

Example:
age = 20

# Check whether the person is at least 18
if age >= 18:
    print("Adult")
Output:
Adult
Comments in Loops

Comments can explain why a loop is being used.

Example:
# Display numbers from 1 to 5
for i in range(1, 6):
    print(i)
Output:
1
2
3
4
5
Good Comments vs Poor Comments

A good comment explains information that is useful but not obvious from the code.

Poor Comment:
# Set age to 20
age = 20

This comment simply repeats what the code already says.

Better Comment:
# Minimum age required for registration
minimum_age = 18

This comment explains why the value exists.

Best Practices for Comments
  • Keep comments short and clear.
  • Explain why code exists when the reason is not obvious.
  • Avoid repeating exactly what the code says.
  • Update comments when the code changes.
  • Remove comments that are no longer correct.
  • Use meaningful variable and function names so fewer comments are needed.
  • Use docstrings to document functions, classes, and modules.
Example Program with Comments

Let's create a simple program containing several comments.

Example:
# Student information
name = "Rahul"
marks = 450
total = 500

# Calculate percentage
percentage = (marks / total) * 100

# Display student result
print("Name:", name)
print("Percentage:", percentage)
Output:
Name: Rahul
Percentage: 90.0
Practice Exercise

Write Python programs for the following tasks and add suitable comments.

  1. Store and display your name.
  2. Store your age and city.
  3. Add two numbers and display the result.
  4. Calculate the area of a rectangle.
  5. Store student marks and calculate the percentage.
  6. Write three print() statements and comment out the second one.
Key Points
  • Comments help explain Python code.
  • Python ignores ordinary comments during execution.
  • A single-line comment begins with #.
  • Inline comments can appear after a statement.
  • Multiple # lines can be used for multi-line comments.
  • Triple-quoted strings are not a dedicated comment syntax.
  • Docstrings are used to document functions, classes, modules, and methods.
  • Comments can temporarily disable individual lines during testing.
  • Good comments explain useful information rather than repeating obvious code.
  • Comments should be updated whenever related code changes.

🧠 Quick Quiz

Which symbol is used to start a single-line comment in Python?