Question Detail

COS 102 If staments Objective Question

Question

What is the primary purpose of indentation in Python `if` statements?
Options
A To make the code visually appealing.
B To define the start and end of a code block belonging to the `if` statement.
Correct Answer
C To indicate that the code is optional.
D To comment out a line of code.
Correct Answer

Option B is the correct answer.

Detailed Explanation

Page 6, 'The "If" Syntax' slide, states under 'Indentation:' that 'The "action" must be spaced in!'. Page 15, 'Mistake #2: Colon & Indent', further clarifies: 'Python uses whitespace to know what code belongs to the if block.' Indentation is crucial for defining code blocks in Python.

Hint

Recall the 'Indentation' rule and its significance for Python code blocks, as highlighted in the syntax and common mistakes sections.

Question Info