Python Syntax

Indentation, statements and line structure.

✍️ 3 exercises + code challenge in this lesson

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.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services