Coding/Learn/python/syntax
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnPython Syntax & Indentation
3 / 32 0%
Lesson 03 of 32
6 mins read

Python Syntax & Indentation

PreviousNext Lesson
In Plain English

Most programming languages use curly braces { } to group blocks of code together. Python decided that looks messy and uses clean indentation (usually 4 spaces) instead. Clean spacing is not just cosmetic in Python: it is the law.

The Mental Model

"Think of an outline or a bulleted list. When you indent a bullet point, it belongs to the heading above it. Python works the exact same way with code blocks."

Deep Dive: How It Works

In Python, indentation defines code structure. When you write an if statement, loop, or function, everything indented underneath belongs to that block.

The standard Python convention (PEP 8) is to use 4 spaces per indentation level. In ZenCompiler, pressing the Tab key inserts 4 clean spaces automatically.

Python is also case-sensitive. Variable names like "user", "User", and "USER" are three completely different variables to the interpreter.

Syntax Blueprint

if condition:
    # 4 spaces of indentation
    do_something()

A colon : marks the start of a block, and the indented lines below it belong to that block.

PointCore Rules to Remember

Point star
Indentation is Structure: Spaces determine which lines of code run together in a block.
Point star
Case Sensitive: Print() with a capital P will fail. Built-in functions are always lowercase.
Point star
Colons Start Blocks: Whenever you open a block (if, for, while, def), end the line with a colon :.

Common Beginner Traps & How to Fix Them

Mistake:IndentationError: unexpected indent

Why it happens: Adding random spaces at the start of a normal line where no block was opened.

How to fix: Make sure standard top-level code starts at column 0 with no leading spaces.

Pro Tip: Never mix tabs and spaces in the same file. In modern Python, stick strictly to spaces.

Live Interactive Example

Hit Run Code to see it live
Clean Indentation in Action
Python 3.12
1
2
3
4
5
6
7
Output Console
Click "Run Code" to view the rendered output.
How it works: The first two prints belong to the if block because they are indented. The last print runs unconditionally.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Fix the Indentation

  • PointFix the broken code so the greeting prints properly inside the block.
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates