Comments in Python
Comments in Python are notes written inside a program to explain what the code is doing. Comments are not executed by the computer. They are only written for humans to read and understand the code better.
In simple words, comments help explain the code.
Real-Life Analogy
Comments are like writing notes in a notebook or adding instructions beside a problem.
- A teacher writes extra notes near a math problem to explain it.
- In the same way, comments help programmers understand and remember the code easily.
For example:
# This prints a message
print("Hello")
Output
Hello
What are Comments?
Comments in Python are notes written inside the code to explain what the program is doing.
- They do not affect the output.
- Python ignores comments when running the program.
- Comments make code easy to read and understand.
Syntax of Comments
# This is a comment
Why Do We Use Comments in Python?
Comments help us:
- Explain what code does
- Make programs easy to understand
- Remember important instructions
- Make code clean and readable
- Help others understand our programs
Practical Example
Like writing notes in a notebook to understand your work better, comments help explain your code.
# This is a comment
print("Hello, World!")
# Adding two numbers
print(5 + 3)
Output
Hello, World!
8
Types of Comments in Python
1. Single-Line Comment
A single-line comment is used to write a note in one line.
It starts with the # symbol.
Example
# This is a single-line comment
print("Hello")
Output
Hello
2. Multi-Line Comment (Using Triple Quotes)
Python does not have a special multi-line comment symbol like some languages.
We can write multi-line explanations using:
Method 1: Using Multiple
# This is line 1
# This is line 2
# This is line 3
Method 2: Using Triple Quotes
"""
This is a multi-line text
used for explanation
or documentation
"""
Examples of Comments in Python
Example-1
# Print student name
print("Ravi")
Output
Ravi
Example-2
# Store two numbers
a = 5
b = 3
# Add numbers
print(a + b)
Output
8
➤ Key Points
● Comments are notes written in the code
● They are not executed by the computer
● Written using the # symbol
● Help in understanding the program
● Make code clean and readable
➤ Tips for Students
● Use comments to explain your code
● Keep comments short and clear
● Do not overuse comments
● Write comments for important parts of the code
➤ Why Comments Are Important
Comments are useful for:
- Beginners learning Python
- Explaining difficult code
- Remembering program steps
- Making programs easier to maintain
- Writing better and cleaner code
Comments in Python are notes written inside a program to explain the code. They do not run with the program and do not change the output.
Comments make coding simple, clear, and easy to understand for students and programmers.
Learning comments is like learning to write notes beside your work so you can understand it later.
