introlambda.py

Created by ews31415

Created on September 06, 2021

602 Bytes

Demonstration of the lambda, filter, and map commands.


from math import *
from random import *
# lambda test

n=randint(10,9999)
tens=lambda x:int(x/10)%10
hunds=lambda x:int(x/100)%10
thous=lambda x:int(x/1000)%10

print(n)
print(tens(n))
print(hunds(n))
print(thous(n))

print("List:")
l1=[1,2,3,4,5,6]
print(l1)

print("Filter demonstation")
print("Greater than 3")
l2=list(filter(lambda x:x>3,l1))
print(l2)
print("Odd numbers")
l3=list(filter(lambda x:x%2!=0,l1))
print(l3)

print("Map demonstation")
print("Triple the numbers")
l4=list(map(lambda x:3*x,l1))
print(l4)
print("exp(x)-1")
l5=list(map(lambda x:exp(x)-1,l1))
print(l5)