Python Comments: Code কে সহজে বুঝানোর সম্পূর্ণ Beginner Guide


Introduction

Programming করার সময় শুধু code লিখলেই হয় না — code কেন লেখা হয়েছে সেটাও বোঝানো গুরুত্বপূর্ণ। এজন্যই ব্যবহার করা হয় comments

Python-এ comments এমন text যা programmer লিখতে পারে code explain করার জন্য, কিন্তু Python interpreter এগুলো execute করে না।

Comments ব্যবহার করলে:

  • Code পড়া সহজ হয়

  • Future-এ code modify করা সহজ হয়

  • Bug খুঁজে বের করা সহজ হয়

  • Team collaboration সহজ হয়

এই tutorial-এ আমরা শিখবো:

  • Python comments কী

  • Single line comment

  • Multiple line comment করার উপায়

  • Triple quotes দিয়ে comment

  • Real examples


Python Comment কী?

Python-এ comment হলো এমন line যা program run করার সময় ignore করা হয়।

Example:

# This is a comment
print("Welcome to Python")

এখানে প্রথম line execute হবে না।

Output:

Welcome to Python

কেন Comments ব্যবহার করা গুরুত্বপূর্ণ?

Programming-এ comments খুব গুরুত্বপূর্ণ কয়েকটি কারণে।

1️⃣ Documentation

Comment code-এর উদ্দেশ্য ব্যাখ্যা করে।

# Calculate total price of products
total = price1 + price2

2️⃣ Readability

Code পড়া সহজ হয়।

Example:

# Check if user is adult
if age >= 18:
print("Adult")

3️⃣ Debugging

Temporary ভাবে code disable করতে comments ব্যবহার করা হয়।

# print("Debug value:", total)

4️⃣ Team Collaboration

Team-এ কাজ করলে অন্য developer সহজে code বুঝতে পারে।


Method 1: Single-Line Comment (#)

Python-এ সবচেয়ে সাধারণ comment হলো hash (#) ব্যবহার করা।

Syntax

# comment text

Example

# Display welcome message
print("Hello Python")

Output:

Hello Python

Inline Comment Example

Code এর পাশে comment লেখা যায়।

age = 20 # user age
print(age)

Multiple Lines Comment করার Method (Method 1)

Python-এ official block comment নেই। তাই সাধারণত প্রতিটি line-এ # ব্যবহার করা হয়।

Example:

# print("Start program")
# print("Loading data")
# print("Processing...")

Python এগুলো ignore করবে।


Practical Example

# x = 10
# y = 20
# print(x + y)

print("Program running")

Output

Program running

Method 2: Triple Quotes Comment

Multiple line comment করার আরেকটি popular method হলো triple quotes ব্যবহার করা।

দুইভাবে লেখা যায়:

''' comment '''

অথবা

""" comment """

Example

'''
This code block is temporarily disabled
for testing purpose
'''

print("Hello Python")

Output

Hello Python

Python Docstrings কী?

Triple quotes সাধারণত docstring হিসেবে ব্যবহার করা হয়।

Docstring হলো function বা class explain করার official documentation।

Example:

def greet(name):
"""
This function prints greeting message
for the given user name
"""
print("Hello", name)

greet("Rahul")

Output:

Hello Rahul

Real-Life Analogy

ধরো তুমি Kolkata-তে একটি restaurant software বানাচ্ছো।

# Calculate bill amount
total = price + tax

৬ মাস পরে যখন আবার code দেখবে, তখন comment দেখে সহজে বুঝবে এই line কেন লেখা হয়েছিল

যদি comment না থাকে, তাহলে code বুঝতে অনেক সময় লাগবে।


Beginners এর জন্য Best Practices

✔ Important logic এর আগে comment লিখো
✔ Complex code explain করো
✔ Temporary debug করতে comment ব্যবহার করো
✔ Unnecessary comment এড়িয়ে চলো

Bad Example:

# add two numbers
x = a + b

Good Example:

# Calculate monthly salary including bonus
salary = base_salary + bonus

Short Summary

  • Python-এ comment হলো explanatory text যা execute হয় না

  • Single line comment করতে # ব্যবহার করা হয়

  • Multiple lines comment করতে:

    • অনেকগুলো #

    • অথবা ''' ''' / """ """

  • Triple quotes সাধারণত docstring হিসেবে ব্যবহার করা হয়

  • Comments code কে readable, maintainable এবং collaborative করে