Foundations-of-Programming-Python practice question 1 of 10
Which components are required in every Python for loop?
Full set: 60 questionsA Python for loop is designed to iterate over the items of any sequence or iterable object. Its fundamental syntax requires three components: a target variable to hold the current item during each iteration, the in keyword followed by the iterable object (e.g., a list, string, or range), and a colon followed by an indented block of code. This code block, or suite, contains the statements to be executed for each item in the iterable.
B. A condition, counter, and break are typical of while loops or C-style for loops, but are not required components of a Python for loop.
C. A function call, parameter, and return value are components related to defining and using functions, not loops.
D. A list index, range limit, and step value are specific to iterating with the range() function, not a universal requirement for all for loops.
1. Python Software Foundation. (2023). The Python Tutorial, Section 4.2: for Statements. "The for statement in Python differs a bit from what you may be used to in C or Pascal... Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence." Retrieved from https://docs.python.org/3/tutorial/controlflow.html#for-statements
2. Guttag, J. V. (2016). Introduction to Computation and Programming Using Python, With Application to Understanding Data (2nd ed.). MIT Press. Chapter 3, Section 3.1 on iteration describes the for loop syntax as for in
3. Severance, C. R. (2016). Python for Everybody: Exploring Data Using Python 3. Chapter 5: Iteration. "The for loop has a looping variable that iterates through the sequence... The indented block of code is executed once for each value in the sequence."
Keep practicing
Take the free Foundations-of-Programming-Python practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other Foundations-of-Programming-Python practice questions
- Question 2Which keyword moves a loop immediately to its subsequent iteration?
- Question 3A program needs to update all values in a list by adding 5 to each number. Which for loop…
- Question 4What must be consistent within a Python code block for the code to run without syntax err…
- Question 5Which Python data structure cannot be modified after creation?
