Question Detail

COS 102 6 lecture note; fundamentals of programming Objective Question

Question

What is the correct general format for a `for` loop in Python designed to iterate through elements of an iterable (like a list, tuple, or string)?
Options
A <code>for (element in iterable) { body }</code>
B <code>for each element of iterable do: body</code>
C <code>for element in iterable:</code><br><code> body</code>
Correct Answer
D <code>iterate element from iterable: body</code>
Correct Answer

Option C is the correct answer.

Detailed Explanation

Page 74 of the document states: "Syntax or general format of for loop is, for element in iterable: body". This shows the keyword for, a variable name for the element, the in keyword, the iterable object, and a colon, followed by an indented body.

Hint

Recall the common pattern for iterating directly over collection items in Python, using the `in` keyword.

Question Info