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?
Question 2: What is the standard number of spaces recommended for a Python indentation level?
Question 3: Which symbol is used to start a single-line comment in Python?
Question 4: How can you write a multiline comment block in Python?
Question 5: What error is raised when code inside a block scope is not indented?
Question 6: Do you need a semicolon to complete a statement in Python?
Question 7: What does the Python interpreter do when it encounters a comment?
Question 8: Which of these block definitions is syntactically invalid?
Question 9: Can comments be placed on the same line as a code statement?
Question 10: What is the output of: # print('Hello')?
๐ฅ๏ธ Output Prediction Challenges
Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.
Predict the output of the code snippet below. Type the exact output in the console.
if 10 > 5:
print("Yes")
Predict the output of the code snippet below. Type the exact output in the console.
# print("Hello")
print("World")
Predict the output of the code snippet below. Type the exact output in the console.
"""
print("Skip me")
"""
print("Run me")
Predict the output of the code snippet below. Type the exact output in the console.
if True:
# print("A")
print("B")
Predict the output of the code snippet below. Type the exact output in the console.
print("Line 1")
# print("Line 2")
print("Line 3")
๐ฎ Interactive Code Playground
Type your own Python code below and click "Run Code" to execute it instantly in your browser!
Console ready.
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
๐ Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.