Comments and Indentation in Python 📝

Python is known for its readability and clean syntax. Two important aspects of writing clean Python code are comments and indentation.
1️⃣ Comments in Python 🗨️
Comments are ignored by the Python interpreter and are used to explain code, making it easier to read and understand.
🔹 Single-Line Comments
Use the # symbol for single-line comments.
# This is a comment
print("Hello, World!") # This prints a message
🔹 Multi-Line Comments
Python doesn’t have a specific syntax for multi-line comments, but you can use triple quotes (""" or ''') as a workaround.
"""
This is a
multi-line comment.
"""
print("Python is awesome!")
⚠ Note: Triple quotes are usually used for docstrings (documentation for functions/classes) rather than comments.
2️⃣ Indentation in Python 🔢
Indentation (spaces or tabs at the beginning of a line) is mandatory in Python. It defines code blocks (unlike other languages that use {}).
🔹 Correct Indentation
if True:
print("This is inside an if statement") # Indented correctly
print("This is outside") # Not indented, part of the main program
🔹 Incorrect Indentation (Error)
if True:
print("This will cause an IndentationError") # ❌ No indentation
Error:
makefile
IndentationError: expected an indented block
3️⃣ Indentation in Loops and Functions
🔹 Indentation in Loops
for i in range(3):
print(i) # Inside the loop
print("Loop finished") # Outside the loop
🔹 Indentation in Functions
def greet():
print("Hello, Python!") # Inside function
greet() # Function call
Best Practices for Indentation ✅
✔ Use 4 spaces per indentation level (recommended).
✔ Be consistent—don’t mix spaces and tabs.
✔ Use an IDE (like VS Code or PyCharm) that auto-formats indentation.
Date: 2025-03-22 00:00:00.000000