Cara menggunakan python lambda square

You can run Python code in AWS Lambda. Lambda provides runtimes for Python that run your code to process events. Your code runs in an environment that includes the SDK for Python (Boto3), with credentials from an AWS Identity and Access Management (IAM) role that you manage.

Lambda supports the following Python runtimes.

Python

NameIdentifierSDKOperating systemArchitecturesDeprecation

Python 3.9

python3.9

boto3-1.20.32 botocore-1.23.32

Amazon Linux 2

x86_64, arm64

Python 3.8

python3.8

boto3-1.20.32 botocore-1.23.32

Amazon Linux 2

x86_64, arm64

Python 3.7

python3.7

boto3-1.20.32 botocore-1.23.32

Amazon Linux

x86_64

The runtime information in this table undergoes continuous updates. For more information on using AWS SDKs in Lambda, see Managing AWS SDKs in Lambda functions.

To create a Python function

  1. Open the Lambda console.

  2. Choose Create function.

  3. Configure the following settings:

    • Namemy-function.

    • RuntimePython 3.9.

    • RoleChoose an existing role.

    • Existing rolelambda-role.

  4. Choose Create function.

  5. To configure a test event, choose Test.

  6. For Event name, enter test.

  7. Choose Save changes.

  8. To invoke the function, choose Test.

The console creates a Lambda function with a single source file named lambda_function. You can edit this file and add more files in the built-in code editor. To save your changes, choose Save. Then, to run your code, choose Test.

The Lambda console uses AWS Cloud9 to provide an integrated development environment in the browser. You can also use AWS Cloud9 to develop Lambda functions in your own environment. For more information, see Working with Lambda Functions in the AWS Cloud9 user guide.

To get started with application development in your local environment, deploy one of the sample applications available in this guide's GitHub repository.

Sample Lambda applications in Python

  • blank-python – A Python function that shows the use of logging, environment variables, AWS X-Ray tracing, layers, unit tests and the AWS SDK.

Your Lambda function comes with a CloudWatch Logs log group. The function runtime sends details about each invocation to CloudWatch Logs. It relays any logs that your function outputs during invocation. If your function returns an error, Lambda formats the error and returns it to the invoker.

Topics

  • Lambda function handler in Python
  • Deploy Python Lambda functions with .zip file archives
  • Deploy Python Lambda functions with container images
  • AWS Lambda context object in Python
  • AWS Lambda function logging in Python
  • AWS Lambda function errors in Python
  • Instrumenting Python code in AWS Lambda

  • What are Python Lambda Functions?
  • Functions Vs Lambda functions:
  • Why use Python Lambda Functions?
  • Syntax:
  • Examples:
    • With a single argument:
    • With multiple arguments:
    • Lambda functions with normal functions:
    • lambda function with filter:
      • Syntax:
      • Example:
    • lambda function with map:
      • Syntax:
      • Example:
    • lambda function with reduce:
      • Syntax:
      • Example:

What are Python Lambda Functions?

Python Lambda functions are functions that do not have any name. Hence they are also known as anonymous or nameless functions. The word ‘lambda’ is a keyword. The function that follows the ‘lambda’ keyword is anonymous.

Functions Vs Lambda functions:

  • To define normal functions in function in Python, we use the ‘def’ keyword. We use ‘lambda’ keyword to define anonymous functions.
  • The lambda function can have any number of arguments, but they can return only one value in the form of expression. The Python lambda functions are syntactically restricted to a single expression.

Why use Python Lambda Functions?

  • Lambda functions come into the picture when we need a function only once.
  • Python Lambda functions are often used with built-in functions like filter(), map(), reduce(), etc.,
  • Using Lambda functions reduces the number of lines of code compared to normal functions.

Syntax:

lambda arguments:expression

As mentioned already, a lambda function can take any number of arguments but a single expression. Hence the number of arguments ranges from 0 to any number.

Examples:

With a single argument:

Here is an example to print the square root of a number.

a=lambda x:x**(1/2)
print(a(225))
15.0

x – is the argument.

x**(1/2) – The expression that gets evaluated.

lambda x:x**(1/2) – anonymous function which returns a function object.

>>> print(lambda x: x**(1/2))
 at 0x048388A0>

With multiple arguments:

Below is an example of a python lambda function that accepts multiple parameters.

a=lambda a,b,c:a+b+c
print(a(3,10,12))
25

When we write the same example using normal functions, which will take more lines. For instance,

def func(a,b,c):
    return a+b+c

print(func(3,10,12))

You might get this question, when we say lambda as nameless functions, then why we need to assign lambda function to some variable and call the lambda function using a variable. In general, we use lambda functions with higher-order functions and built-in functions like filter, map, reduce, etc.,

Lambda functions with normal functions:

def func(x):
    return lambda :x*x

a=func(4)
b=func(5)
print(a())
print(b())
16
25

In the above example, the function func() returns a lambda function. Whenever we call the func(), we make use of the lambda function.

lambda function with filter:

The filter function filters the iterable with the help of another function. The filter function takes the function to filter the iterable and iterable as an argument.

Syntax:

filter(function, iterable)

Example:

Let’s see an example to filter the even numbers from the list.

l=[7,4,9,2,3,8,5,12,10]
new_l=list(filter(lambda a:a%2==0,l))
print(new_l)
[4, 2, 8, 12, 10]

lambda function with map:

The map function applies a function to all the values in an iterable. The map function takes the function to be applied to the iterable and iterable as an argument.

Syntax:

map(function, iterable)

Example:

Get the even numbers from a list using the map function.

l=[12,14,3,9,15,25]
new_list = list(map(lambda x: (x%2 == 0), l))
print(new_list)
[True, True, False, False, False, False]

lambda function with reduce:

The reduce function applies a function to all the values of an iterable and returns a single number. The reduce function takes the function and iterable as an argument.

Syntax:

reduce(function, iterable)

Example:

Get the product of all the numbers in a list.

from functools import reduce

l=[12,14,3,9,15,25]
product=reduce((lambda x,y:x*y), l)
print(product)
1701000