Python Comments

  • Comments in Python are used to explain code, improve readability, and help developers understand the purpose of a program or logic. Python supports single-line comments using the # symbol and multi-line comments using triple quotes (''' or """). Comments are ignored by the Python interpreter, which means they do not affect the program’s execution.


  • 💬 What is a Comment in Python?

    A comment in Python is a line (or part of a line) of text that is not executed by the Python interpreter. Comments are written to explain code, make it more readable, and help developers debug or maintain programs.

    ✅ Why We Use Comments in Python

    • To explain what the code does

    • To improve code readability

    • To help others (and yourself) understand logic later

    • To temporarily disable code during testing/debugging

Note:

Comments are ignored by Python and do not affect the output.
They are used to explain code, improve readability, and help during   debugging.

  • 1️⃣ Creating a Single-Line Comment

    A single-line comment begins with the # symbol.
    Everything written after # on the same line is ignored by Python.

Single-Line Comment Example

This example shows how a comment starting with # describes what the program does without affecting execution.

# Displaying a welcome message
print("Welcome to Python!")
  • 2️⃣ Inline Comment Example

    Inline comments are written on the same line as code to explain that line.

Inline Comment Example

This example demonstrates how comments can be added at the end of a line to clarify its purpose.

print("Learning Python")   # printing a message to the screen
  • 3️⃣ Using Comments to Disable Code

    Comments can be used to temporarily turn off code so Python will not execute it.

Disabling Code Using Comments

This example shows how commenting out a line prevents it from executing.

#print("This line is turned off")
print("This line will run")
  • 4️⃣ Multi-Line Comments (Two Methods)

    Python does not have a dedicated multi-line comment syntax, but we can write multi-line comments using the following methods:

    🔹 Method 1: Using Multiple # Symbols (Recommended)

    Add # at the beginning of each line.

Multi-Line Comments Using #

This example uses multiple # symbols to explain a code block. It is the most recommended and readable method.

# This section calculates
# the total price after tax
# using a fixed tax rate
price = 500
tax = 0.18
print(price + price * tax)
Note:

Follows industry best practices
Clear and readable in real-world projects


  • 🔹 Method 2: Triple-Quoted Strings

    Triple quotes (""" """ or ''' ''') can act as multi-line comments if not assigned to a variable.

Multi-Line Comments Using Triple Quotes

This example uses a triple-quoted string as a block comment to explain a set of instructions.

"""
This block describes the
purpose of the following code:
calculating the area of a rectangle
"""
length = 6
width = 4
print("Area =", length * width)
Note:

Useful for long explanations

  • 5️⃣ Inline Comment with Variable

    Inline comments can also explain variable usage.

Inline Comment with Variable

This example shows how to place a comment next to a variable assignment.

age = 20  # storing age of the user
  • 6️⃣ Comments for Functions and Logic

    Comments are very helpful for explaining function behavior or complex logic.

Comment for Function Explanation

This example uses a comment to describe what the function does.

# Function to check if the number is even
def is_even(num):
    return num % 2 == 0

print(is_even(10))
  • 7️⃣ Comments for Testing / Debugging

    Developers often comment out code temporarily while testing.

Debugging with Comments

This example shows how comments help test different values without deleting code.

# Temporary value for testing
# salary = 55000
salary = 62000

print(salary)