Comments in Python
📝 What are comments in Python?
Comments are lines in code that are ignored by the interpreter.
They are used to explain code for humans.
1. Single-line comments
Use #
# This is a comment
print("Hello") # This is also a comment
✔ Used for short explanations
✔ Most common type
2. Multi-line comments (not official, but used)
Python doesn’t have real multi-line comment syntax like other languages.
Method 1: Multiple #
# This is line 1# This is line 2# This is line 3
Method 2: Triple quotes (doc-style, not true comments)"""
This looks like a comment
but actually it's a string
"""
⚠ This works only if not assigned to a variable (otherwise it's a string).
3. Docstrings (special comments for documentation)
Used inside functions, classes, modules:
def add(a, b):
"""This function returns sum of two numbers"""
return a + b
thanks
ReplyDelete