….
💬 Python Comments – GoNimbus Learning Series
🧠 Why Use Comments in Python?
In Python, comments are crucial for writing clean, understandable, and maintainable code. You can use comments to:
✅ Explain complex logic to other developers (or your future self)
✅ Improve code readability
✅ Prevent certain lines from executing during debugging
✅ Quickly test alternative solutions without deleting code
✍️ Creating a Single-Line Comment
In Python, a comment begins with a #
symbol. Everything after #
on that line is ignored by the interpreter.
✅ Example:
# This is a comment
print("Hello, GoNimbus!")
You can also place a comment at the end of a code line:
print("Learning Python at GoNimbus!") # This is an inline comment
⛔ Using Comments to Disable Code Execution
Need to skip a line temporarily while testing? Just comment it out!
✅ Example:
# print("This line is disabled")
print("Only this will print")
🧾 Multi-line Comments in Python
Python doesn’t have a special syntax for multi-line comments, but you have two clean options:
1. Use #
on each line:
# This is a comment
# that spans multiple
# lines in the script
print("Multi-line comment demo")
2. Use triple quotes ('''
or """
) as a block comment:
Python ignores unassigned string literals, so triple-quoted strings work as pseudo multi-line comments.
"""
This is a block of text
used as a multi-line comment.
Python will ignore it.
"""
print("GoNimbus Python Class")
🔔 Note: Triple quotes are primarily for docstrings, but can be used as comments when not assigned to a variable.
🧪 Pro Tip – Docstrings vs Comments
- Use
#
for in-line and quick comments. - Use
""" """
for documentation strings inside functions, classes, or modules.
Example:
def greet():
"""This function prints a welcome message"""
print("Welcome to GoNimbus!")
📘 Summary Table – Python Comments
Type | Syntax Example | Purpose |
---|---|---|
Single-line | # This is a comment | General explanation |
End-of-line | print("Hi") # This is a comment | Quick note on the same line |
Multi-line (# ) | # Line 1\n# Line 2 | Comment block |
Multi-line (""" ) | """ This is a long comment """ | Optional comment block (ignored) |
Disable code | # print("Oops!") | Temporary skip during testing |
🧠 Practice Task for You
Try writing a Python program and add comments explaining each step:
# Ask the user for their name
name = input("Enter your name: ")
# Greet the user
print("Hello, " + name + "! Welcome to GoNimbus.")
✨ With clear comments, your Python code becomes readable, teachable, and collaborative — just like what we value at GoNimbus.