Lambda Functions For Python 3
Lambda Functions For Python 3
print(add_two(3))
print(add_two(100))
print(add_two(-2))
would print:
>>> 5
>>> 102
>>> 0
Let’s break this syntax down:
print(is_substring('I'))
print(is_substring('am'))
print(is_substring('the'))
print(is_substring('master'))
would print:
>>> False
>>> False
>>> True
>>> True
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…'. So,
the code:
print(check_if_A_grade(91))
print(check_if_A_grade(70))
print(check_if_A_grade(20))
would print:
grade >= 90
3. Otherwise, return 'Did not get an A...' if this statement is not true:
grade >= 90
Lambda functions only work if we’re just doing a one line command. If we
wanted to 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 functions is not present
with lambda functions. By saving the work of defining a function, a lambda
function allows 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.