Member-only story

Interview question: Explain Python’s lambda function

Crystal X
2 min readApr 13, 2023

--

Working in data science requires me to know a fair bit about Python programming. One function that is inbuilt in Python that I am familiar with is lambda.

The lambda function is an anonymous function that is written on one line of Python code. It can take any number of arguments, but return only one expression. These functions are subject to a more restrictive but more concise syntax than regular Python functions.

Some examples of how the lambda function can be used in Python are:-

  1. A lambda function with one argument,
  2. A lambda function with two argument,
  3. A lambda function with three arguments,
  4. A lambda function and its argument surrounded in parenthesis.

The first sample code is the lambda function with one argument, as shown below:-

The second sample code is a lambda function with two arguments, as shown below:-

The third sample code is a lambda function with three arguments, as shown below:-

The fourth sample code is a line of code where the lambda function is applied to the function to an argument by surrounding the function and its argument with parentheses, as shown in the code below:-

In summary, it is always a good idea to explore the different inbuilt functions in Python, and learn the different ways in which they can be written. Python is said to be a very forgiving language when it comes to the syntax, so it would be wise to experiment with different ways to code.

--

--

Crystal X
Crystal X

Written by Crystal X

I have over five decades experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.

Responses (1)