Concepts / Python Syntax Basics

Python Syntax Basics

Python is designed to have an unusually simple, easy-to-read syntax that lets a learner focus on solving the problem rather than fighting the language's own rules.

  • CORE CONCEPT
  • Programming

Why Python Chose Indentation

Most programming languages use special characters—braces, brackets, or keywords—to mark where a block of code begins and ends. Python takes a radically different approach: it uses indentation, the blank space at the start of a line, as the actual syntax that tells Python which statements belong together. This is not a style preference. It is not optional formatting. Indentation is how Python knows which lines are part of the same block.

Python was designed this way deliberately. The language's philosophy prioritizes readability and simplicity so that you can focus on solving the problem rather than fighting the language's own rules. Because indentation is required for the code to work, every Python file you read will have consistent, visible structure. There is no way to write sloppy, hard-to-read Python and have it still work—the syntax itself enforces clarity.

How Indentation Groups Statements

When you write a statement that requires a block of code to follow—such as an if condition, a loop, or a function definition—Python expects the next line to be indented. All lines at that same indentation level belong to the same block. When the indentation returns to the previous level, the block ends.

if x > 5:Block starts hereprint('x is large')Indented: part of the ifblocky = x * 2Indented: part of the ifblockprint(y)No indent: block ended,runs always
What does indentation do? Each indented line belongs to the block that started with the colon. When indentation returns to the left, the block ends.

In the example above, the if statement on line 1 ends with a colon. This colon tells Python to expect an indented block next. Lines 2 and 3 are indented by four spaces, so they belong to the if block and only execute if the condition is true. Line 4 has no indentation, so it is not part of the if block and always executes, regardless of the condition.

The Four-Space Standard

The official Python recommendation, documented in the style guide known as PEP 8, is to indent consistently using four spaces per level. This means each nested block should be indented four more spaces than the block that contains it.

Use four spaces for each level of indentation. Do not use tabs, and do not mix tabs and spaces in the same file.

Why four spaces and not two, or eight, or tabs? Four spaces is a compromise: it is enough to be visually clear on any screen, but not so much that deeply nested code becomes hard to read. Tabs can look different depending on your editor settings, which causes problems when code is shared. By standardizing on spaces, Python ensures that code looks the same everywhere.

Common Indentation Errors

Indentation errors are among the first errors beginners encounter in Python. They happen because Python is strict: even a single extra space at the start of a line can break your code. The most common mistakes are mixing tabs and spaces, indenting when you should not, and forgetting to indent when you should.

  • Mixing tabs and spaces in the same file

    Python treats tabs and spaces as different characters. Even though they may look the same on screen, Python sees them as inconsistent indentation and raises an IndentationError.

    Fix: Choose one method (spaces) and use it consistently throughout the entire file. Most modern editors can be configured to insert spaces when you press Tab.

  • Indenting a line that should not be indented

    Python will treat that line as part of the if block, so it will only execute if the condition is true. The code runs, but not as intended.

    Fix: Return to the previous indentation level. Use your editor's visual guides or count spaces carefully.

  • Forgetting to indent after a colon

    Python expects an indented block after a colon. Without it, Python raises an IndentationError: expected an indented block.

    Fix: Indent the line after the colon by four spaces.

  • Inconsistent indentation depth across similar blocks

    Python will treat them as different nesting levels, which is almost never what you intend. The code may run but behave unexpectedly.

    Fix: Use exactly four spaces per level, every time.

if x > 5:if x > 5:print('large')Indented with TABprint('large')Indented with 4 SPACESy = x * 2Indented with SPACESy = x * 2Indented with 4 SPACES
What does inconsistent indentation look like, and why does Python reject it? The left side shows mixed tabs and spaces; the right side shows the corrected version using only spaces.

When Python encounters mixed tabs and spaces, it raises an error message like: File 'whitespace.py', line 5 ... IndentationError: unexpected indent. This error tells you that Python found an indentation it did not expect. The fix is to go back and make all indentation consistent using spaces only.

Nested Blocks and Indentation Depth

Code can have multiple levels of nesting. A loop inside an if statement, a function inside a class, or an if statement inside a loop all require additional indentation. Each level adds four more spaces. Python has no hard limit on nesting depth, but deeply nested code becomes hard to read and is usually a sign that your code could be simplified.

containscontainscontainsif condition:0 spaces (top level)for item in list:4 spaces (nested in if)if item > 10:8 spaces (nested in for)print(item)12 spaces (nested in if)
How many levels deep can I indent, and how do I know when to stop? Each level of nesting adds four more spaces. The diagram shows the hierarchy of indentation levels.

In the example above, the if statement is at the top level with zero indentation. The for loop inside it is indented by four spaces. The if statement inside the for loop is indented by eight spaces. The print statement inside that if is indented by twelve spaces. Each level is exactly four spaces deeper than the level that contains it.

Indentation in Practice

Checking a Grade with Proper Indentation

Write a program that checks if a student's grade is passing (70 or higher) and prints a message. If the grade is 90 or higher, also print a bonus message.

Start with the outer if: Write the first if statement that checks if the grade is 70 or higher. End it with a colon. This is the top level, so no indentation.

Indent the first block: The line that prints 'Passing grade' should be indented by four spaces because it belongs to the first if block.

Add the nested if: Inside the first if block, add another if statement that checks if the grade is 90 or higher. This line is also indented by four spaces because it is part of the first if block.

Indent the nested block: The line that prints 'Excellent work' should be indented by eight spaces because it belongs to the second if block, which is nested inside the first.

Return to the top level: Any code after both if blocks should return to zero indentation. This code runs regardless of the grade.

The structure shows three indentation levels: top level (if grade >= 70), first nested level (print and second if), and second nested level (print inside second if). Each level is exactly four spaces deeper than the one before it.

What do you think happens?

Look at this code structure. Which lines belong to the outer if block, and which belong to the inner if block?

  • Lines 2-3 belong to the outer if; line 4 belongs to the inner if
  • Lines 2-4 all belong to the outer if; line 5 belongs to the inner if
  • Lines 2-4 all belong to the outer if; line 4 also belongs to the inner if
Reveal answer

Answer: Lines 2-4 all belong to the outer if; line 4 also belongs to the inner if

Lines 2 and 3 are indented by four spaces, so they are part of the outer if block. Line 4 is the inner if statement, also indented by four spaces, so it is part of the outer if block. Line 5, indented by eight spaces, belongs to the inner if block. A line can belong to multiple blocks if it is nested—line 4 is both part of the outer if and the start of the inner if.

Why This Matters

Python's use of indentation as syntax is unusual among programming languages. Languages like C, Java, and JavaScript use braces to mark blocks, which means indentation is optional—you can write messy code and it still works. Python forces you to indent correctly, which means every Python file you read will have clear, consistent structure. This design choice makes Python easier to learn and easier to read, especially for beginners.

Understanding indentation is not just about avoiding errors. It is about understanding how Python organizes code into logical blocks. When you see indentation, you immediately know which statements are grouped together and which are separate. This visual clarity is one of the reasons Python is considered one of the most readable programming languages.

Practice

EASY

Write a simple program with an if statement and a nested loop. Make sure to indent the loop by four spaces and any code inside the loop by eight spaces. Check that every line has the correct indentation by counting spaces carefully. Run the program and verify that it works without an IndentationError.

Hints
  • Remember: the if statement ends with a colon and has zero indentation.
  • The for loop inside the if should be indented by four spaces.
  • Any code inside the for loop should be indented by eight spaces.
  • If you get an IndentationError, check that you are using spaces, not tabs, and that every level is exactly four spaces deeper than the level before it.

Key Takeaways

  • Indentation in Python is not optional formatting—it is meaningful syntax that determines which statements belong to the same code block.
  • Use exactly four spaces per indentation level, and never mix tabs and spaces in the same file.
  • A colon at the end of a line signals that an indented block follows. When indentation returns to the previous level, the block ends.
  • Common indentation errors include mixing tabs and spaces, indenting when you should not, and forgetting to indent after a colon. Python's strict indentation rules prevent these mistakes from going unnoticed.
  • Python's design choice to use indentation as syntax makes code more readable and forces consistency, which is especially valuable when learning to program.