Q: Which of the following allows looping over a collection in Python?
-
A
for (i = 0; i < 5; i++)
-
B
for i in range(5)
-
C
for i = 1; i < 5; i++
-
D
foreach (i in collection)
B
Answer:
B
Explanation:
In Python, the for loop syntax uses the in keyword to iterate over an iterable such as a list, tuple, or range. The range(5) generates a sequence of numbers from 0 to 4, and the loop iterates through each number in that sequence. Option A and C are syntax used in languages like Java or C++, and Option D resembles the foreach loop from languages like PHP or C#, but it's not valid in Python.
Related Topic:
Share Above MCQ