Open oldoc63 opened 1 year ago
Let's write a lambda function that checks if a string is a substring of the string "This is the master string".
We might want a function that will perform differently based on different inputs. Let's say that we have a function check_if_A_grade that outputs 'Got an A! If a grade is at least 90, and otherwise says you 'Did not get an A ... '
This is what this code of line does:
Lambda functions only work if we're just doing a one line command. If we wanted to write something longer, we'd need a more complex function. Lambda functions are great when you need to use a function once. Because you aren't defining a function, the reusability aspect function is not present with lambda functions. By saving a work of defining a function, a lambda function allow us to efficiently run an expression and produce an output for a specific task, such as defining a column in a table, or populating information in a dictionary.
A function is an object that is able to accept some sort of input, possibly modify it, and return some sort of output. In Python, a lambda function is a one-line shorthand for function.