A program needs to update all values in a list by adding 5 to each number. Which for loop structure accomplishes this task?
To update the actual values inside a list, the program must update each value by itsindex.
The correct code is:
for i in range(len(numbers)):
numbers[i] = numbers[i] + 5
Here, range(len(numbers)) produces valid index positions for the list. The variable i is used to access and update each list element.
Example:
numbers = [10, 20, 30]
for i in range(len(numbers)):
numbers[i] = numbers[i] + 5
print(numbers)
Output:
[15, 25, 35]
Option A does not correctly update the original list:
for number in numbers:
number = number + 5
This changes only the temporary loop variable number, not the actual elements stored inside the list.
Therefore, the correct answer isB.
Which symbol begins a single-line comment in Python?
In Python, a single-line comment begins with the # symbol.
Example:
# This is a comment
print('Hello')
Python ignores the comment when the program runs. The Python documentation identifies # as the symbol used for comments.
Therefore, the correct answer isB. # pound symbol.
What sequence of steps is required to execute a Python script from a text editor using the terminal on a Windows device?
To run a Python script from a text editor using the terminal on Windows, the file should first be saved with the .py extension. Then, the terminal is opened, and the script is executed using the Python command followed by the filename.
Example:
python filename.py
The official Python documentation explains that when the interpreter is called with a filename argument, it reads and executes the script from that file. On Windows, the python command can be used from a terminal after Python is installed and configured correctly.
Therefore, the correct answer is A. Save file > open terminal > type python filename.py.
In Python, what must follow the in keyword in a for loop?
In a Python for loop, the in keyword is followed by an iterable object, such as a list, string, tuple, dictionary, set, or range() object.
Example:
for name in ['Alice', 'Bob', 'Carol']:
print(name)
Here, ['Alice', 'Bob', 'Carol'] is the iterable object. Python's documentation explains that a for statement iterates over the items of a sequence or other iterable object.
Therefore, the correct answer isB. An iterable object.
A program uses this while loop to count down:
count = 5
while count > 0:
print(count)
Which issue is preventing the loop from working correctly?
The loop condition is:
while count > 0:
At the start, count is 5, so the condition is true. The program prints the value of count.
However, inside the loop, the value of count is never changed. That means count always stays 5, so the condition count > 0 is always true. As a result, the loop runs forever.
A corrected version would be:
count = 5
while count > 0:
print(count)
count = count - 1
This decreases count by 1 during each loop iteration. Eventually, count becomes 0, and the condition count > 0 becomes false.
Therefore, the correct answer isB. It runs infinitely because count is never modified.
Currently there are no comments in this discussion, be the first to comment!
Currently there are no comments in this discussion, be the first to comment!