Python Basics — Syntax & Indentation
Syntax & Indentation
In most programming languages, code blocks are defined by curly braces {}. Python is different — it uses indentation (whitespace at the beginning of a line).
This makes Python code clean and readable, but it also means you need to be precise about spacing.
Indentation Rules
The Basics
Every code block (after if, for, while, def, class, etc.) must be indented:
if True:
print("This is indented") # 4 spaces
print("This is also inside") # 4 spaces
print("This is outside") # No indentation
Output:
This is indented
This is also inside
This is outside
The Standard: 4 Spaces
Python accepts any consistent indentation, but the community standard is 4 spaces per level:
# Good — 4 spaces (PEP 8 standard)
if age >= 18:
print("Adult")
# Works but NOT recommended — 2 spaces
if age >= 18:
print("Adult")
# Works but NOT recommended — tab
if age >= 18:
print("Adult")
Rule: Pick one style (4 spaces) and use it everywhere. Never mix tabs and spaces — Python will throw an error.
Nested Indentation
Each nested level adds another 4 spaces:
if score >= 90:
print("Grade: A") # 4 spaces (level 1)
if score == 100:
print("Perfect score!") # 8 spaces (level 2)
Output (if score is 100):
Grade: A
Perfect score!
The Colon Rule
Every statement that starts a code block ends with a colon ::
if condition: # colon required
do_something()
for item in list: # colon required
process(item)
def my_function(): # colon required
return result
class MyClass: # colon required
pass
Forgetting the colon is one of the most common beginner errors.
IndentationError
Python will immediately tell you if your indentation is wrong:
if True:
print("Oops!") # Not indented!
Error:
IndentationError: expected an indented block after 'if' statement on line 1
Common Indentation Mistakes
| Mistake | Example | Fix |
|---|---|---|
| No indentation | if True:\nprint("x") |
Add 4 spaces before print |
| Inconsistent spaces | Mixing 2 and 4 spaces | Use 4 spaces everywhere |
| Mixing tabs and spaces | Using both in the same file | Use spaces only |
| Extra indentation | Indenting when not needed | Remove unnecessary indentation |
Line Continuation
Python statements usually end at the end of a line. For long lines, you have two options:
Option 1: Backslash
total = 100 + 200 + 300 + \
400 + 500 + 600
print(total)
Output:
2100
Option 2: Parentheses (Preferred)
total = (100 + 200 + 300 +
400 + 500 + 600)
print(total)
Output:
2100
Parentheses are preferred because they’re cleaner and don’t require the backslash.
Long Strings
Triple quotes span multiple lines naturally:
message = """This is a
very long message that
spans multiple lines."""
print(message)
Output:
This is a
very long message that
spans multiple lines.
Multiple Statements on One Line
You can put multiple statements on one line using semicolons, but you shouldn’t:
# Legal but ugly — don't do this
x = 1; y = 2; z = 3
# Better — one statement per line
x = 1
y = 2
z = 3
The Zen of Python says: “Readability counts.”
Blank Lines
Use blank lines to separate logical sections of code:
# Section 1: Setup
name = "Alice"
age = 25
# Section 2: Processing
if age >= 18:
status = "adult"
else:
status = "minor"
# Section 3: Output
print(f"{name} is a {status}")
Guidelines:
- Two blank lines before and after function/class definitions
- One blank line to separate logical sections within a function
Summary
- Python uses indentation (4 spaces) to define code blocks
- Never mix tabs and spaces
- Code blocks start after a colon
: - Use parentheses for line continuation (preferred over backslash)
- One statement per line — avoid semicolons
- Use blank lines to improve readability
Next up: Keywords & Identifiers →