Core Module
12 min forge
Python Syntax & Indentation
Why indentation matters and Python's philosophy (PEP 8).
Python Syntax & Indentation
π What is it
Python is a high-level, interpreted language known for its readability. Unlike many other languages that use curly braces {} to define blocks of code, Python uses indentation (whitespace).
β‘ Why it's unique
- Indentation: Consistent indentation (usually 4 spaces) is required to define scope.
- Dynamic Typing: Variables don't need explicit declarations of types.
- Interpreted: Code is executed line by line, making debugging easier.
π§ PEP 8
PEP 8 is the official style guide for Python code. Key rules include:
- Use 4 spaces per indentation level.
- Limit all lines to a maximum of 79 characters.
- Use
snake_casefor functions and variables, andPascalCasefor classes.
π» Code example
python Standarddef greet(name): if name: print(f"Hello, {name}!") else: print("Hello, Anonymous!") greet("Developer")
β Common mistakes
- IndentationError: Mixing tabs and spaces (always use 4 spaces).
- Scope confusion: Forgetting that Python uses indentation to define where a loop or function ends.
- Global Variables: Overusing
globalkeywords instead of passing arguments.