What is the correct way to write and run a Python script named 'my_script.py' from the command line?
Explanation:
To execute a Python script, you use the command 'python' followed by the name of the script file ('my_script.py' in this case).
What is the output of the following Python code snippet?
print(True and False or True)
Explanation:
In Python, the 'and' operator has higher precedence than 'or'. So, 'True and False' evaluates to False, and then 'False or True' evaluates to True.
What is the primary purpose of using functions in Python?
Explanation:
Functions promote code reusability and organization. While they might indirectly contribute to efficiency, their primary role is not to directly speed up execution or reduce memory usage.
What will the following Python code snippet print?
x = 5
if x > 10:
print('A')
elif x > 3:
print('B')
else:
print('C')
Explanation:
The code checks if x
is greater than 10. Since it's not, it moves to the elif
condition. As x
is greater than 3, 'B' is printed.
Which of the following is NOT a valid way to format a string in Python?
Explanation:
Python doesn't have a built-in concat()
function for string formatting. The valid options are f-strings, the format()
method, and the %
operator.
What will the following code print: print(10 > 5 and 'apple' == 'orange')
?
Explanation:
The expression uses the and
operator. While 10 > 5
is True, 'apple' == 'orange'
is False. For 'and' to return True, both conditions must be True.
What is the maximum number of arguments a lambda function can take?
Explanation:
Lambda functions, despite their concise nature, can accept any number of arguments, making them versatile for various operations.
What will the following Python code print to the console?
name = input("Enter your name: ")
print("Hello,", name)
Explanation:
The code first prompts the user to enter their name. After the user provides input, the code then prints a greeting concatenated with the user's input.
What is the difference between arguments and parameters in Python functions?
Explanation:
Parameters act as placeholders within the function definition, while arguments are the actual values supplied when calling the function.
What is the output of the following Python code?
string = 'Hello, world!'
print(string[7:12])
Explanation:
In Python, string slicing uses a zero-based index. string[7:12]
extracts characters from index 7 (inclusive) to 12 (exclusive), resulting in the substring 'world'.
How many expressions can a lambda function have?
Explanation:
Lambda functions are limited to a single expression, which is evaluated and returned. This enforces their design for short, self-contained operations.
What is the time complexity of checking if an element is present in a Python set?
Explanation:
Sets in Python are implemented using hash tables, which allow for constant time complexity for checking membership. This means that the time it takes to check if an element is in the set does not depend on the size of the set.
What will the following code snippet print?
x = 5
y = 2 * x
print(y)
Explanation:
The code assigns 5 to 'x', then calculates 'y' as 2 times 'x' (which is 10), and finally prints the value of 'y'.
What will this code print?
for i in range(3): for j in range(2): print('*', end='') print()
Explanation:
The outer loop iterates three times, and the inner loop iterates twice for each outer loop iteration. The end=''
argument in the print statement prevents a newline after each asterisk, creating two asterisks per line.
What is the output of the following code?
name = 'Alice'
age = 30
print(f'{name} is {age} years old.')
Explanation:
F-strings (formatted string literals) allow embedding variables directly within strings using curly braces {}
. The code substitutes the values of name
and age
into the string.