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)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.