What are Python Comments?
A comment in Python is a line (or part of a line) that:
✅ Is ignored by Python
✅ Does NOT run as code
✅ Is written for humans to read
Think of comments as notes inside your program that explain what is happening.
Simple Example
# This is a comment
print("Hello, world!")
Output:
Hello, world!
Python skips the comment and runs only the print() line.
Why Comments Are Important
Comments help you:
Without comments, code can become confusing very quickly.
Types of Python Comments
Python has two main types:
1️⃣ Single-Line Comment (#)
This is the most common.
# This is a single-line comment
print("Python is fun")
You can also place it after code:
print("Python") # This prints a word
2️⃣ Multi-Line Comments (Using Triple Quotes)
Python does not have a true multi-line comment symbol like /* */.
Instead, we use:
""" This is a multi-line comment It can span many lines """
OR
''' This is also a multi-line comment '''
Technically these are multi-line strings, but they are used as comments when not assigned to a variable.
Python Comment Types
| Comment Type | Symbol Used | Purpose | Example |
|---------------------------|------------|----------------------------------|----------------------------------|
| Single-line comment | # | Short explanation | # This is a comment |
| Inline comment | # | Comment after code | print("Hi") # greeting |
| Multi-line (doc/comment) | """ """ | Long explanation or documentation| """ multi-line comment """ |
| Multi-line (doc/comment) | ''' ''' | Same as above | ''' multi-line comment ''' |
Example
❌ Without Comments
x = 5 y = 10 z = x + y print(z)
You may forget what this does later.
✅ With Comments
# First number x = 5 # Second number y = 10 # Add the two numbers z = x + y # Display the result print(z)
Now everything is clear 👍
Using Comments to Disable Code (For Testing)
print("This will run")
# print("This will NOT run")
Where Comments Are Commonly Used
1. Explaining Variables
# Store the user's age age = 25
2. Explaining Logic
# Check if the user is an adult
if age >= 18:
print("Adult")
3. Creating Sections in Code
# ========================= # USER AUTHENTICATION # =========================
Docstrings (Very Important in Python)
Docstrings are special multi-line comments used in:
They explain what the code does.
Example
def add(a, b):
"""
This function adds two numbers
and returns the result.
"""
return a + b
You can access it using:
print(add.__doc__)
Docstring vs Normal Comment
| Feature | Normal Comment (#) | Docstring (""" """) |
|---------------|--------------------|-----------------------------|
| Used for | Small notes | Documentation |
| Accessible | No | Yes |
| Used in | Anywhere | Functions, classes, modules |
| Multi-line | No | Yes |
Beginner vs Good Comments
❌ Bad Comment
# add 1 to i i = i + 1
This is obvious and unnecessary.
✅ Good Comment
# Increase login attempt count for security tracking login_attempts += 1
Explains WHY, not just WHAT.
Best Practices
✔ Write comments for complex code
✔ Keep them short and clear
✔ Update comments when code changes
✔ Use docstrings for functions and classes
✔ Avoid obvious comments
Common Mistakes
1. Too Many Comments
# create variable name = "Danny"
Not useful.
2. Outdated Comments
# stores email username = "Danny"
This causes confusion.
3. Commenting Every Line
Makes code messy and harder to read.
# Start of simple login system
# Stored username and password
correct_username = "admin"
correct_password = "1234"
# Get input from user
username = input("Enter username: ")
password = input("Enter password: ")
# Check if the details are correct
if username == correct_username and password == correct_password:
print("Login successful")
else:
print("Invalid login")