How can you specify a default value for a parameter in a function definition?
- By wrapping the parameter in square brackets []
- Using an asterisk (*)
- Using the '=' sign
- Using the 'default' keyword
To specify a default value for a parameter, you can use the '=' sign followed by the default value. For example, def func(param=10):.
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.
In Python, a multi-line string can be defined using _______ characters.
- Double quotes
- Single quotes
- Triple quotes
- Triple single-quotes
In Python, a multi-line string can be defined using triple single-quotes ('''...''') or triple double-quotes ("""...""").
What does the *args notation in function parameters allow for?
- Defining keyword arguments
- Passing a variable number of arguments
- Restricting the number of function calls
- Specifying the return type of the function
The *args notation allows a function to accept a variable number of non-keyword arguments. These arguments are packed into a tuple, allowing flexibility in function calls. It's often used when the exact number of arguments is unknown in advance.