2.3 Comments

View Categories

2.3 Comments

4 min read

Comments are used to add notes, explanations, or reminders inside a program. They make the code easier to read and understand without affecting how the program executes.

Python completely ignores comments during execution. They are meant only for programmers.

Comments are commonly used to:

  • Explain the purpose of the code.
  • Improve code readability.
  • Temporarily disable code during testing.
  • Add documentation and reminders.

Single-Line Comment #

A single-line comment begins with the # (hash) symbol.

Everything after the # on that line is treated as a comment.


Example 1: Writing a Single-Line Comment #

main.py

# This program prints a welcome message.

print("Welcome to Python")

Output #

Welcome to Python

Explanation #

  • The line beginning with # is a comment.
  • Python ignores the comment during execution.
  • Only the print() statement is executed.
  • Comments help explain what the program does.

Example 2: Commenting Individual Statements #

main.py

# Store employee information
name = "Alice"

# Store employee age
age = 25

# Display employee information
print(name)
print(age)

Output #

Alice
25

Explanation #

  • Each comment describes the purpose of the statement that follows.
  • Comments improve code readability, especially in large programs.
  • They do not become part of the program output.

Example 3: Inline Comments #

main.py

marks = 95      # Student marks
bonus = 5       # Extra marks

print(marks + bonus)

Output #

100

Explanation #

  • An inline comment appears after a statement on the same line.
  • Everything after the # symbol is ignored by Python.
  • Inline comments should be short and meaningful.

Example 4: Using Multiple Comments #

main.py

# Program to calculate the total amount

price = 500
tax = 50

# Display the final amount
print(price + tax)

Output #

550

Explanation #

  • Comments can be placed before important sections of code.
  • They help divide a program into logical parts.
  • This makes programs easier to understand and maintain.

Example 5: Multi-Line Comments #

Python does not have a special syntax for multi-line comments.

Instead, write multiple single-line comments.

main.py

# This program demonstrates
# how multiple comments
# can be written together.

print("Python")

Output #

Python

Explanation #

  • Each line begins with the # symbol.
  • Python treats every line as a separate comment.
  • This is the recommended way to write comments spanning multiple lines.

Example 6: Using Triple Quotes as Documentation #

main.py

"""
This program prints
a welcome message.
"""

print("Welcome")

Output #

Welcome

Explanation #

  • Triple quotes (''' or """) create a multi-line string.
  • When such a string is not assigned to a variable, it has no effect on the program.
  • Although many beginners use triple quotes as comments, they are actually string literals, not true comments.
  • Triple-quoted strings are mainly used for documentation strings (docstrings), which will be discussed in a later lesson.

Note: For regular comments, prefer using the # symbol.


Example 7: Temporarily Disabling Code #

main.py

print("Program Started")

# print("This line is temporarily disabled.")

print("Program Finished")

Output #

Program Started
Program Finished

Explanation #

  • The second print() statement has been commented out.
  • Since it begins with #, Python ignores it.
  • This technique is often used while testing or debugging programs.

Example 8: Comments Do Not Affect Program Execution #

main.py

# First number
a = 20

# Second number
b = 30

# Print the sum
print(a + b)

Output #

50

Explanation #

  • Comments do not change the behavior of the program.
  • Removing all comments from this program produces the same output.
  • Their only purpose is to help programmers understand the code.

Common Mistakes #

1. Forgetting the # Symbol #

Incorrect

This program prints a message.

print("Hello")

Output #

SyntaxError: invalid syntax

Reason

Python tries to interpret the text as Python code.

Correct

# This program prints a message.

print("Hello")

2. Using Comments Instead of Meaningful Variable Names #

Poor Practice

# Store employee age
x = 25

Better Practice

employee_age = 25

Reason

Good variable names reduce the need for excessive comments.


3. Writing Obvious Comments #

Poor Practice

# Print Hello
print("Hello")

Better Practice

print("Hello")

Reason

Comments should explain why something is done, not simply repeat what the code already makes obvious.


4. Assuming Triple Quotes Are Comments #

Common Misconception

"""
This is a comment.
"""

Reason

This is actually a string literal, not a comment.

For normal comments, always use:

# This is a comment.

Best Practices #

  • Write comments that explain why, not just what.
  • Keep comments short, clear, and meaningful.
  • Update comments whenever the related code changes.
  • Avoid unnecessary or obvious comments.
  • Use # for regular comments.
  • Reserve triple-quoted strings for documentation (docstrings).

Key Points to Remember #

  • Comments are ignored during program execution.
  • A single-line comment begins with the # symbol.
  • Python does not have a dedicated syntax for multi-line comments.
  • Multiple single-line comments can be used to write longer explanations.
  • Triple-quoted strings are primarily intended for documentation (docstrings), not regular comments.
  • Comments improve code readability and maintainability.
  • Comments do not affect the output or behavior of a program.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *