Skip to content

Create lambda_func.py #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions samples/function/lambda_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


# Lambda functions are called anonymous functions because they don't have a name,
# and they are not defined with the def keyword. Instead,
# they are defined using the lambda keyword followed by a list of arguments,
# a colon, and an expression that is evaluated and returned as the result of the function.

# Example 1
# this is a simplest example of a lambda function that inputs an integer and returns its square
unknownSquarer_func = lambda x: x*2

print(unknownSquarer_func(2)) # --> 4


# Example 2

# iterable list

numList = [1,2,3,4,5]

# this lambda function is the function that will be applied to each item in iterable using map keyword

squaredNumbers = map(lambda x:x*2,numList)

print(list(squaredNumbers))