Home | Computer Science

Python lambda

  n Python, an anonymous function means that a function is without a name. As we already know that 
 def keyword is used to define the normal functions and the lambda
 keyword is used to create anonymous functions.

Python lambda Syntax:
lambda arguments : expression
Python lambda Example:

 

calc = lambda num: "Even number" if num % 2 == 0 else "Odd number" print(calc(20))

OUTPUT Even number

  Python lambda properties:
This function can have any number of arguments but only one expression, which is evaluated and returned.
One is free to use lambda functions wherever function objects are required.
You need to keep in your knowledge that lambda functions are syntactically restricted to a single expression.
It has various uses in particular fields of programming, besides other types of expressions in functions.
 Example 1: Program to demonstrate return type of Python lambda keyword

 

string = 'GeeksforGeeks' # lambda returns a function object print(lambda string: string)

OUTPUT <function <lambda> at 0x7fd7517ade18

Explanation: In this above example, the lambda is not being called by the print function, but simply returning the function object and the memory location where it is stored. So, to make the print to print the string first, we need to call the lambda so that the string will get pass the print.

Generator Object

  Python Generator functions return a generator object that is iterable, i.e., can be used as an Iterator. Generator objects are used either by calling the next method of the generator object or using the generator object in a “for in” loop.

Example:

In this example, we will create a simple generator function in Python to generate objects using the next() function.

 

# A Python program to demonstrate use of # generator object with next() # A generator function def simpleGeneratorFun(): yield 1 yield 2 yield 3 # x is a generator object x = simpleGeneratorFun() # Iterating over the generator object using next # In Python 3, __next__() print(next(x)) print(next(x)) print(next(x))

OUTPUT 1 2 3

 
  

   Example:

In this example, we will create two generators for Fibonacci Numbers, first a simple generator and second generator using a for loop.

 

# A simple generator for Fibonacci Numbers def fib(limit): # Initialize first two Fibonacci Numbers a, b = 0, 1 # One by one yield next Fibonacci Number while a < limit: yield a a, b = b, a + b # Create a generator object x = fib(5) # Iterating over the generator object using next # In Python 3, __next__() print(next(x)) print(next(x)) print(next(x)) print(next(x)) print(next(x)) # Iterating over the generator object using for # in loop. print("\nUsing for in loop") for i in fib(5): print(i)

OUTPUT 0 1 1 2 3 Using for in loop 0 1 1 2 3

Python Generator Expression

  In Python, generator expression is another way of writing the generator function. It uses the Python list comprehension technique but instead of storing the elements in a list in memory, it creates generator objects.

Generator Expression Syntax
The generator expression in Python has the following Syntax:
(expression for item in iterable)
Example:
In this example, we will create a generator object that will print 
the multiples of 5 between the range of 0 to 5 which are also divisible by 2.

 

# generator expression generator_exp = (i * 5 for i in range(5) if i%2==0) for i in generator_exp: print(i)

OUTPUT 0 10 20