python icon

Indentation, Comments & PEP 8

Expert Answer & Key Takeaways

Mastering Indentation, Comments & PEP 8 is essential for high-fidelity technical performance and advanced exam competency in 2026.

Python Basic Syntax: Indentation, Comments & PEP 8

Python syntax is defined by its use of whitespace for block scoping and a strict adherence to the PEP 8 style guide for professional readability.

1. The Proof Code

import sys def calculate_discount(price: float, discount: float) -> float: """ Calculate the final price after discount. Follows PEP 8: 4-space indent, snake_case naming. """ if price < 0 or discount < 0: # ❌ Avoid: Raising generic Exception # ✅ Correct: Raising specific ValueError raise ValueError("Price and discount must be positive") final_price: float = price * (1 - discount) return round(final_price, 2) if __name__ == "__main__": result = calculate_discount(100.0, 0.15) print(f"Final Price: ${result}") # Output: Final Price: $85.0

2. Execution Breakdown

  1. Significant Whitespace: Unlike C-style languages that use {}, Python uses indentation levels to define scope. A change in indentation tells the parser that a block (like an if or def) has ended.
  2. Type Hinting: Modern Python (3.12+) uses : float and -> float to provide static analysis hints, though they aren't enforced at runtime by the interpreter.
  3. Docstrings: The triple-quoted string below the function definition is a 'Docstring'. It is stored in the __doc__ attribute of the function object and used for auto-documentation.
  4. Module Guard: The if __name__ == "__main__": block ensures that the testing code only runs when the script is executed directly, not when imported as a module.

3. Detailed Theory

The philosophy of Python syntax is captured in PEP 20 (The Zen of Python): 'Readability counts.'

The PEP 8 Standard

  • Indentation: Exactly 4 spaces per level. Never use tabs (though modern editors convert tabs to spaces automatically).
  • Line Length: Limit all lines to a maximum of 79 characters for better side-by-side code review.
  • Naming: snake_case for functions and variables; PascalCase for classes; UPPER_SNAKE_CASE for constants.

Comments vs. Docstrings

  • Comments (#): Used for explaining why a specific piece of logic exists for other developers.
  • Docstrings: Used for explaining what a function or class does and how to use it. These are accessible via the help() function.
[!TIP] Senior Secret: Use the Ruff linter in your CI/CD pipeline. It enforces PEP 8 and checks for 'code smells' (like mutable default arguments) with zero configuration, making your codebase 'Lead Engineer' ready instantly.

Top Interview Questions

?Interview Question

Q:What happens if you mix tabs and spaces for indentation in Python 3?
A:
Python 3 will raise a TabError. Mixing them is strictly prohibited because it leads to invisible scoping bugs that are extremely hard to debug.

?Interview Question

Q:Why is the 'if __name__ == "__main__":' block considered a best practice?
A:
It prevents code from executing unintentionally when a file is imported as a module into another script. This makes your files 'reusable' as both scripts and libraries.

Course4All Engineering Team

Verified Expert

Data Science & Backend Engineers

The Python curriculum is designed by backend specialists and data engineers to cover everything from basic logic to advanced automation and API design.

Pattern: 2026 Ready
Updated: Weekly