What is the output of the following code snippet?
matrix = [[1, 2, 3], [4, 5, 6]]
print(matrix[0][2])
Explanation:
matrix[0]
accesses the first sublist [1, 2, 3], and then [0][2] accesses the third element in that sublist, which is 3.
What is the purpose of the finally
block in Python exception handling?
Explanation:
The finally
block is used for cleanup actions that should always happen, like closing files or releasing resources, whether an exception occurred or not.
In a function definition, what is the order in which you should define different types of arguments?
Explanation:
This is the standard order enforced by Python to avoid ambiguity in function definitions and calls. Mixing the order can lead to syntax errors.
What is the primary advantage of using the with
statement for file operations in Python?
Explanation:
The with
statement provides all these benefits: automatic file closure (ensuring resources are released), improved code readability, and slightly better performance in some cases.
How can you access the constant 'pi' from the 'math' module after importing it?
Explanation:
After importing a module, you use the dot notation (.) to access its members. Therefore, 'math.pi' correctly retrieves the value of 'pi' from the 'math' module.
How can you best describe the functionality of a decorator in Python?
Explanation:
Decorators provide a clean way to extend or modify the functionality of existing functions by wrapping them in another function. This is a key aspect of aspect-oriented programming in Python.
What is the purpose of the 'r' prefix in a regular expression pattern?
Explanation:
The 'r' prefix before a regular expression pattern in Python indicates a raw string literal, which treats backslashes as literal characters, avoiding the need to escape them.
In Python, what is the difference between a class variable and an instance variable?
Explanation:
Class variables hold data associated with the class itself, and this data is shared across all instances. Instance variables, on the other hand, store data that is specific to each individual object (instance) created from the class.
What does the following code snippet print?
def outer_func(x):
def inner_func(y):
return x + y
return inner_func
my_func = outer_func(5)
print(my_func(3))
Explanation:
This demonstrates closures. inner_func
has access to x
from outer_func
even after outer_func
has finished executing. When my_func(3)
is called, it adds 3 to the remembered x
value of 5.
What is a key characteristic of a closure in Python?
Explanation:
Closures are important for creating functions that maintain state between calls, even without using global variables, making code more modular and organized.
What is the output of the following Python code:
string = 'Hello, world!'
print(string.replace('o', '0', 1))
Explanation:
The replace()
method replaces the first occurrence of the specified substring ('o') with the new substring ('0') because the optional third argument (count) is set to 1.
In Python, how do you access an instance variable within a class method?
Explanation:
'self' represents the instance of the class. Inside a class method, 'self.variable_name' is used to access or modify the instance variable associated with that specific instance.
What is the most efficient way to concatenate a large number of strings in Python?
Explanation:
The ''.join() method is the most efficient way to concatenate a large number of strings in Python because it avoids creating intermediate string objects, unlike the '+' operator.
What is the primary purpose of decorators in Python?
Explanation:
Decorators allow you to wrap a function with another function, modifying its behavior without directly altering its implementation.
What is the purpose of using a default argument in a Python function?
Explanation:
Default arguments allow you to set a predefined value for a parameter, which will be used if the caller doesn't provide a value for that parameter.