Which tool is used to execute Python code line-by-line interactively within a terminal?
Answer : A
The Python shell allows Python code to be entered and executed interactively, one line at a time.
Example:
>>> 2 + 3
5
Python's documentation explains that when commands are read from a terminal, the interpreter is in interactive mode and prompts for the next command.
Therefore, the correct answer isA. Python shell.
SIMULATION
Write a complete function calculate_discount(price, discount_percent) that calculates and returns the final price after applying a discount percentage.
For example, calculate_discount(75, 20) should return 60.0.
def calculate_discount(price, discount_percent):
# TODO: Calculate and return the final price after discount
pass
Answer : A
==========
Step 1: The function receives the original price and the discount_percent.
Step 2: Convert the discount percentage into a decimal by dividing by 100.
Step 3: Subtract the discount from 1 to find the remaining price percentage.
Step 4: Multiply the original price by the remaining percentage.
Correct code:
def calculate_discount(price, discount_percent):
return price * (1 - discount_percent / 100)
Example:
print(calculate_discount(75, 20))
Output:
60.0
SIMULATION
Complete the function double_number(num) that takes one number parameter and returns double that number.
For example, double_number(5) should return 10.
def double_number(num):
# TODO: Return double the input number
# Example: double_number(5) should return 10
pass
Answer : A
==========
Step 1: The function receives one parameter named num.
Step 2: To double a number, multiply it by 2.
Step 3: The function should return the result using the return statement.
Correct code:
def double_number(num):
return num * 2
Example:
print(double_number(5))
Output:
10
Which type of loop repeatedly checks a condition to determine whether to continue?
Answer : B
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.
Which Python data structure allows duplicate values and supports methods like append() and remove()?
Answer : C
A list is a Python data structure that can store multiple values. Lists can contain duplicate values, and they support methods such as append() and remove().
Example:
fruits = ['apple', 'banana', 'apple']
fruits.append('orange')
fruits.remove('banana')
print(fruits)
Output:
['apple', 'apple', 'orange']
The value 'apple' appears more than once, showing that lists allow duplicates.
The official Python documentation lists list.append() as a method that adds an item to the end of a list and list.remove() as a method that removes the first matching value from a list.
Therefore, the correct answer is C. List.
Which punctuation mark must appear at the end of an if statement line?
Answer : B
In Python, an if statement line must end with a colon.
Example:
age = 18
if age >= 18:
print('Adult')
The colon : tells Python that an indented block of code follows. That indented block contains the statements that should run when the condition is true.
A semicolon, period, or comma is not used to begin the body of an if statement in Python.
Therefore, the correct answer is B. : colon.
What determines which lines of code are executed when the condition is true in a Python if statement?
Answer : B
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.