Junior — Middle
Can you list the main concepts and elements of functional programming used in Python?
sobes.tech AI
Answer from AI
In Python, the following core concepts and elements are used for functional programming:
- Higher-order functions — functions that accept other functions as arguments or return them.
- Lambda functions — anonymous functions created using the
lambdakeyword. - Iterables and generators — allow working with data sequences lazily.
- Functions
map(),filter(),reduce()— apply functions to collections for data transformation and filtering. - Immutability — using immutable data structures (e.g., tuples) to avoid side effects.
Example of using higher-order functions and lambda functions:
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
filtered = list(filter(lambda x: x % 2 == 0, squared))
print(filtered) # [4, 16]