Python Tutorial 0/52 lessons ~6 min read Lesson 5

    Syntax & Indentation

    Python uses indentation, not braces, to define code blocks.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Python uses indentation, not braces, to define code blocks. This forces consistent formatting and is one of the most distinctive features of the language.

    Understanding the topic

    Core concepts to understand:

    • 4 spaces per indent (PEP 8).
    • Colons start blocks: if, for, def, class.
    • Comments use #; docstrings use """...""".
    • Statements end at line break — no semicolons.

    Syntax reference

    Visual flow / code:

    python
    def factorial(n: int) -> int:
    """Return n! using recursion."""
    if n <= 1:
    return 1
    return n * factorial(n - 1)
    # Multi-line: use parens or backslash
    total = (
    1 + 2 + 3
    + 4 + 5
    )

    Execution workflow

    1Python Syntax & Indentation Workflow
    1 / 4

    Step 1

    4 spaces per indent (PEP 8).

    Apply this step while implementing python syntax & indentation in real code.

    Real-world use

    Indentation-based syntax is divisive but enforces readable code — there's only one way to format a block, which kills bikeshedding in code reviews.

    Best practices

    • Always 4 spaces, never tabs.
    • Configure your editor for PEP 8.
    • Add docstrings to public functions.

    Common mistakes

    • Mixing tabs and spaces — Python 3 errors out.
    • Inconsistent indent depth.

    Hands-on exercise

    Interview preparation — practice these questions:

    • Why does Python use indentation?
    • What's PEP 8?
    • Tabs vs spaces?

    Summary

    In summary: Indentation defines structure. PEP 8 = community style law.

    Ready to mark this lesson complete?Track your journey across the entire course.