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.
SIMULATION
Fix the off-by-one error in this function that should return the first 3 characters of a string.
def first_three(text):
return text[0:2]
==========
Step 1: Python string slicing uses this format:
text[start:stop]
Step 2: The start index is included.
Step 3: The stop index is excluded.
Step 4: To return the first 3 characters, start at index 0 and stop at index 3.
Correct code:
def first_three(text):
return text[0:3]
Simplified correct code:
def first_three(text):
return text[:3]
Example:
print(first_three('Python'))
Output:
Pyt
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.
Gerald Clark
9 days agoMargaret Nelson
12 days agoJeffrey White
21 days agoMargaret Parker
1 month agoGeorge Nguyen
1 month ago