Python Tutorials

Python Comments and Indentation

When writing code in Python, making it work is only half the job. Making it readable and well-structured is equally important. Python uses two primary concepts to achieve this: Comments for documenting the code and Indentation for defining code blocks.

Let us understand both concepts in detail.

Python Comments

Comments are lines of text inside a Python program that are completely ignored by the Python interpreter during execution. They are written in plain English to explain what a specific block of code does.

Why Use Comments?

  • Readability: Helps other developers (or your future self) understand the logic behind the code.
  • Testing: You can comment out a specific line of code to prevent it from running while debugging, without actually deleting it.
  • Maintenance: Makes large software projects easier to maintain and update.

Types of Comments in Python

There are two ways to write comments in Python:

1. Single-Line Comments

A single-line comment starts with a hash symbol (#). Anything written after the # on that specific line is ignored by Python.

# This is a single-line comment
print("Welcome to Python")  # This comment is placed after a statement
Code language: PHP (php)

Output:

Welcome to Python

2. Multi-Line Comments

Unlike languages like C++ or Java, which use /* ... */ for multi-line comments, Python does not have a specific syntax for them. However, you can achieve multi-line comments in two ways:

Method A: Using multiple hash symbols

# This is a multi-line comment.
# We are using multiple hash symbols
# to span across multiple lines.
print("Hello, Geek")
Code language: PHP (php)

Method B: Using Triple Quotes (String Literals) If a multi-line string is not assigned to any variable, the Python interpreter ignores it. We can use triple single quotes ''' or triple double quotes """ for this.

"""
This is another way to write
multi-line comments in Python.
It is widely used by developers.
"""
print("Learning Comments")
Code language: PHP (php)

Python Indentation

In most programming languages like C, C++, and Java, curly braces {} are used to define a block of code (like the body of a loop or a function).

Python takes a different approach. Python uses Indentation (whitespaces or tabs at the beginning of a line) to define a block of code.

How Indentation Works

Statements that are indented to the same level are considered part of the same code block.

age = 20

# This is the main block
if age >= 18:
    # This is an indented block inside the if statement
    print("You are an adult.")
    print("You can vote.")

# This is back to the main block
print("Program ended.")
Code language: PHP (php)

Output:

You are an adult.
You can vote.
Program ended.

In the example above, the two print statements inside the if condition are indented. They will only execute if the condition is true.

Indentation Rules and Best Practices

To avoid errors and maintain clean code, follow these standard practices:

  1. Use 4 Spaces: The official Python style guide (PEP 8) strictly recommends using 4 spaces for each level of indentation.
  2. Be Consistent: Never mix spaces and tabs in the same Python file. Doing so will cause an error.
  3. Automatic Indentation: Modern IDEs like VS Code and PyCharm automatically handle indentation when you press the Enter key after a colon (:).

IndentationError in Python

If you skip indentation where it is required, or if the number of spaces is inconsistent, Python will throw an IndentationError and stop executing.

Incorrect Code:

if 10 > 5:
print("10 is greater than 5") # Missing indentation
Code language: PHP (php)

Output:

IndentationError: expected an indented block after 'if' statement on line 1
Code language: JavaScript (javascript)

Correction: Always add spaces (preferably 4) before the statement inside the block.

if 10 > 5:
    print("10 is greater than 5")
Code language: PHP (php)
Tags: #IndentationError #Learn Python #PEP 8 #Python #Python Basics #Python Code Structure #Python Comments #Python Indentation #Python Programming #Python Tutorial

Leave a Comment