Prompt Detail:
- What is the purpose of the map() function in Python?
The map() function in Python applies a function to each element of an iterable (such as a list) and returns a new iterable with the results.
- How do you use the map() function in Python?
To use the map() function in Python, pass a function and an iterable as arguments:
pythondef my_function(x):
return x * 2
my_list = [1, 2, 3, 4]
result = map(my_function, my_list)
print(list(result)) # outputs [2, 4, 6, 8]
This applies the my_function() function to each element of the my_list list and returns a new list with the results.
- What is the purpose of the filter() function in Python?
The filter() function in Python applies a function to each element of an iterable and returns a new iterable with only the elements for which the function returns True.
- How do you use the filter() function in Python?
To use the filter() function in Python, pass a function and an iterable as arguments:
pythondef my_function(x):
return x % 2 == 0
my_list = [1, 2, 3, 4]
result = filter(my_function, my_list)
print(list(result)) # outputs [2, 4]
This applies the my_function() function to each element of the my_list list and returns a new list with only the even elements.
- What is the purpose of the reduce() function in Python?
The reduce() function in Python applies a function to the first two elements of an iterable, then applies the function to the result and the next element, and so on, until all elements have been processed and a single value is returned.
- How do you use the reduce() function in Python?
To use the reduce() function in Python, import it from the functools module and pass a function and an iterable as arguments:
pythonfrom functools import reduce
def my_function(x, y):
return x + y
my_list = [1, 2, 3, 4]
result = reduce(my_function, my_list)
print(result) # outputs 10
This applies the my_function() function to the first two elements of the my_list list (1 and 2), then applies it to the result (3) and the next element (3), and so on, until all elements have been processed and the final result (10) is returned.
- What is the purpose of the zip() function in Python?
The zip() function in Python takes two or more iterables and returns a new iterable with tuples containing the corresponding elements from each iterable.
- How do you use the zip() function in Python?
To use the zip() function in Python, pass two or more iterables as arguments:
pythonmy_list1 = [1, 2, 3]
my_list2 = ["a", "b", "c"]
result = zip(my_list1, my_list2)
print(list(result)) # outputs [(1, "a"), (2, "b"), (3, "c")]
This creates a new iterable with tuples containing the corresponding elements from the my_list1 and my_list2 lists.
- What is the difference between a module and a package in Python?
A module in Python is a file containing Python code that can be imported and used in other Python code. A package in Python is a collection of modules (and possibly sub-packages) that can be imported and used in other Python code.
- How do you import a module in Python?
To import a module in