Mastering Python: Part 1: Lambda

Learning Python

Using Python

Mastering Python

Python is a general-purpose interpreted programming language. It emphasizes readability through the use of whitespace and supports multiple programming paradigms such as functional and object-oriented models.


Lambda

Usually, functions have names in Python. Through using the def keyword, a function can be named and then called using its name.

Beyond the normal named function, there also exists a type of anonymous function in Python called a lambda.

A lambda is created using the keyword and then as many arguments as wanted. The list of the data sent into the special function stops at a colon. After this, there can only be a single expression statement.

In Python, a lambda is written using a single line. Unlike other functions where lines are “under” (spaced away from the left margin), a lambda only ever has a single expression statement as part of it.

With filter()

While lamdba may seem to be used in mathematical contexts, it can also be paired with the function filter() to parse something like a List.

As a type of function, a lambda can be written as part of working with a filter() usage to test as a series of entries. As a function with a single expression statement that returns a result, it can be used as a replacement for written a new function.

Play with the example on Repl.it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.