Python · Chapter 3 of 45

Python Syntax

Python uses indentation to group code instead of braces `{}`. Every statement in the same block must start at the same column — usually 4 spaces.

Statements normally end at the newline. Semicolons exist but are almost never used.

Indentation defines blocks

Everything inside an `if`, loop or function is indented. If you mix tabs and spaces you'll get an IndentationError.

Case sensitivity

Python is case-sensitive: `Name` and `name` are different variables. Keywords like `if`, `for`, `while` are always lowercase.

Example 1 (python)
if 10 > 5:
    print("Ten is greater")
Output
Ten is greater

The indented print() belongs to the if-block.

Example 2 (python)
for i in range(3):
    print(i)
Output
0
1
2

The body of the loop is indented under `for`.

Key points

  • Indentation (usually 4 spaces) defines code blocks.
  • Mixing tabs and spaces breaks the parser.
  • Statements end at the newline, not with `;`.
  • Python is case-sensitive.
💡 Note: PEP 8, the official style guide, recommends 4 spaces per indent — never tabs.

📝 Quick Quiz

1. Python uses ____ to define code blocks:

2. PEP 8 recommends how many spaces per indent?

3. Are `Name` and `name` the same variable?