Lesson 3 of 408%

(Progress persistence is disabled until Phase 9)

Python
Beginner

Python Syntax

Understand Python's unique indentation-based syntax and structure.

Python Syntax

What You’ll Learn

You will learn about Python’s unique syntax, particularly its use of indentation to define code blocks.

Indentation is Mandatory

In many programming languages, curly braces {} are used to define blocks of code (like in HTML, CSS, JavaScript). Python uses indentation (whitespace at the beginning of a line) instead.

Basic Example

# Correct Indentation
if 5 > 2:
    print("Five is greater than two!")

If you skip the indentation, Python will throw an error:

# This will cause an IndentationError
if 5 > 2:
print("Five is greater than two!")

The Number of Spaces

The number of spaces is up to you as a programmer, but it must be at least one. The most common standard is 4 spaces. You must use the same number of spaces in the same block of code.

if 5 > 2:
    print("First line")
    print("Second line") # This is fine, both have 4 spaces

Line Endings

Python doesn’t require semicolons ; at the end of statements. A new line indicates the end of a command.

Common Mistakes

  • Mixing tabs and spaces: Pick one (spaces are preferred) and stick to it. Modern editors like VS Code handle this automatically.
  • Inconsistent indentation within the same block.

Lesson Progress

Take QuizBuild a Python Project