Python uses indentation (spaces at the start of a line) to define blocks of code. Other languages use curly braces; in Python, indentation is required.
Use 4 spaces per indentation level.
All lines in the same block must have the same indentation.
A colon : starts a new block (after if, for, def...).
No semicolons are needed at the end of lines.
Wrong indentation causes an IndentationError. Try removing the spaces before the second print and press Run.
💡 Key Takeaways: Python Syntax
Mastering Python Syntax helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
Python Syntax Code Example
Python
if 5 > 2:
print("Five is greater than two!")
print("This line is in the same block.")
print("This line is outside the block.")
Free sign-in required · Google login available
Python Syntax Practice Exercises
Check what you learned in “Python Syntax”.
/ 3 solved
Fill in the blank
Python uses this character to start a block after if, for, def...
if 5 > 2
print("Five is greater")
✓ Correct!Not quite. Try again.
Multiple choice
What defines a code block in Python?
✓ Correct!Not quite. Try again.
Code challenge
Fix the indentation so the program prints "Inside" and then "Outside".
Write the code in the live editor. Your answer is checked automatically.