Python Comments


🧠 What Are Comments in Python?

In Python, comments are notes written in code that are ignored by the Python interpreter. They're used to make code easier to understand, explain logic, or temporarily disable code during debugging.

💡 Comments are meant for humans, not the machine.


🔹 Why Use Comments?

  • To explain complex code.

  • To improve code readability.

  • To document the purpose of code sections.

  • To disable code without deleting it.


#️⃣ Single-Line Comments in Python

Single-line comments start with a # symbol. Everything after # on that line is ignored.

# This is a single-line comment
print("Hello, World!")  # This is an inline comment

🔸 Multi-Line Comments in Python

Python doesn't have a specific multi-line comment syntax. You can:

✅ Use multiple # lines:
# This is a multi-line comment
# written in Python using
# the hash symbol
✅ Or use multi-line strings (''' or """):

Although intended for documentation strings, they can act as comments if not assigned to a variable.

'''
This is a multi-line string
used as a comment.
'''
print("Code continues...")

📝 Inline Comments

These are comments placed on the same line as a statement.

x = 5  # Assign 5 to variable x

ℹ️ Keep inline comments concise and relevant.


🚫 What Comments Do NOT Do

  • They do not affect your program’s output.

  • They are not compiled or executed.

✅ Summary

Type Syntax Example
Single-Line # comment # This is a comment
Inline After statement x = 10 # inline comment
Multi-Line ''' text ''' or # # # '''multi-line comment'''

📌 Pro Tips for Writing Comments

  • Use clear and meaningful language.

  • Avoid over-commenting obvious code.

  • Keep comments up to date as code changes.


Practice Questions

Q1. Write a Python program with a single-line comment above a print() statement.

Q2. Write a Python program that uses a comment to describe a variable assignment.

Q3. Write a Python program that adds a comment after a line which prints your name.

Q4. Write a Python program using three single-line comments (#) to explain three steps in your code.

Q5. Write a Python program that includes a multi-line comment using triple quotes (''' or """).

Q6. Write a Python program where you comment out a line that adds two numbers so it doesn't execute.

Q7. Write a Python program that includes both code and comments to explain what each part does.

Q8. Write a Python program that uses a comment to label a section, such as # Calculation.

Q9. Write a Python program that starts with a multi-line description at the top using triple quotes (''' ''').

Q10. Write a Python program and comment each block of the program: input, process, and output.


Go Back Top