words per minute

3

napomokoetle


00:00

Speed

# 1234567890- = 10`~ # +1_2)3(4*5&6 ^7%8$9#0@[!]{\}; : '>",.?./\ # Lambda functions, also known as anonymous functions, are small, unnamed functions defined using the lambda keyword. # The syntax is: lambda arguments: expression # Lambda functions can have any number of arguments but only one expression. # Here's a simple lambda function that takes two arguments and returns their sum: sum_lambda = lambda x, y: x + y # Test the lambda function: result = sum_lambda(5, 3) print(f"Sum of 5 and 3 is: {result}") # Outputs: 8 # Lambda functions are often used for short operations that can be encapsulated in a single expression. # One common use case is with the `sorted` function and its `key` argument. # Consider a list of tuples where each tuple contains a name and age: people = [("Alice", 30), ("Bob", 25), ("Charlie", 35), ("Dana", 28)] # To sort this list by age, you can use a lambda function: sorted_people = sorted(people, key=lambda person: person[1]) # Print sorted list: print(sorted_people) # Outputs: [('Bob', 25), ('Dana', 28), ('Alice', 30), ('Charlie', 35)] # Lambda functions are meant for simple operations. If the operation is complex, it's usually better to use a regular function. # Let's demonstrate using a lambda with the `filter` function: # We want to filter out people older than 28 young_people = filter(lambda person: person[1] <= 28, people) # Convert the filter object to a list and print it: print(list(young_people)) # Outputs: [('Alice', 30), ('Bob', 25), ('Dana', 28)] # Lambda functions are powerful for quick operations, especially in combination with functions like `sorted`, `filter`, and `map`. # 1234567890- = 10`~ # +1_2)3(4*5&6 ^7%8$9#0@[!]{\}; : '>",.?./\ # Defining a normal function to replace the lambda function for sum. def sum_function(x, y): return x + y # Test the normal function: result = sum_function(5, 3) print(f"Sum of 5 and 3 is: {result}") # Outputs: 8 # Consider a list of tuples where each tuple contains a name and age: people = [("Alice", 30), ("Bob", 25), ("Charlie", 35), ("Dana", 28)] # Defining a normal function to replace the lambda function for sorting by age. def age_key(person): return person[1] # Sorting the list by age using the normal function: sorted_people = sorted(people, key=age_key) # Print sorted list: print(sorted_people) # Outputs: [('Bob', 25), ('Dana', 28), ('Alice', 30), ('Charlie', 35)] # Defining a normal function to replace the lambda function for filtering people older than 28. def is_young(person): return person[1] <= 28 # Filtering out people using the normal function: young_people = filter(is_young, people) # Convert the filter object to a list and print it: print(list(young_people)) # Outputs: [('Alice', 30), ('Bob', 25), ('Dana', 28)]
words per minute
0
wpm
accuracy
0%
Text Practice - Time 2067 - English

words per minute