14.06.2022 17.
Lambda Functions in Python - Jupyter
10:53 Notebook
Python Tutorial
Created by Mustafa Germec, PhD
17. Lambda Functions in Python
A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one
expression. The expression is evaluated and returned.
Lambda functions can be used wherever function objects are required.
Lambda expressions (or lambda functions) are essentially blocks of code that can be assigned
to variables, passed as an argument, or returned from a function call, in languages that support
high-order functions.
They have been part of programming languages for quite some time.
The main role of the lambda function is better described in the scenarios when we employ them
anonymously inside another function.
In Python, the lambda function can be utilized as an argument to the higher order functions as
arguments.
localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/17. Lambda
Fun… 1/
14.06.2022 17. Lambda Functions in Python - Jupyter
10:53 Notebook
In [1]:
1 # Define a function using 'def'
2 def f(x):
3 return x + 6
4 print(f(3.14))
5
6 # Define the same function using 'lambda'
7 f = lambda x: x+6
8 print(f(3.14))
9.14
9.14
In [2]:
1 # Define a function using 'def'
2 def f(x, y):
3 return x + y
4
5 print('The sum of {} and {} is'.format(3.14, 2.718), f(3.14, 2.718))
6
7 # Define the same function using 'lambda
8 f = lambda x, y: x+y
9 print(f'The sum of pi number and euler number is {f(3.14, 2.718)}.')
The sum of 3.14 and 2.718 is 5.8580000000000005
The sum of pi number and euler number is 5.8580000000000005.
localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/17. Lambda
Fun… 2/
14.06.2022 17. Lambda Functions in Python - Jupyter
10:53 Notebook
In [3]:
1 # Calculate the volume of a cube using def and lambda functions
2 # def function
3 def cube_volume_def(a):
4 return a*a*a
5
6 print(f'The volume of a cube using def function is {cube_volume_def(3.14)}.')
7
8 # lambda function
9 cube_volume_lambda = lambda a: a*a*a
10 print(f'The volume of a cube using lambda function is {cube_volume_lambda(3.14)}.')
The volume of a cube using def function is 30.959144000000002.
The volume of a cube using lambda function is
30.959144000000002.
Multiplication table
In [16]:
1 def mult_table(n):
2 return lambda x:x*n
3
4 n = int(input('Enter a number: '))
5 y = mult_table(n)
6
7 print(f'The entered number is {n}, which is a perfect number.')
8 for i in range(11):
9 print(('%d x %d = %d' %(n, i, y(i))))
The entered number is 6, which is a perfect number.
6x0=0
6x1=6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
filter()
In [5]:
1 # This program returns a new list when the special numbers in the list are divided by 2 and the remainder is equal to
0
2 special_nums = [0.577, 1.618, 2.718, 3.14, 6, 28, 37, 1729]
3 new_list = list(filter(lambda x:(x%2==0), special_nums))
4 new_list
Out[5]:
[6, 28]
localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/17. Lambda
Fun… 3/
14.06.2022 17. Lambda Functions in Python - Jupyter
10:53 Notebook
In [15]:
lambda function with if/else
In [8]:
1 age = int(input('Enter an age: '))
2 print(f'The entered age is {age}.')
3 vote = lambda age: print('Therefore, you can use a vote.') if (age>=18) else print('Therefore, you do not use a vote.')
4 vote(age)
The entered age is 18.
Therefore, you can use a vote.
localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/17. Lambda
Fun… 4/