Which statement is used to create a new function in Python?
- class
- def
- import
- return
In Python, the def keyword is used to define and create a new function. This statement is followed by the function name and a block of code that defines the function's behavior.
What is the purpose of the name variable in Python?
- It is used to define a custom name for a Python function.
- It is used to determine if a Python script is being run as the main program or if it is being imported as a module into another script.
- It is used to specify the name of a Python package.
- It is used to store the current date and time.
The __name__ variable in Python is used to determine if a Python script is being run as the main program (__name__ is set to "__main__") or if it is being imported as a module into another script (__name__ is set to the name of the module). This allows you to create reusable modules that can also be run as standalone scripts.
How can you execute a Python file from within another Python file?
- exec('file.py')
- import 'file.py'
- run('file.py')
- subprocess.call('file.py')
You can execute a Python file from within another Python file using subprocess.call('file.py', shell=True) to run it as a separate process. The other options are not used for running external Python files.
How would you dynamically import a module if you have the module’s name stored in a string variable?
- import moduleName from 'modulePath'
- import(moduleName as 'modulePath')
- import(moduleName)
- import(moduleName, 'modulePath')
To dynamically import a module when you have the module's name stored in a string variable, you would use:
import(moduleName)
However, this isn't directly valid syntax in Python.
The recommended way for Python 2.7 and 3.1 and later is to use importlib
module:
import importlib
module = importlib.import_module(moduleName)
With Python older than 2.7/3.1, using __import__
you can import a list of modules by doing this:
>>> moduleNames = ['sys', 'os', 're', 'unittest']
>>> moduleNames
['sys', 'os', 're', 'unittest']
>>> modules = map(__import__, moduleNames)
How would you remove duplicate values from a list in Python?
- Use a set to store unique values
- Use a for loop with conditions
- Use the list.remove() method
- Use the list.sort() method
To remove duplicate values from a list in Python, you can convert the list to a set. Sets automatically store only unique values, so when you convert a list to a set and back to a list, duplicates are removed. The other options do not directly address the removal of duplicates.
How can you handle an exception for an error caused by dividing by zero in Python?
- Use a try-except block
- Use a for loop
- Use a while loop
- Use an if statement
To handle an exception for an error caused by dividing by zero in Python, you should use a try-except block. Inside the try block, you should place the code that might raise the exception, and in the except block, you can define how to handle the exception gracefully. The other options are not suitable for handling this specific exception.
How would you remove duplicate values from a list in Python?
- Use a set to store unique values
- Use a for loop with conditions
- Use the list.remove() method
- Use the list.sort() method
To remove duplicate values from a list in Python, you can convert the list to a set. Sets automatically store only unique values, so when you convert a list to a set and back to a list, duplicates are removed. Here’s an example:
my_list = [1, 2, 2, 3, 4, 4]
unique_list = list(set(my_list))
The other options do not directly address the removal of duplicates.
How can you handle an exception for an error caused by dividing by zero in Python?
- Use a for loop
- Use a while loop
- Use an if statement
- Use a try-except block
To handle an exception for an error caused by dividing by zero in Python, you should use a try-except block. Inside the try block, you should place the code that might raise the exception, and in the except block, you can define how to handle the exception gracefully. The other options are not suitable for handling this specific exception.
How would you access the last element of a list stored in a variable named my_list?
- my_list[-1]
- my_list[0]
- my_list[1]
- my_list[len(my_list)]
In Python, my_list[-1]
is used to access the last element of a list. my_list[0]
would access the first element. my_list[1]
would access the second element. my_list[len(my_list)]
would cause an IndexError as the index would be out of range.
What would be the output of the following Python code? print(type([]))
- <class 'str'>
- <class 'list'>
- <class 'int'>
- <class 'tuple'>
The output of type([])
would be <class 'list'>
because the empty square brackets []
represent an empty list, so the type() function returns <class 'list'>.