You need to filter out all even numbers from a list numbers and square them. Which Python feature would be the most concise to achieve this?
- Filter and lambda functions
- For loop
- List comprehensions
- Map and lambda functions
List comprehensions are the most concise way to filter and manipulate elements in a list. You can achieve this with a one-liner using list comprehensions.
What's the primary difference between from module import * and import module as alias?
- Both statements are identical and can be used interchangeably.
- The former imports all names from the module, polluting the namespace.
- The former is faster in terms of execution.
- The latter allows selective access to module members using the alias.
'from module import *' imports all names from the module into the current namespace, potentially causing naming conflicts. 'import module as alias' imports the module with a specified alias, offering more controlled access to its members.
What mode should you open a file in to write to it without deleting its existing content?
- append mode
- create mode
- update mode
- write mode
To write to a file without deleting its existing content, you should open the file in 'write mode' ('w'). It creates a new file if it doesn't exist.
You have written a function that accepts any number of positional and keyword arguments and prints them. However, the function signature does not specify any parameter names. How was this achieved?
- Using Default Arguments
- Using Variable-Length Argument Lists with *args and **kwargs
- Using Variable-Length Argument Lists with var_args and kw_args
- Using the args and kwargs Keywords
This is achieved by using variable-length argument lists with *args and **kwargs in the function signature. *args allows you to pass any number of positional arguments, and **kwargs allows you to pass keyword arguments without specifying their names.
To check the version of Python installed, one can use the command python _______.
- --version
- -check
- -v
- -version
To check the Python version, you can use the command 'python --version' or 'python -V'. It displays the installed Python version information.
Using the super() function without any arguments inside a derived class method implicitly refers to the _______ class.
- Ancestor
- Base
- Derived
- Parent
Using super() without any arguments inside a derived class method refers to the base class. It's commonly used to access and invoke methods and properties of the base class.
Which Python object can be used to create a simple context manager without defining a class?
- @contextmanager decorator
- contextlib.ExitStack()
- contextlib.AbstractContextManager()
- contextlib.contextmanager()
The contextlib.contextmanager() decorator is used to create a simple context manager without defining a class. Other options are used for different purposes related to context management.
How does Python handle the absence of method overloading like in some other programming languages?
- By raising an error
- By using decorators
- By using default arguments
- By using variable arguments
Python handles the absence of method overloading by allowing functions to accept different numbers and types of arguments using variable arguments.
When using Python virtual environments, the command to activate the environment on Unix or MacOS is source _______.
- activate env
- activate venv
- pythonenv
- source venv
When using virtual environments, on Unix or MacOS, the command to activate the environment is source venv, where venv is the name of your virtual environment. This isolates your Python environment for project-specific dependencies.
To exit out of a loop prematurely, you would use the _______ statement.
- break
- continue
- exit
- return
The break statement is used to exit out of a loop prematurely. When encountered, it terminates the loop's execution and moves to the next statement after the loop.