Hintor
Python Basics for Programmers

Python Blocks: Indentation and Colons

Python for Developers/Python Basics for Programmers/

Python Blocks: Indentation and Colons

Python

Lesson explanation

Python Blocks: Indentation and Colons


🎯 Learning Objective

After this lesson, you will be able to:

  • Open Python blocks with a colon and use consistent indentation.
  • Recognize which statements belong to a branch from their indentation level.
  • Indent nested blocks without relying on braces.
  • Use pass for an intentionally empty block and initialize values safely when they are needed later.

Short Explanation

Python uses indentation as syntax. A colon opens a block, and the indented statements below it belong to that block:

Python
if is_ready:
    print("Deploy")

Four spaces per indentation level is the usual convention. Python does not use {} to mark blocks, and inconsistent indentation can change the program or stop it from running.

Nested decisions add another indentation level. When the inner block ends, move back to the outer level.

Python also rejects an empty block. Use pass when you need a legal placeholder without behavior.

An if block does not create a separate variable scope, but that does not guarantee an assignment happened. If a value must exist after the block, initialize it before the condition or assign it in every possible branch.

💡 Tip: Read indentation as structure: same level means same block; deeper means nested; moving left closes a block.


Clean Code Examples

1. Read block membership from indentation

Both indented lines belong to the successful branch.

Python
checks_passed = True

if checks_passed:
    print("Validation complete")
    print("Release unlocked")

print("Workflow finished")

Output:

TEXT
Validation complete
Release unlocked
Workflow finished

The final line is not indented, so it runs after the condition.

2. Indent a nested decision

The second condition is checked only when the first one passes.

Python
account_exists = True
email_verified = True

if account_exists:
    if email_verified:
        print("Login allowed")

Output:

TEXT
Login allowed

The inner print() is two indentation levels deep.

3. Keep an empty block valid with pass

pass does nothing, but it satisfies Python's requirement that a block contain a statement.

Python
feature_enabled = False

if feature_enabled:
    pass

print("Configuration loaded")

Output:

TEXT
Configuration loaded

4. Initialize a value before a conditional update

Create a safe default before the branch when the value is printed later.

Python
is_enterprise = False
support_label = "Standard support"

if is_enterprise:
    support_label = "Priority support"

print(support_label)

Output:

TEXT
Standard support

The variable always exists, even when the branch does not run.



⚠️ Common Mistakes

  1. Do not use braces instead of indentation.
  2. Do not align a nested statement with its parent branch.
  3. Do not leave a block empty when pass is required.
  4. Do not assign a value only inside a branch when later code needs it regardless of the branch result.
Start Practice

Python for Developers

Python Blocks: Indentation and Colons

5 tasks
Lesson progress0 / 5 tasks completed

Tasks

Practice progress

Complete a placeholder block

View task

Complete the placeholder branch so the script remains valid and reaches the final message.

Requirements

  • Fill the missing block punctuation and placeholder statement.
  • Keep the existing indentation.

Expected output

Service configuration loaded
Code: PythonTask 1

Complete a placeholder block

Change the starter code or fill in the blanks, then run it before asking Hintor.

1
2
3
4
5
6
7

Assembled code

Preview of the code that will run

Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.

Output / ConsolePreparing Python runtime...

0 / 5 tasks completed

Previous lessonBack to unitBack to track
StructureMeaning
if condition:Open a block
Four leading spacesOne indentation level
Eight leading spacesA block nested inside another block
passValid placeholder with no behavior
Move indentation leftClose the current block