Lesson explanation
After this lesson, you will be able to:
pass for an intentionally empty block and initialize values safely when they are needed later.Python uses indentation as syntax. A colon opens a block, and the indented statements below it belong to that block:
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.
Both indented lines belong to the successful branch.
checks_passed = True
if checks_passed:
print("Validation complete")
print("Release unlocked")
print("Workflow finished")Output:
Validation complete
Release unlocked
Workflow finishedThe final line is not indented, so it runs after the condition.
The second condition is checked only when the first one passes.
account_exists = True
email_verified = True
if account_exists:
if email_verified:
print("Login allowed")Output:
Login allowedThe inner print() is two indentation levels deep.
passpass does nothing, but it satisfies Python's requirement that a block contain a statement.
feature_enabled = False
if feature_enabled:
pass
print("Configuration loaded")Output:
Configuration loadedCreate a safe default before the branch when the value is printed later.
is_enterprise = False
support_label = "Standard support"
if is_enterprise:
support_label = "Priority support"
print(support_label)Output:
Standard supportThe variable always exists, even when the branch does not run.
pass is required.Python for Developers
Assembled code
Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.
0 / 5 tasks completed
| Structure | Meaning |
|---|
if condition: | Open a block |
| Four leading spaces | One indentation level |
| Eight leading spaces | A block nested inside another block |
pass | Valid placeholder with no behavior |
| Move indentation left | Close the current block |