Tuesday, 22 March 2022

Python Visualizing λ

λ +

Python compact coding is very much possible with lambda function.

Anonymous function.. Function without name. 

# lambda arguments : expression

# using Lambda
result = lambda x,y : x + y
# print(result(5,5))

# otherwise by function
def add(x,y):
return (x+y)
add(5,5)

sorted() takes elements from the list and sort the elements. Please see how key is implemented using lambda function. This can be used for any list with numbers, strings, list of lists. For simple understanding, number list is given. keys are defined using lambda arguments.


# Difference Between function and lambda -anonymous function
def cube(x):
return (x*x*x)

lambda_cube = lambda x: x*x*x

print(cube(125))
print((lambda_cube(25))) # No function call or return

list_of_tuples = [(1, 2, 3, 4), (4, -3, 2, 1), (3, 4, -1, 2), (2, 3, 4, -1)]
list_of_tuples_sorted = sorted(list_of_tuples)
print(list_of_tuples_sorted)
"""
[(1, 2, 3, 4), (2, 3, 4, -1), (3, 4, -1, 2), (4, -3, 2, 1)]
"""

list_of_tuples_sorted = sorted(list_of_tuples, key =lambda x: x[1]) # Sort in second element of all tuples
print(list_of_tuples_sorted)
"""
[(4, -3, 2, 1), (1, 2, 3, 4), (2, 3, 4, -1), (3, 4, -1, 2)]
"""

list_of_tuples_sorted = sorted(list_of_tuples, key =lambda x: x[3]) # Sort in last element of all tuples
print(list_of_tuples_sorted)

"""
[(2, 3, 4, -1), (4, -3, 2, 1), (3, 4, -1, 2), (1, 2, 3, 4)] """
Map function is used to pass the arguments from the list. Literally mapping values..
# Using function map()
alist = [1, 2, 3, 4, 5]
mlist =
map(lambda x : x*x, alist) # map supplies elements instead of one element
print(list(mlist)) # [1, 4, 9, 16, 25]

# Otherwise using c-omprehensive list
clist = [x * x for x in alist]
print(clist) # [1, 4, 9, 16, 25]
#Using Filter to find even numbers in the list
flist = filter(lambda x: x%2 == 0, alist)
print(list(flist)) # [2, 4]

# Otherwise using c-omprehensive list
clist = [x for x in alist if x%2== 0]
print(clist) # [2, 4]
#Using reduce ....please import reduce from functools

def my_add(a, b):
   result = a + b
print(f"{a} + {b} = {result}")
return result

from functools import reduce
numbers = [0, 1, 2, 3, 4]
print(reduce(my_add, numbers))

# Note passing a,b and see the cumulative sum is first
element in the next iteration

0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10
"""
import functools

# list definition
alist = [1, 2, 7, 4, 5, 6, 3, ]

# educe to compute sum of list
print("The sum of the list elements is : ", end="")
print(functools.reduce(lambda a, b: a + b, alist)) #28

# reduce to compute maximum element from list
print("The maximum element of the list is : ", end="")
print(functools.reduce(lambda a, b: a if a > b else b, alist)) # 7

To understand the visualization the above reduce code execution: please click the  link https://pythontutor.com/visualize.html#mode=display Paste the above code in the editor window. Click visualize execution and next button. 

Step 1:


Step 2: Delete the first line (if any) as shown in the above screen shot. 
Paste the copied code as shown below


Step 3: Click Visualize Button to see the screen shot similar to the following one:


Step 4: Click Next Button to see the following 



Please note global frame with functools points with arrow to  objects module instance by arrow.

Step 5: Click Next button again to see as shown below



Notice the pointer from alist  1,2,7,4,5.  

Step 5: Click again to see the window similar to one shown below:



Please note the print out box in top right : First print out put 
'The sum of the list elements is seen  :'

Step 6:  Again Click Next  to see the window as shown below.


In this screen shot, please notice  lambda list with a =1 ,b=2 . Press Clicks to see the screen below to see the return value 3:



Step 7:.. Click Next many times until that button is disabled  to see execution until you get results 
The maximum element of the list is : 7  as shown in the screen below.


This is the video showing the execution.


Hope, you understand the execution of the python code. 

Hey, you know how to write concise coding using Lambda.. If you don't, sorry, God only save you!!!!😇

Happy Learning with AMET, ODL


No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...