Writing Single-Line and Multi-Line Comments

🏷️ Python Basics: Syntax, Variables, and Types / Basic Syntax Rules

🎯 Introduction

Comments are an essential part of writing clean, maintainable code. They help you and other engineers understand what your code does, why certain decisions were made, and how different parts of your program work together. In Python, comments are ignored during execution, so they exist purely for human readers.


⚙️ What Are Comments?

Comments are lines of text in your code that Python does not execute. They serve as notes, explanations, or reminders embedded directly within your scripts. Good comments make your code more readable and easier to debug later.

Key points about comments: - They help explain complex logic or calculations - They document why you chose a particular approach - They can temporarily disable code during testing - They improve collaboration between team members


🛠️ Single-Line Comments

A single-line comment starts with the hash symbol (#) and continues until the end of that line. Everything after the # on that line is treated as a comment.

How to write a single-line comment: - Place a # at the beginning of the comment text - The comment ends when the line ends - You can put comments on their own line or at the end of a line of code

Examples of single-line comments:

Comment on its own line: - # This is a comment explaining the next line of code - server_count = 10

Comment at the end of a line of code: - total_memory = 4096 # Memory in megabytes

Using comments to temporarily disable code: - # print("This line won't run") - print("This line will run")


📊 Multi-Line Comments

Python does not have a dedicated multi-line comment syntax like some other languages. However, there are two common ways to write comments that span multiple lines.

Method 1: Using multiple single-line comments

Simply use a # at the beginning of each line:

  • # This is a multi-line comment
  • # using multiple single-line comments
  • # Each line starts with a hash symbol

Method 2: Using triple-quoted strings

You can use triple quotes (""" or ''') to create a string that is not assigned to any variable. Python ignores these strings during execution, effectively making them multi-line comments:

  • """
  • This is a multi-line comment
  • using triple quotes.
  • It can span several lines.
  • """

Important note: Triple-quoted strings are technically string literals, not true comments. They are commonly used as documentation strings (docstrings) for functions and classes, but they work well as multi-line comments when placed alone.


🕵️ When to Use Comments

Knowing when to comment is just as important as knowing how to comment. Here are some guidelines:

Good times to use comments: - Explaining complex algorithms or calculations - Documenting why a specific value or constant was chosen - Marking sections of code for easier navigation - Leaving TODO notes for future improvements - Explaining workarounds for known issues

When comments are unnecessary: - Stating the obvious (e.g., # This adds two numbers) - Repeating what the code already clearly shows - Leaving outdated comments that no longer match the code


📋 Comparison: Single-Line vs Multi-Line Comments

Feature Single-Line Comments Multi-Line Comments
Syntax # at the start Multiple # lines or """ blocks
Best for Short notes, inline explanations Detailed explanations, documentation
Readability Quick and easy to scan Better for longer descriptions
Common use Explaining one line of code Describing a function or section
Execution Completely ignored Ignored if not assigned to a variable

✅ Best Practices for Writing Comments

Follow these simple rules to write effective comments:

  • Keep comments concise and relevant
  • Update comments when you update code
  • Use clear and simple language
  • Avoid obvious comments that state what the code already shows
  • Use comments to explain the "why," not the "what"
  • Place comments above the code they describe for better readability

💡 Quick Recap

  • Single-line comments start with # and cover one line
  • Multi-line comments use either multiple # lines or triple quotes (""")
  • Comments help explain code logic and decisions
  • Good comments focus on the "why" behind the code
  • Always keep comments up to date with your code changes

Comments are your way of communicating with other engineers (and your future self) who will read your code. Use them wisely to make your Python scripts clearer and more maintainable.


Comments are text in your code that Python ignores — they help engineers explain what the code does.


💬 Example 1: Single-line comment with #

This example shows the most basic way to add a single-line comment using the hash symbol.

# This is a single-line comment
print("Hello, engineers!")

📤 Output: Hello, engineers!


💬 Example 2: Comment after code on the same line

This example shows how to place a comment at the end of a line of code.

print("Calculating...")  # This comment explains the print statement
result = 5 + 3  # Adding two numbers together
print(result)

📤 Output: Calculating...
📤 Output: 8


💬 Example 3: Multi-line comment using triple quotes

This example shows how to write a comment that spans multiple lines using triple double-quotes.

"""
This is a multi-line comment.
It can span several lines.
Python ignores everything inside.
"""
print("Multi-line comment above is ignored")

📤 Output: Multi-line comment above is ignored


💬 Example 4: Commenting out code temporarily

This example shows how engineers use comments to disable code without deleting it.

# print("This line is commented out and will not run")
print("This line will run")
# print("This line is also commented out")

📤 Output: This line will run


💬 Example 5: Using comments to explain a calculation

This example shows a practical use of comments to document what a calculation does.

# Calculate the area of a rectangle
length = 10  # Length in meters
width = 5    # Width in meters
area = length * width  # Area formula: length x width
print(area)

📤 Output: 50


📊 Comparison: Single-Line vs Multi-Line Comments

Feature Single-Line Comment (#) Multi-Line Comment (""" """)
Syntax Starts with # Enclosed in triple quotes
Lines covered One line only Multiple lines
Best for Quick notes, disabling one line Longer explanations, documentation
Readability Compact, inline possible Clear separation from code

🎯 Introduction

Comments are an essential part of writing clean, maintainable code. They help you and other engineers understand what your code does, why certain decisions were made, and how different parts of your program work together. In Python, comments are ignored during execution, so they exist purely for human readers.


⚙️ What Are Comments?

Comments are lines of text in your code that Python does not execute. They serve as notes, explanations, or reminders embedded directly within your scripts. Good comments make your code more readable and easier to debug later.

Key points about comments: - They help explain complex logic or calculations - They document why you chose a particular approach - They can temporarily disable code during testing - They improve collaboration between team members


🛠️ Single-Line Comments

A single-line comment starts with the hash symbol (#) and continues until the end of that line. Everything after the # on that line is treated as a comment.

How to write a single-line comment: - Place a # at the beginning of the comment text - The comment ends when the line ends - You can put comments on their own line or at the end of a line of code

Examples of single-line comments:

Comment on its own line: - # This is a comment explaining the next line of code - server_count = 10

Comment at the end of a line of code: - total_memory = 4096 # Memory in megabytes

Using comments to temporarily disable code: - # print("This line won't run") - print("This line will run")


📊 Multi-Line Comments

Python does not have a dedicated multi-line comment syntax like some other languages. However, there are two common ways to write comments that span multiple lines.

Method 1: Using multiple single-line comments

Simply use a # at the beginning of each line:

  • # This is a multi-line comment
  • # using multiple single-line comments
  • # Each line starts with a hash symbol

Method 2: Using triple-quoted strings

You can use triple quotes (""" or ''') to create a string that is not assigned to any variable. Python ignores these strings during execution, effectively making them multi-line comments:

  • """
  • This is a multi-line comment
  • using triple quotes.
  • It can span several lines.
  • """

Important note: Triple-quoted strings are technically string literals, not true comments. They are commonly used as documentation strings (docstrings) for functions and classes, but they work well as multi-line comments when placed alone.


🕵️ When to Use Comments

Knowing when to comment is just as important as knowing how to comment. Here are some guidelines:

Good times to use comments: - Explaining complex algorithms or calculations - Documenting why a specific value or constant was chosen - Marking sections of code for easier navigation - Leaving TODO notes for future improvements - Explaining workarounds for known issues

When comments are unnecessary: - Stating the obvious (e.g., # This adds two numbers) - Repeating what the code already clearly shows - Leaving outdated comments that no longer match the code


📋 Comparison: Single-Line vs Multi-Line Comments

Feature Single-Line Comments Multi-Line Comments
Syntax # at the start Multiple # lines or """ blocks
Best for Short notes, inline explanations Detailed explanations, documentation
Readability Quick and easy to scan Better for longer descriptions
Common use Explaining one line of code Describing a function or section
Execution Completely ignored Ignored if not assigned to a variable

✅ Best Practices for Writing Comments

Follow these simple rules to write effective comments:

  • Keep comments concise and relevant
  • Update comments when you update code
  • Use clear and simple language
  • Avoid obvious comments that state what the code already shows
  • Use comments to explain the "why," not the "what"
  • Place comments above the code they describe for better readability

💡 Quick Recap

  • Single-line comments start with # and cover one line
  • Multi-line comments use either multiple # lines or triple quotes (""")
  • Comments help explain code logic and decisions
  • Good comments focus on the "why" behind the code
  • Always keep comments up to date with your code changes

Comments are your way of communicating with other engineers (and your future self) who will read your code. Use them wisely to make your Python scripts clearer and more maintainable.

Interactive Views

You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.

Comments are text in your code that Python ignores — they help engineers explain what the code does.


💬 Example 1: Single-line comment with #

This example shows the most basic way to add a single-line comment using the hash symbol.

# This is a single-line comment
print("Hello, engineers!")

📤 Output: Hello, engineers!


💬 Example 2: Comment after code on the same line

This example shows how to place a comment at the end of a line of code.

print("Calculating...")  # This comment explains the print statement
result = 5 + 3  # Adding two numbers together
print(result)

📤 Output: Calculating...
📤 Output: 8


💬 Example 3: Multi-line comment using triple quotes

This example shows how to write a comment that spans multiple lines using triple double-quotes.

"""
This is a multi-line comment.
It can span several lines.
Python ignores everything inside.
"""
print("Multi-line comment above is ignored")

📤 Output: Multi-line comment above is ignored


💬 Example 4: Commenting out code temporarily

This example shows how engineers use comments to disable code without deleting it.

# print("This line is commented out and will not run")
print("This line will run")
# print("This line is also commented out")

📤 Output: This line will run


💬 Example 5: Using comments to explain a calculation

This example shows a practical use of comments to document what a calculation does.

# Calculate the area of a rectangle
length = 10  # Length in meters
width = 5    # Width in meters
area = length * width  # Area formula: length x width
print(area)

📤 Output: 50


📊 Comparison: Single-Line vs Multi-Line Comments

Feature Single-Line Comment (#) Multi-Line Comment (""" """)
Syntax Starts with # Enclosed in triple quotes
Lines covered One line only Multiple lines
Best for Quick notes, disabling one line Longer explanations, documentation
Readability Compact, inline possible Clear separation from code