Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of indentation in Python?
Indentation in Python shows which lines of code belong together in a block. It helps the computer understand the structure and flow of the program.
Click to reveal answer
beginner
How many spaces are recommended for each indentation level in Python?
The recommended number of spaces for each indentation level is 4 spaces. This keeps code clear and consistent.
Click to reveal answer
intermediate
What happens if you mix tabs and spaces in Python indentation?
Mixing tabs and spaces can cause errors or unexpected behavior because Python treats them differently. It's best to use only spaces or only tabs consistently.
Click to reveal answer
beginner
Which Python keywords introduce a new block that requires indentation?
Keywords like if, for, while, def, and class start new blocks that must be indented.
Click to reveal answer
beginner
What error do you get if indentation is incorrect in Python?
Python raises an IndentationError when the indentation is wrong or inconsistent.
Click to reveal answer
What does indentation in Python indicate?
AWhich lines belong to the same block
BThe color of the code
CThe speed of the program
DThe size of variables
✗ Incorrect
Indentation shows which lines of code are grouped together in a block.
Which of these keywords starts a new indented block in Python?
Aif
Breturn
Cprint
Dimport
✗ Incorrect
The 'if' keyword starts a new block that requires indentation.
What is the recommended number of spaces for one indentation level?
A3 spaces
B4 spaces
C2 spaces
D1 space
✗ Incorrect
Python style guides recommend 4 spaces per indentation level.
What error occurs if indentation is inconsistent?
ASyntaxError
BTypeError
CIndentationError
DNameError
✗ Incorrect
Python raises an IndentationError when indentation is wrong.
Why should you avoid mixing tabs and spaces?
AIt makes code run faster
BIt improves readability
CIt changes variable names
DIt causes errors or confusion
✗ Incorrect
Mixing tabs and spaces can cause errors because Python treats them differently.
Explain why indentation is important in Python and how it affects the program flow.
Think about how Python knows which lines belong together.
You got /4 concepts.
Describe the best practices for indentation in Python.
Consider style and avoiding errors.
You got /4 concepts.
Practice
(1/5)
1. What does indentation in Python indicate?
easy
A. Which lines of code belong together in a block
B. The data type of a variable
C. The speed of code execution
D. The name of a function
Solution
Step 1: Understand Python's use of indentation
Python uses indentation to group lines of code that belong to the same block, like inside a function or loop.
Step 2: Identify what indentation shows
Indentation shows which lines are connected logically and executed together.
Final Answer:
Which lines of code belong together in a block -> Option A
Quick Check:
Indentation = Code blocks [OK]
Hint: Indentation groups code lines into blocks [OK]
Common Mistakes:
Thinking indentation shows variable types
Confusing indentation with execution speed
Assuming indentation names functions
2. Which of the following code snippets has correct indentation in Python?
easy
A.
def greet():
print('Hello')
print('World')
B.
def greet():
print('Hello')
print('World')
C.
def greet():
print('Hello')
print('World')
D.
def greet():
print('Hello')
print('World')
Solution
Step 1: Check indentation inside the function
All lines inside the function must be indented the same amount to belong to the function block.
Step 2: Identify the snippet with consistent indentation
def greet():
print('Hello')
print('World')
has both print statements indented equally under the function, which is correct.
Final Answer:
def greet():\n print('Hello')\n print('World') -> Option D
Quick Check:
Consistent indentation inside function = Correct [OK]
Hint: Indent all block lines equally under the header [OK]
Common Mistakes:
Mixing indentation levels inside a block
Not indenting any lines inside a function
Indenting only some lines inconsistently
3. What will be the output of this code?
for i in range(2):
print('Start')
if i == 0:
print('First loop')
print('End')
medium
A. Start\nFirst loop\nEnd\nStart\nEnd
B. Start\nEnd\nStart\nFirst loop\nEnd
C. Start\nFirst loop\nEnd\nStart\nFirst loop\nEnd
D. Start\nEnd\nStart\nEnd
Solution
Step 1: Trace the loop iterations
The loop runs twice: i=0 and i=1.
Step 2: Check prints for each iteration
For i=0: prints 'Start', then 'First loop' (because i==0), then 'End'. For i=1: prints 'Start' and 'End' only.
Final Answer:
Start\nFirst loop\nEnd\nStart\nEnd -> Option A
Quick Check:
Loop with condition prints extra line only once [OK]
Hint: Follow indentation to see which prints run inside conditions [OK]
Common Mistakes:
Ignoring indentation and printing 'First loop' twice
Mixing order of printed lines
Assuming all prints run every iteration
4. Identify the error in this code:
if True:
print('Yes')
print('Still Yes')
medium
A. SyntaxError because of missing colon
B. IndentationError due to inconsistent indentation
C. No error, code runs fine
D. NameError due to undefined variable
Solution
Step 1: Check indentation after if statement
Lines inside the if block must be indented equally.
Step 2: Identify inconsistent indentation
The first print is not indented, the second print is indented. This causes an IndentationError.
Final Answer:
IndentationError due to inconsistent indentation -> Option B
Quick Check:
Inconsistent indentation = IndentationError [OK]
Hint: All lines in a block must have same indentation [OK]
Common Mistakes:
Thinking missing colon causes error here
Assuming code runs without indentation
Confusing IndentationError with NameError
5. You want to write a function that prints numbers 1 to 3, each followed by 'Done' on a new line. Which code correctly uses indentation to achieve this?
def print_numbers():
for i in range(1, 4):
print(i)
print('Done')
hard
A.
def print_numbers():
for i in range(1, 4):
print(i)
print('Done')
B.
def print_numbers():
for i in range(1, 4):
print(i)
print('Done')
C.
def print_numbers():
for i in range(1, 4):
print(i)
print('Done')
D.
def print_numbers():
for i in range(1, 4):
print(i)
print('Done')
Solution
Step 1: Understand desired output
Each number 1 to 3 should print, then 'Done' on a new line after each number.
Step 2: Check indentation for print statements
To print 'Done' after each number, both print(i) and print('Done') must be inside the for loop, indented equally.
Step 3: Identify correct option
def print_numbers():
for i in range(1, 4):
print(i)
print('Done')
has both prints indented inside the loop, so 'Done' prints after each number.
Final Answer:
def print_numbers():\n for i in range(1, 4):\n print(i)\n print('Done') -> Option C
Quick Check:
Same indentation inside loop = prints per iteration [OK]
Hint: Indent all loop actions equally to repeat each time [OK]
Common Mistakes:
Indenting 'Done' outside loop causing single print