Last updated

Python Syntax

Definition: "Syntax" is the set of rules for how code must be written. Python's most distinctive rule is indentation — the spaces at the start of a line group code together.

Most languages use curly braces { } to group code. Python uses spacing instead, and it is required — not just for neatness.

Example 1 — indentation defines a block

if 5 > 2:
    print("Five is greater than two")
    print("This line is also inside the if")

The four spaces tell Python both print lines belong to the if. Removing them causes an IndentationError.

Example 2 — consistency matters

Every line in the same block must use the same indentation. This is wrong and would error:

if True:
    print("two spaces here")
      print("six spaces here -- error!")

Example 3 — one statement per line

Python normally puts one instruction on each line, which keeps code easy to read:

name = "Sam"
age = 20
print(name)
print(age)

💡 Standard: use 4 spaces per level of indentation. In the editor, the Tab key inserts spaces for you.

Try it Yourself
Output

          
Ad · responsive