Skip to content

Latest commit

 

History

History
42 lines (37 loc) · 908 Bytes

29. Unit 3 - Lesson 4.md

File metadata and controls

42 lines (37 loc) · 908 Bytes

Anonymous Functions

Q1. 1,2,4
Q2.

tax = lambda salary:salary*20/100
	
salary = int(input("Please enter your salary: "))
print("Tax to be paid is", tax(salary))

Q3.

doublenum=lambda x:(x)*2
x=int(input("a: "))
print(doublenum(x))

Q4.

def squares(x):
	return x ** 2
list1 = [1, 2, 3, 4, 5]
#use the squares func inside map function print the result
print(list(map(squares,list1)))


#use the lambda func inside map function print the result
print(list(map(lambda x:x**2,list1)))
#use list comprehension to get equivalent behaviour as map and print the result
print([x**2 for x in list1])

Q5.

a = [1, 2, 3, 5, 7, 9]
b = [2, 3, 6, 7, 9, 8]
 # apply lambda function to the filter function and print the result
print(list(filter(lambda x:x in a,b)))
print([x for x in a if x in b])
 
 # print the result using list comprehension
 
 # follow the example given in description