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_case for functions and variables, and PascalCase for classes.

πŸ’» Code example

python Standard
def 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 global keywords instead of passing arguments.