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 is the purpose of the '#' symbol in Python code?
Explanation:
In Python, any text following a #
symbol on a line is considered a comment and is ignored by the interpreter. Comments are used to explain code and make it more readable.
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.
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 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'.
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 be the output of this code snippet?
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
Explanation:
The continue
statement skips the current iteration when i
equals 3, preventing the number 3 from being printed.
What will the following code snippet print?
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
print(tuple1 + tuple2)
Explanation:
The '+' operator concatenates tuples in Python, creating a new tuple containing all elements.
Which comparison operator is used to check if two values are not equal?
Explanation:
The '!=' operator checks for inequality between two values. If the values are not equal, it returns True; otherwise, it returns False.
What will the following Python code snippet print?
mult = lambda x, y: x * y
print(mult(5, 3))
Explanation:
The code defines a lambda function mult
that multiplies two numbers. When called with mult(5, 3)
, it returns the product of 5 and 3, which is 15.
What is the output of the following Python code?
my_list = [1, 2, 3, 4, 5]
print(my_list[1:3])
Explanation:
In Python slicing, the starting index is inclusive, and the ending index is exclusive. Therefore, my_list[1:3]
will return a new list containing elements from index 1 (the second element) up to, but not including, index 3.
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 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 purpose of the break
statement in a loop?
Explanation:
When encountered inside a loop, the break
statement immediately terminates the loop's execution and continues with the code after the loop.
What is the output of the following Python code snippet?
print(type(5.0))
Explanation:
The type()
function in Python returns the data type of a given value. In this case, 5.0
is a floating-point number, hence the output is <class 'float'>
.