GoConqr hat ein neues Design! Sag uns, wie es dir gefällt.

Notizen

Python Programming Notes

Sean Matthews Python - Programming Principal Support Engineer Englisch 1 Seite

Python Programming Notes

Inhalt der Notizen (1)

Seite 1

Lambda Functions

The following terms may be used interchangeably depending on the programming language type and culture:

Anonymous functions

Lambda functions

Lambda expressions

Lambda abstractions

Lambda form

Function literals

Assume I want to write a function that returns twice the number I pass it. We can define a user-defined function as follows:

def f(x): return x * 2 f(3)

>> 6

Now for a lambda function. We'll create it like this:

lambda x: x * 3

As I explained above, the lambda function does not have a return keyword. As a result, it will return the result of the expression on its own. The x in it also serves as a placeholder for the value to be passed into the expression. You can change it to whatever you want.

Now if you want to call a lambda function, you will use an approach known as immediately invoking the function. That looks like this:

(lambda x : x * 2)(3)

>> 6

The reason for this is that since the lambda function does not have a name you can invoke (it's anonymous), you need to enclose the entire statement when you want to call it.

When to use?

You should use the lambda function to create simple expressions. For example, expressions that do not include complex structures such as if-else, for-loops, and so on.

So, for example, if you want to create a function with a for-loop, you should use a user-defined function.

How to Use a Lambda Function with Iterables

An iterable is essentially anything that consists of a series of values, such as characters, numbers, and so on.

In Python, iterables include strings, lists, dictionaries, ranges, tuples, and so on. When working with iterables, you can use lambda functions in conjunction with two common functions: filter() and map().

The map, filter, sorted, and reversed functions are great examples of higher-order functions that can receive a function (or lambda) as an argument and do things accordingly.

Examples

Python Programming Notes

Good example exercise code

# 1) Sort the `people` list of dictionaries alphabetically based on the

# 'name' key from each dictionary using the `sorted` function and store

# the new list as `sorted_by_name`

people = [

{"name": "Kevin Bacon", "age": 61},

{"name": "Fred Ward", "age": 77},

{"name": "finn Carter", "age": 59},

{"name": "Ariana Richards", "age": 40},

{"name": "Victor Wong", "age": 74},

]

sorted_by_name = sorted(people, key=lambda people_d: people_d['name'].lower())

assert sorted_by_name == [

{"name": "Ariana Richards", "age": 40},

{"name": "finn Carter", "age": 59},

{"name": "Fred Ward", "age": 77},

{"name": "Kevin Bacon", "age": 61},

{"name": "Victor Wong", "age": 74},

], f"Expected sorted_by_name to equal '{sorted_by_name}' to equal '{[{'name': 'Ariana Richards', 'age': 40}, {'name': 'finn Carter', 'age': 59}, {'name': 'Fred Ward', 'age': 77}, {'name': 'Kevin Bacon', 'age': 61}, {'name': 'Victor Wong', 'age': 74}]}''"

# 2) Use the `map` function to iterate over `sorted_by_name` to generate a

# new list called `name_declarations` where each value is a string with

# `<NAME> is <AGE> years old.` where the `<NAME>` and `<AGE>` values are from

# the dictionaries.

name_declarations = list(map(lambda d: f"{d['name']} is {d['age']} years old", sorted_by_name))

assert name_declarations == [

"Ariana Richards is 40 years old",

"finn Carter is 59 years old",

"Fred Ward is 77 years old",

"Kevin Bacon is 61 years old",

"Victor Wong is 74 years old",

], f"Expected name_declarations to equal '{name_declarations}' to equal '{['Ariana Richards is 40 years old', 'finn Carter is 59 years old', 'Fred Ward is 77 years old', 'Kevin Bacon is 61 years old', 'Victor Wong is 74 years old']}'"

# 3) Combine the `filter` and `sorted` functions to iterate over `sorted_by_name` to generate a

# new list called `under_seventy` that only contains the dictionaries where the

# 'age' key is less than 70, sorting the list by age.

under_seventy = sorted(filter(lambda d: d['age'] < 70, sorted_by_name), key=lambda d: d['age'])

assert under_seventy == [

{"name": "Ariana Richards", "age": 40},

{"name": "finn Carter", "age": 59},

{"name": "Kevin Bacon", "age": 61},

], f"Expected under_seventy to equal '{under_seventy}' to equal '{[{'name': 'Ariana Richards', 'age': 40}, {'name': 'finn Carter', 'age': 59}, {'name': 'Kevin Bacon', 'age': 61}]}'"

~

Informationen

Beschreibung
Principal Support Engineer Python - Programming Notiz am Python Programming Notes, erstellt von Sean Matthews am 02/04/2023.
Erstellt von
Sean Matthews
Aufrufe
0
Bewertung

Feedback

Worum geht es?

Wie gefällt dir das neue Design?

Dein Eindruck