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.02. Execution Breakdown
- 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 anifordef) has ended. - Type Hinting: Modern Python (3.12+) uses
: floatand-> floatto provide static analysis hints, though they aren't enforced at runtime by the interpreter. - 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. - 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_casefor functions and variables;PascalCasefor classes;UPPER_SNAKE_CASEfor 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 ExpertData 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
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.