The Philosophy of Python Syntax
Python's syntax is famously clean and minimalist. Unlike languages that use curly braces {} or semicolons ; to define structure, Python relies on indentation. This isn't just a style choice—it is a requirement of the language that enforces readable code.
- Indentation is Mandatory
In Python, a block of code (like the body of a loop or function) starts with a colon : and must be indented. While you can use any number of spaces, the standard (PEP 8) is 4 spaces per level.
- Comments & Docstrings
Comments are essential for documenting logic. Python supports single-line comments and multi-line documentation strings (docstrings).
- Single-line: Use the hash
#symbol. - Multi-line / Docstrings: Use triple quotes
"""or'''. These are often used at the start of functions to explain their purpose.
- Statement Termination
In Python, the end of a line marks the end of a statement. You don't need semicolons. If a statement is very long, you can use a backslash \ to continue it on the next line, though surrounding the code in parentheses () is the preferred way.
Syntax Comparison
| Feature | Dart / JavaScript | Python |
|---|---|---|
| Code Blocks | Curly braces { } | Colon : and Indentation |
| Statement End | Semicolon ; | New line |
| Main Function | void main() { } | if __name__ == "__main__": |
| Comments | // or /* */ | # or """ """ |