In Python we have many built-in functions like print() , len() but we can define our own reusable functions which can perform user defined set of operations.

How to define a function:

  • function definition starts with def keyword and followed by function name & ()
  • Within the () we write the input parameters.
  • Followed by : and indentation , we should avoid wrong indentation to avoid code error.
  • end with an optional return statement to return the values from where the function was called , this also exists the function.

Below is an example in the form of code.

Lambda Expression/Functions (Anonymous Functions): 

  • This type functions does not have any names and does not start with def keyword.
  • Lambda functions takes any number of arguments and returns only one value in the form of expression.
  • This function will have only one line.
  • Syntax – lambda arguments: expression
  • Lambda functions are used in combination with in built map & filter functions.
  • Also used in sorting operations.

Now lets look at examples in code & see how to use Lambda expression/functions –

In Maps and filters , we can use other in built or user defined functions.

Tagged with:
 

Leave a Reply