Python Lambda

Python Lambda

Is a function that take as many arguments as possible but can only have one expression. They are also consider anonymous as they have no name. The expression will be executed and the result returned.

Syntax

lambda arguments : expression        

Example: lets add 100 to an argument in lambda as follow

y = lambda x : x + 100

print(y(17))         

Example: lets multiply two argument as follow

a = lambda x, y : x * y

print(a(4, 12))         

Let see how lambda and normal function will find the cube of a number:

def cube(y):
    return y*y*y

lambda_cube = lambda y: y*y*y

print("Using function defined with def keyword, cube:", cube(5))
print("Using lambda function, cube:", lambda_cube(5))        

Python Lambda Function with if-else

Here we are using the Max lambda function to find the maximum of two integers.

max = lambda a, b : a if(a > b) else b

print(max(1, 2))        

Difference Between Lambda functions and def defined function

Using lambda function

  • Supports single-line sometimes statements that return some value.
  • Good for performing short operations/data manipulations.
  • Using the lambda function can sometime reduce the readability of code.

def defined function

  • Supports any number of lines inside a function block
  • Good for any cases that require multiple lines of code.
  • We can use comments and function descriptions for easy readability.

Why Use Lambda Functions?

The power of lambda is better shown when you use them as an anonymous function inside another function.

Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number:

def myfunc(n):
  return lambda a : a * n         

Use that function definition to make a function that always doubles the number you send in:

def myfunc(n):
  return lambda a : a * n

mydoubler = myfunc(2)

print(mydoubler(11))        

Or, use the same function definition to make a function that always triples the number you send in:

def myfunc(n):
  return lambda a : a * n

mytripler = myfunc(3)

print(mytripler(11))        

Or, use the same function definition to make both functions, in the same program:

def myfunc(n):
  return lambda a : a * n

mydoubler = myfunc(2)
mytripler = myfunc(3)

print(mydoubler(11))
print(mytripler(11))        


要查看或添加评论,请登录

Abdulmutalib Idris的更多文章

  • Improving Nigerian Education Through School Management Systems

    Improving Nigerian Education Through School Management Systems

    The Nigerian education sector faces numerous challenges, particularly at the higher institution level. These include…

  • Python Functions

    Python Functions

    A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a…

    2 条评论
  • Python Conditional and Loop Statements

    Python Conditional and Loop Statements

    If ..

    1 条评论
  • Python Datatype - Part 5 (Dictionary)

    Python Datatype - Part 5 (Dictionary)

    As we learnt earlier Dictionary is one of 4 built-in data types in Python used to store collections of data, the other…

    2 条评论
  • Python Datatype - Part 4 (Sets)

    Python Datatype - Part 4 (Sets)

    Python Sets As we learnt earlier Set is one of 4 built-in data types in Python used to store collections of data, the…

    1 条评论
  • Python Datatype - Part 3 (Tuples)

    Python Datatype - Part 3 (Tuples)

    Python Tuples As we learnt in the last class there are datatype use to store multiple items in single variable. Python…

  • Python Datatype - Part 2 (Lists)

    Python Datatype - Part 2 (Lists)

    Python Lists There are datatype use to store multiple items in single variable. Python have 4 built-in datatype that…

    1 条评论
  • Python Datatype - Part 1

    Python Datatype - Part 1

    In the last class we talked about variables and we said variable data type is whatever values are assigned to the…

  • Python Variables

    Python Variables

    In python variables are the reserved memory locations used for storing values. In python a built-in id() function…

  • Python Indentation, Multi-line Statements, and Quotations

    Python Indentation, Multi-line Statements, and Quotations

    Python Indentation The spaces left at the beginning of a code line is called indentation. In other languages indenting…

    1 条评论

社区洞察

其他会员也浏览了