Lambda functions
The Lambda functions are nice for Calculus functions or ant one-line function with a simple input-output format. They can also be used to create functions “anonymously”. A Lambda function can take any number of arguments, but can only have one expression. Using a Lambda function, we can make our codes more concise.
# The syntax for Lambda is:
lambda<argument(s)>: <expression>
# suppose a normal function: f(x)=x^3
# function in Python
def f(x):
return x**3
# Lambda
f = lambda x: x**3
# another normal function: g(x,y,z)=1/2*x*y*z
# Lambda
g = lambda x, y, z: 1/2 * x * y * zLambda functions can be used without naming them. For instance, some functions require a function as input, and a lambda can be directly plugged in as the input. The power of lambda is also shown when you use them as an anonymous function inside another function.
# Lambda inside other function
def my_fun(n):
return lambda x: x ** n
# using my_fun
foo = my_fun(3)
print(foo(5))
# the result is 5**3 = 125Map
Another use of Lambda is in the “map” function. The “map” function in python will “map” all the values of a iterable container (mostly a list) using a given function. As in, all the values will be plugged into the function and transformed accordingly. These functions used in the “map” function are mostly Lambda functions.
# the syntax of map:
map(function, iterables)
# it will return a map object, we can use list() to turn it into a list
# suppose there is a list L, generate another list using the values in L but square them
>>> L = [3, 5, 1, 9]
>>> m = map(lambda x: x**2, L)
>>> print(list(m))
[9, 25, 1, 81]
# map function also works for multiple iterable objects
>>> L1 = [1, 3, 5, 7]
>>> L2 = [2, 4, 6, 8]
>>> m = map(lambda a, b: a + b, L1, L2)
>>> print(list(m))
[3, 7, 11, 15]
# here I have two lists, and a lambda function that used in map function to add the according numbers in these two listsFilter
Filter is used to pick out the elements of a iterable container (mostly a list) that satisfy some conditions. These conditions are mostly given by some Lambda functions. Here the Lambda function needs to be an expression that return either true or false.
# the syntax of filter:
filter(function, iterable)
# it will return a filter object, we can use list() to turn it into a list
# suppose there is a list L, generate a new list using the values in L but need to be greater than 5
>>> L = [8, 1, 6, 2, 7]
>>> f = filter(lambda x: x > 5, L)
>>> print(list(f))
[8, 6, 7]
# using filter, we can get the values in L which are greater than 5
# here filter function can only work for 1 iterable object
# we can also combine filter and map
# nested vision:
>>> L = [8, 1, 6, 2, 7]
>>> c = map(lambda x: x ** 2, filter(lambda x: x >= 5, L))
>>> print(list(c))
[64, 36, 49]
# here the filter function will be executed first, which picks the values that are >= 5
# then the map function will square those valuesArbitrary inputs
Sometimes in the main function of C, we can see int main(int argc, char* argv[]). Here argc stands for argument count, and argv stands for argument values. which will be used for arbitrary inputs. In python, there is a similar keyword for arbitrary input arguments called “*args” which stands for “arguments”. We can use “*args” to allow a function in Python to have arbitrarily many inputs, especially in the case when we don’t know how many inputs will be passed into our function. Although “*args” stands for arguments, it can be any names, not necessarily be “args”. But a “*” needs to be added before the argument’s name. On the other hand, we should treat “*args” as a tuple. In this case, the arguments in “*args” can be 0 or as many as we want.
# example of arbitrary arguments
>>> def print_all(*args):
... print(type(args))
... print("Here are the content of *argus:")
... for val in args:
... print(val)
...
>>> print_all(1, "apple", True, 3.14)
<class 'tuple'> # the data type of "*argus"
Here are the content of *argus:
1
apple
True
3.14
# we can have regular inputs AND arbitrary input, but be sure to put "*args" at last
# for instance:
def foo(var_1, var_2, *args)
# we can also give a default value to our input argument
def foo(x = 5)
# in this case, if there is no input, python will assume x is 5
# small note: if we want to pass all the values of "*args" to another function
# be sure to include "*" when passing, otherwise, it will be a tuple in tuple
>>> def foo1(*args):
... for val in args:
... print(val)
...
>>> def foo2(*args):
... foo1(*args)
...
>>> foo2(1, "apple", True, 3.14)
1
apple
True
3.14There is another arbitrary input arguments called “**kwargs”. “**kwargs” stands for keyword arguments. We should treat kwargs as a DICTIONARY. When passing in arguments, we should use the syntax of key=value.
# example of **kwargs
def foo1(**kwargs):
for k in kwargs:
print(f"key={k}, value={kwargs[k]}")
# pass in arguments
foo1(fruit="apple", value_of_pi=3.14)
# result:
key=fruit, value=apple
key=value_of_pi, value=3.14
# similar to "*args", when passing all the values in "**kwargs" to another function
# be sure the "**" was added before the kwargs
def foo1(**kwargs):
for k in kwargs:
print(f"key={k}, value={kwargs[k]}")
def foo2(**kwargs):
foo1(**kwargs)
