MCQ Single Best Answer Moderate

QWhich of the following allows looping over a collection in Python?

ID: #23077 For Loop in Programming Language 73 views
Question Info
#23077Q ID
ModerateDifficulty
For Loop in Programming LanguageTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • 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)
Correct Answer: Option 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.

Share This Question

Challenge a friend or share with your study group.