Python lambda function basic usage Example Analysis, pythonlambda

Source: Internet
Author: User

Python lambda function basic usage Example Analysis, pythonlambda

This article describes the basic usage of Python lambda functions. We will share this with you for your reference. The details are as follows:

Here we will briefly learn about the python lambda function.

First, let's take a look at the syntax of the python lambda function as follows:

F = lambda [parameter1, parameter2,…] : Expression

In a lambda statement, there are 0 or more parameters before the colon, which are separated by commas. the return value is displayed on the right side of the colon. A lambda statement is actually a function object.

1 No Parameters

F = lambda: 'python lambda! '>>> F <function <lambda> at 0x06BBFF30 >>> f ()' python lambda! '

2. There are parameters and no default values

F = lambda x, y: x + y >>> f (3, 4) 7

3. There are parameters and default values.

F = lambda x = 2, y = 8: x + y >>>> f <function <lambda> at 0x06C51030 >>>> f () # x takes the default value 2, y takes the default value 810> f (1) # x takes 1, y takes the default value 89> f () # x, y all values are 36

4. The function returned by lambda can also be used as a parameter of another function.

Sumxy = lambda x, y: x + ydef test (f, m, n): print f (m, n) >>> sumxy (4, 5) 9 >>> test (sumxy, 4,5) 9

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.