Python Lambda Tutorial

Lambda expressions are a powerful feature in the Python programming language. However, they can be a little intimidating at first glance, especially since they have no direct equivalent in languages like Java that most programming courses are taught in. These expressions allow the programmer to define unnamed functions on the fly and assign them to a variable name. You can create an array of functions with lambda and assign them all to a single variable name.

Instructions

    • 1

      Define a function with the lambda keyword and assign it to the "sum" variable name.

      >>> sum = lambda x,y: x+y

      Test to see if your lambda function works with the following command:

      >>> sum(2,2)
      4

      This shows the absolute minimum for lambda, but you can do a little more with it.

    • 2

      Define more than one lambda function to a single variable.

      >>> operation = { 'sum':lambda x,y:x+y, 'sub':lambda x,y:x-y, 'mul':lambda x,y:x*y, 'div':lambda x,y:x/y }
      >>> operation['sum'] (2,2)
      4
      >>> operation['mul'] (2,4)
      8
      >>> operation['div'] (4,4)
      1
      >>> operation['sub'] (4,1)
      3

      That still doesn't reveal one of the most stunning features of lambda. In fact, more than any other feature, this is the one that generates the most excitement about lambda functions in older languages like LISP which supported it.

    • 3

      Use lambda to, in a single line, perform an operation on every element on a list and return a new list containing the results of the operation.

      >>> alist = [ 0, 1, 2, 3, 4 ]
      >>> map(lambda x: x*6, alist)
      [0, 6, 12, 18, 24]

      For another example, to find the lengths of all the words in a string, use the following lambda function:

      >>> string = "It was a very long day at the office, but when I come home to you, it is all worthwhile."
      >>> map(lambda x: len(x), string.split())
      [2, 3, 1, 4, 4, 3, 2, 3, 7, 3, 4, 1, 4, 4, 2, 4, 2, 2, 3, 11]

      This allows a programmer to do, in one short and easy to read line, what a Java programmer would require four or five lines to achieve.

Related Searches:

References

Comments

You May Also Like

  • How to Multiply All Elements in a List With Python

    One of the nice features common to many of the latest programming languages like Python is support for lambda functions and functional...

  • How to Create an Array in Python

    Arrays are useful and fundamental structures that exist in every high-level language. In Python, arrays are native objects called "lists," and they...

  • How to Declare a List in Python

    Python is a popular interpreted scripting language that provides a deep set of libraries and modules allowing software developers to quickly create...

  • What Is Lambda Max?

    Lambda max is the wavelength at which the maximum fraction of light is absorbed by a solution. Lambda (λ) is a Greek...

  • How to Send a HTML Email With Python

    Python is an open-source programming language that is useful for building a variety of applications. This programming language is popular with developers...

  • What Is a Lambda Sensor?

    Most people don't take into account how much maintenance is required to keep a car running properly. They take care of things...

  • How to Identify Types of Pythons and Boas

    Because boa constrictors, reticulated pythons and anacondas are some of the biggest snakes in the world, many people get confused about which...

  • How to Call the Default Constructor in Java

    When you create a new class in Java, the default constructor takes no parameters. To call this default constructor, you call the...

  • Types of Python Snakes

    Types of Python Snakes. Like all varieties of snake, members of the python family are cold-blooded and rely on their tongues for...

  • How to Create a Chart in Python

    Displaying data in the form of charts or graphs in Python requires the use of specialized, external code libraries, and the open-source...

  • How to Convert a List to an Array in Python

    Python -- the high-level programming language used by such organizations as Google and NASA -- has the usual kinds of data types:...

  • Lambda Life Cycle

    Bacteriophages are viruses that infect bacteria. Lambda bacteriophage is a particular type of phage that infects e. coli, a bacterium used for...

  • How to Solve a Wooden Snake Puzzle

    Draw axis X, Y and Z on each of your sticky notes to remind you of all three directions that you may...

Related Ads

Featured