Which components are required in every Python for loop?
Correct Answer: A
Explanation:
A Python for loop is used to iterate over an iterable object, such as a list, tuple, string, dictionary, set, or range() object. A basic for loop needs: A loop variable An iterable object An indented code block Example: for number in range(3): print(number) In this example: number is the loop variable. range(3) is the iterable. print(number) is the indented code block that runs during each loop iteration. According to the official Python documentation, the for statement is a construct that works with iterable objects. Therefore, the correct answer is A. A variable, an iterable, and an indented code block.
Question 2
What determines which lines of code are executed when the condition is true in a Python if statement?
Correct Answer: B
Explanation:
In Python, indentation determines which statements belong to an if block. Example: temperature = 30 if temperature > 25: print("It is warm.") print("Drink water.") Both print() statements are executed when the condition is true because they share the same indentation level under the if statement. Python does not use parentheses, semicolons, or square brackets to define the body of an if statement. It uses indentation. Therefore, the correct answer isB. Lines that share the same indentation level.
Question 3
Which type of loop repeatedly checks a condition to determine whether to continue?
Correct Answer: B
Explanation:
Awhile looprepeatedly executes a block of code as long as its condition remains true. Example: count = 1 while count <= 3: print(count) count += 1 In this example, Python checks the condition count <= 3 before each loop iteration. If the condition is true, the loop continues. When the condition becomes false, the loop stops. A for loop is usually used to iterate over a sequence, such as a list, string, or range. Python does not have a built-in repeat loop construct. Therefore, the correct answer isB. while loop.
Demo Practice Mode
You are viewing only the questions marked as Demo.