Comments in Python

The 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 written only for humans to read and understand 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 code easily.

Example

# This prints a message
print("Hello")

Output

Hello

What are Comments in Python?

Comments in Python are notes written inside code to explain what the program is doing.

  • They do not affect the output
  • Python ignores comments while 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
  • Keep 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

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 separate multi-line comment symbol, but we can write multi-line explanations in two ways.

Method 1: Multiple # Symbols

# 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


➤ Summary

  • Comments are notes written in code
  • They are not executed by the computer
  • Written using the # symbol
  • Help in understanding programs
  • Make code clean and readable

➤  Why Comments Are Important

Comments in Python are useful for:

  • Beginners learning Python
  • Explaining difficult code
  • Remembering program steps
  • Making programs easier to maintain
  • Writing cleaner code

Learning comments is like writing notes beside your work so you can understand it later.

➤ Tips 

  • Use comments to explain your code
  • Keep comments short and clear
  • Do not overuse comments
  • Write comments for important parts
 

Check your knowledge

Quickly verify what you've learned from this tutorial.

Question 1

What is a comment in Python?

Comments are notes used to explain code.

Question 2

Are comments executed by Python?

Python ignores comments when running a program.

Question 3

Why do programmers use comments?

Comments make code easier to understand and maintain.

Question 4

What is a single-line comment?

A comment is just for humans to read. The computer does not execute it.

Question 5

What is the difference between single-line and multi-line comments?

Single-line comments are used for one line, while multi-line comments cover multiple lines.

Congratulations!

You've successfully mastered the knowledge check for "Comments in Python."

For more questions and practice, click the link below:

Practice More Questions
Previous Topic print() Function in Python Next Topic Variables and Data Types