โ† Back to Hub
Module 1 โ€ข Week 2 โ€ข Indentation & Comments Scoping

Python Syntax & Comments

Understand Python indentation scope rules, code commenting using #, multiline comments, and run basic statements.

1. Indentation defines Scope

Unlike other programming languages (like Java or C++) that use curly brackets { } to group blocks of code, Python relies entirely on **indentation (spaces)**. The standard spacing is **4 spaces** (or one Tab press). Failing to indent correctly yields an IndentationError!

2. How Python Executed Code

Python runs from top to bottom. Semicolons are not required to close commandsโ€”each new line marks the completion of a statement.

3. Comments in Python

Comments are notes written to explain the code. They are ignored by the Python interpreter. We use:

  • Single line comments: Start with the hash symbol #.
  • Multiline comment blocks: Created using triple quotes """ or '''. While these are technically multiline strings, Python ignores them if they are not assigned to a variable, acting as a block comment!

๐Ÿ’ป Python Code Examples

Example 1: Valid Indentation Block

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

Description: Indents the print statement by 4 spaces to group it inside the if block.

Example 2: Single Line Comments

# This is a comment line
print("Hello World") # Inline comment

Description: Lines and text starting with # are ignored during execution.

Example 3: Multiline Block Comments

"""
This is a multiline comment block.
Python ignores this block.
"""
print("Code runs!")

Description: Uses triple quotes to write multi-line developer comments.

Example 4: Invalid Indentation (Causes Error)

if 5 > 2:
print("No indentation!") # Triggers IndentationError

Description: Omitting indentation inside block scopes causes Python to halt with an error.

Example 5: Nested Indentation Blocks

if 5 > 2:
    print("Level 1 indentation")
    if 3 > 1:
        print("Level 2 indentation")

Description: Indents subsequent code blocks by an additional 4 spaces to denote nesting.

Example 6: Commands on Newlines

print("Statement 1")
print("Statement 2")

Description: Commands are terminated by newlines, with no semicolons needed.

๐Ÿง  Active Retrieval Quizzes

Answer the following 10 multiple-choice questions to test your storage strength.

Question 1: What determines scope and code blocks in Python?

Explanation: Python relies entirely on whitespace indentation to define code blocks.

Question 2: What is the standard number of spaces recommended for a Python indentation level?

Explanation: The official PEP 8 style guide recommends exactly 4 spaces per indentation level.

Question 3: Which symbol is used to start a single-line comment in Python?

Explanation: The hash symbol # marks the beginning of a single-line comment in Python.

Question 4: How can you write a multiline comment block in Python?

Explanation: Triple quotes are multiline strings that function as block comments when not assigned to variables.

Question 5: What error is raised when code inside a block scope is not indented?

Explanation: Python raises an IndentationError when indentation levels do not match scope expectations.

Question 6: Do you need a semicolon to complete a statement in Python?

Explanation: Python completes statements using new lines, making semicolons unnecessary.

Question 7: What does the Python interpreter do when it encounters a comment?

Explanation: Comments are ignored by the Python interpreter during code execution.

Question 8: Which of these block definitions is syntactically invalid?

Explanation: The third option is invalid because it has zero indentation after the colon.

Question 9: Can comments be placed on the same line as a code statement?

Explanation: Comments can be placed inline at the end of a statement after a # symbol.

Question 10: What is the output of: # print('Hello')?

Explanation: Since the print function is commented out, it is ignored and generates no output.

๐Ÿ–ฅ๏ธ Output Prediction Challenges

Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.

Challenge 1 of 5

Predict the output of the code snippet below. Type the exact output in the console.

if 10 > 5:
    print("Yes")
Console ready. Type your prediction below...
Challenge 2 of 5

Predict the output of the code snippet below. Type the exact output in the console.

# print("Hello")
print("World")
Console ready. Type your prediction below...
Challenge 3 of 5

Predict the output of the code snippet below. Type the exact output in the console.

"""
print("Skip me")
"""
print("Run me")
Console ready. Type your prediction below...
Challenge 4 of 5

Predict the output of the code snippet below. Type the exact output in the console.

if True:
    # print("A")
    print("B")
Console ready. Type your prediction below...
Challenge 5 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print("Line 1")
# print("Line 2")
print("Line 3")
Console ready. Type your prediction below...

๐ŸŽฎ Interactive Code Playground

Type your own Python code below and click "Run Code" to execute it instantly in your browser!

๐Ÿ“ Python Code Editor
Python WebAssembly runtime is unloaded. Click 'Run Code' to load.
๐Ÿ–ฅ๏ธ Output Console
Console ready.
๐Ÿ’ฌ Teacher Consultation

Stuck on any concept, or want to explore an edge case? Don't hesitate to ask follow-up questions to your instructor! Active questioning helps build storage strength.

๐Ÿ“‹ Progress Status

Lesson Complete?

๐Ÿ“Œ Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.