From c28f07767d606ea8bfdedd37fd631b65b51e3c5f Mon Sep 17 00:00:00 2001 From: Hassan-Abbas-SQA <49572299+HassanAbbas7357@users.noreply.github.com> Date: Mon, 20 Mar 2023 02:43:36 +0500 Subject: [PATCH] Create lambda_func.py In this PR i have tried to explain lambda functions with a couple of example --- samples/function/lambda_func.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 samples/function/lambda_func.py diff --git a/samples/function/lambda_func.py b/samples/function/lambda_func.py new file mode 100644 index 00000000..c528ec62 --- /dev/null +++ b/samples/function/lambda_func.py @@ -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)) \ No newline at end of file