How do you convert a list of lists into a single flat list in Python?
- [item for sublist in nested_list for item in sublist]
- list(nested_list)
- nested_list.flat()
- nested_list.flatten()
To flatten a list of lists in Python, you can use a list comprehension with nested loops. This method creates a new list containing all elements from the inner lists concatenated together.
How can you parameterize a test function in pytest to run it multiple times with different arguments?
- Using the @param decorator
- Using the @parametrize decorator
- Using the @pytest.mark.parametrize decorator
- Using the @pytest.parameterize decorator
To parameterize a test function in pytest, you should use the @pytest.mark.parametrize decorator. It allows you to specify multiple sets of input arguments and expected outcomes for a test function.
How do you instantiate an object from a class in Python?
- create Object from Class;
- new Object(Class);
- obj = Class()
- object = new Class()
To instantiate an object from a class in Python, you use the syntax object_name = Class_name(). The other options are not valid syntax for object instantiation in Python.
How can you optimize the performance of static files (CSS, JS, Images) in a web application developed using Python frameworks?
- Compress and minify static files
- Optimize database queries
- Use serverless functions
- Utilize a Content Delivery Network (CDN)
Utilizing a Content Delivery Network (CDN) is a highly effective way to optimize the performance of static files. CDNs distribute your files across multiple geographically distributed servers, reducing latency and improving load times.
How can you optimize the recursive Fibonacci function with dynamic programming?
- Convert it to an iterative function
- Implement a tail-recursive version
- Increase the base case value
- Use memoization to store intermediate results
Dynamic programming can optimize the recursive Fibonacci function by using memoization to store previously calculated Fibonacci numbers, reducing redundant calculations. The other options don't directly optimize the recursive approach.
How would you prevent overfitting in a deep learning model when using frameworks like TensorFlow or PyTorch?
- By increasing the model's complexity to better fit the data.
- By reducing the amount of training data to limit the model's capacity.
- By using techniques like dropout, regularization, and early stopping.
- Overfitting cannot be prevented in deep learning models.
To prevent overfitting, you should use techniques like dropout, regularization (e.g., L1, L2), and early stopping. These methods help the model generalize better to unseen data and avoid fitting noise in the training data. Increasing model complexity and reducing training data can exacerbate overfitting.
How would you replace all NaN values in a DataFrame with zeros in Pandas?
- df.fillna(0)
- df.NaNToZero()
- df.replace(NaN, 0)
- df.zeroNaN()
To replace all NaN values with zeros in a Pandas DataFrame, you can use the fillna() method with the argument 0. This will fill all NaN occurrences with zeros.
How would you run a Python script from the command line and pass arguments to it?
- python execute script.py with-args arg1 arg2
- python -r script.py arg1 arg2
- python run script.py --args arg1 arg2
- python script.py arg1 arg2
To run a Python script from the command line and pass arguments, you use the python command followed by the script name and the arguments separated by spaces, like python script.py arg1 arg2. This allows you to pass arguments to your script for processing.
How would you set a breakpoint in a Python script to start debugging?
- breakpoint()
- debug()
- pause()
- stop()
In Python 3.7 and later, you can set a breakpoint by using the breakpoint() function. It pauses the script's execution and enters the interactive debugger at that point, allowing you to examine variables and step through code.
How would you set up a custom command in Django that can be run using the manage.py file?
- a. Create a Python script with your command logic, save it in the Django project directory, and add an entry in the commands list in the project's __init__.py.
- b. Create a Python script with your command logic and place it in the management/commands directory of your Django app.
- c. Modify the Django source code to add your custom command.
- d. Use a third-party package for custom commands.
To set up a custom management command in Django, you should create a Python script in the management/commands directory of your app. Django will automatically discover and make it available through manage.py. Options a, c, and d are not standard practices.