What is lambda in Python?
Lambda, the 11th letter of the Greek alphabet , is the symbol for wavelength . Lambda comes from the Lambda Calculus and refers to anonymous functions in programming.
The general syntax of a lambda function is quite simple:
lambda argument_list: expression
The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments.
Here's an example. You can build a function in the normal way, using def, like this:
def square_root(x): return math.sqrt(x)
Using lambda:
square_root = lambda x: math.sqrt(x)
example
square_root = lambda x: x*x
print(square_root(2))
Lambda allows you to write quick throw away functions without naming them. It also provides a nice way to write closures also.
exampleFind the sum of two numbers using lambda
add = lambda x, y: x + y
print(add(10,20))
output
30
Lambda in Conditional expressions:
result = lambda x: "Bigger than 100" if x > 100 else "Smaller than 100"print(result(99))
output
Smaller than 100
Map example using lambda
my_list = [1, 2, 3, 4, 5, 6]
squared = map(lambda x: x**2, my_list)
print(list(squared))
output
[1, 4, 9, 16, 25, 36]
Filter example using lambda
my_list = [1, 3,5, 7, 9, 11, 13, 15]
new_list = list(filter(lambda x: (x%3 == 0) , my_list))
print(new_list)
output
[3, 9, 15]
Reduce example using lambda
from functools import reduce
result = reduce((lambda x, y: x * y), [1, 2, 3, 4,5])
print(result)
output
120
Calculating the sum of the numbers from 1 to 1000 using lambda
from functools import reduce
total = reduce(lambda x, y: x+y, range(1,1001))
print(total)
output
500500
Related Topics
- Python Interview Questions (Part 2)
- Python Interview Questions (Part 3)
- What is python used for?
- Is Python interpreted, or compiled, or both?
- Explain how python is interpreted
- How do I install pip on Windows?
- How do you protect Python source code?
- What are the disadvantages of the Python?
- How would you achieve web scraping in Python?
- How to Python Script executable on Unix
- What is the difference between .py and .pyc files?
- What is __init__.py used for in Python?
- What does __name__=='__main__' in Python mean?
- What is docstring in Python?
- What is the difference between runtime and compile time?
- How to use *args and **kwargs in Python
- Purpose of "/" and "//" operator in python?
- What is the purpose pass statement in python?
- Why isn't there a switch or case statement in Python?
- How does the ternary operator work in Python?
- What is the purpose of "self" in Python
- How do you debug a program in Python?
- What are literals in python?
- What is Python's parameter passing mechanism?
- What is the process of compilation and Loading in python?
- Global and Local Variables in Python
- Is there a tool to help find bugs or perform static analysis?
- What does the 'yield' keyword do in Python?
- Comparison Operators != is not equal to in Python
- What is the difference between 'is' and '==' in python
- What is the difference between = and == in Python?
- How are the functions help() and dir() different?
- What is the python keyword "with" used for?
- Is all the memory freed when Python exits?
- Difference between Mutable and Immutable in Python
- Explain split() methods of "re" module in Python
- Accessor and Mutator methods in Python
- How to Implement an 'enum' in Python
- Important characteristics of Python Objects
- How to determine the type of instance and inheritance in Python
- How would you implement inheritance in Python?
- How is Inheritance and Overriding methods are related?
- How can you create a copy of an object in Python?
- How to avoid having class data shared among instances in Python?
- Static class variables in Python
- Difference between @staticmethod and @classmethod in Python
- How to Get a List of Class Attributes in Python
- Does Python supports interfaces like in Java or C#?
- What is used to create Unicode string in Python?
- Difference between lists and tuples in Python?
- What are differences between List and Dictionary in Python
- Different file processing modes supported by Python
- How do you append to a file in Python?
- What are the differences between the threading and multiprocessing?
- Is there any way to kill a Thread in Python?
- What is map, filter and reduce in python?
- Is monkey patching considered good programming practice?
- What is "typeerror: 'module' object is not callable"
- Python: TypeError: unhashable type: 'list'
- How to convert bytes to string in Python?
- What are metaclasses in Python?