What does the as keyword do in the context of importing modules in Python?
- Creates an alias for the module
- Renames the imported function
- Renames the module
- Specifies the module's version
The as keyword creates an alias for the imported module, allowing you to use a different name when referencing it in your code. This can be useful for avoiding naming conflicts.
If you wish to make a shallow copy of a list, you can use the _______ method.
- clone() Method
- copy() Method
- deepcopy() Method
- slice() Method
To make a shallow copy of a list, you can use the 'copy()' method. It creates a new list containing references to the same objects as the original list.
If you want to make a variable defined in an outer function accessible to an inner function, you use the _______ keyword.
- global
- inner
- outer
- public
To make a variable defined in an outer function accessible to an inner function, you use the 'global' keyword. It indicates that the variable should be treated as a global variable.
Which special variable in Python represents the name of the current module?
- __current__
- __main__
- __module__
- __name__
The special variable __name__ represents the name of the current module in Python. When a Python script is run, the value of __name__ is set to '__main__', indicating that it's the main script being executed. In imported modules, __name__ is set to the module's name. It's often used to check if a script is run as the main program or imported as a module.
The _______ file can be used to execute initialization code for a package.
- init.init
- init.py
- init.txt
- package.py
The __init__.py file is used to execute initialization code for a package. It runs when the package is imported or used, allowing you to set up package-level configurations or variables.
You are building a recommendation engine and have a set of products bought by User A and another set for User B. To recommend new products to User A based on User B's purchases, which set operation would you use considering you don't want to recommend products User A has already bought?
- Difference
- Intersection
- Symmetric Difference
- Union
To recommend new products to User A based on User B's purchases while avoiding duplicates, you would use the "Difference" set operation. This will provide products in User B's set that are not in User A's set.
To create a tuple with a single item, you need to add a _______ after the item.
- Colon
- Comma
- Period
- Semicolon
To create a tuple with a single item, you need to add a comma after the item. For example, my_tuple = (42,). A trailing comma distinguishes a single-item tuple from an expression in parentheses.
When sorting a list with the sort() method, using the _______ argument can allow for custom sorting behaviors.
- custom
- custom_sort
- key
- sort_by
When sorting a list using the sort() method, you can provide a key argument to specify a custom sorting behavior. This allows you to sort based on specific criteria or functions.
In a program processing a list of numbers, the objective is to print all numbers until a negative number is encountered. Which control statement would be apt for this task?
- break
- for
- if
- while
A while loop is apt for this task because it allows you to repeatedly check if the current number is negative and, if not, print it. When a negative number is encountered, the loop terminates, ensuring only non-negative numbers are printed.
In Python, the _______ method is used to overload the + operator.
- __add__
- __init__
- __main__
- __str__
In Python, the __add__ method is used to overload the + operator for user-defined classes. This method is called when the "+" operator is applied to objects of the class.