0% found this document useful (0 votes)
10 views

First Class Functions and Lambda Expressions - Examples

Comp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

First Class Functions and Lambda Expressions - Examples

Comp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

First Class Functions and Lambda

Expressions - Examples

Q1.
def sum(numA, numB):
total = numA + numB
return total

temp = sum
print(temp(5,4))

Q2.
def func(x):
return x+1

f = func
print(f(2) + func(2))
Q3.

def sum(x,y):
total = x + y
return total
temp1 = sum

def sum(numA, numB):


total = numA + numB
return total

temp = sum

print(temp(5,4)+ temp1(4,3))

#
def sum(x,y):
total = x - y
return total
temp1 = sum

def sum(numA, numB):


total = numA + numB
return total

temp = sum

print(temp(5,4)+ temp1(4,3))
Q4.

Traditional style:
def divide(x, y):
return x / y

print(divide(15, 3))

Lambda style:

divide = lambda x, y: x / y
print(divide(15, 3))

#
result = (lambda x, y: x + y)(15, 3)
print(result)
Q5.
Traditional style:

def myVar(x):
return x**2

print(myVar(3))

Lambda style:
myVar = lambda x:x**2
print(myVar(3))

Q6.
Traditional style:
def to_uppercase(name):
return name.upper()

print(to_uppercase("Nick"))

Lambda style:
to_uppercase = lambda name :
name.upper()
print(to_uppercase("Nick"))
#
print((lambda name : name.upper())
("Mike"))

Q7.
Traditional style:
def x(a,b,c):
return a*b*c

print(x(2,2,2))

Lambda style:
x = lambda a, b, c : a*b*c
print(x(2, 2, 2))
QSS.
def myVar(n):
return lambda a : a / n

mydoubler = myVar(2)

print(mydoubler(11))

####

def myfunc(n):
return lambda a : a * n

mydoubler = myfunc(2)
mytripler = myfunc(3)

print(mydoubler(11))
print(mytripler(11))

Q8.
Traditional style:
def lowercaseof(anystring):
return anystring.lower()
names = ['Adams', 'Lim', 'Trump', 'Bob']
names.sort(key=lowercaseof)
print(names)

Lambda style:
names = ['Adams', 'Lim', 'Trump',
'Bob']
names.sort(key=lambda s: s.lower())
print(names)

Q9.
def myfunc(i):
return lambda a : a * i

x = myfunc(10)
print(x(2))
Q10.
Lambda style:
f= lambda a: "even" if a%2 ==0 else
"odd"

print(f(2))

Traditional style:
def f(a):
if a%2 == 0:
return "even"
else:
return "odd"

print(f(2))
Q11.
Lambda style:
number = lambda x : True if (x>=2 and
x<20) else False

print(number(5))

Traditional style:
def number(x):
if (x>=2 and x<20):
return True
else :
return False

print(number(x))

####

number = lambda x : True if (x>=2 and


x<20) else False
x = int(input("Choose a number:"))
print(number(x))

def number(x):
if (x>=2 and x<20):
return True
else :
return False

x = int(input("Choose a number:"))

print(number(x))

Q12.
Lambda style:
number = lambda y: y>5 and y< 25
print(number(2))
Traditional style:
def number(y):
return y>5 and y<25

print(number(2))

Q13.
Lambda style:
number = lambda x: "Two" if x%2 ==0
else "Three" if x %3 else "not 2, not 3 "
x = int(input("Choose a number:"))
print(number(x))

Traditional style:
def number(x):
if x%2 == 0:
return "Two"
elif x%3 == 0:
return "Three"
else :
return "not 2, not 3"
x = int(input("Choose a number:"))

print(number(x))

Q14.

def over_age(data, getter):


return getter(data) >= 18
user = { 'username': 'rolf123', 'age':
'35' }
print(over_age(user, lambda x:
int(x['age'])))

Q15.
Traditional style:
def f(a):
if len(a) > 3:
return a.upper()
else:
return a.lower()
a = input("Write your name: ")
print(f(a))

Lambda style:
f = lambda a: a.upper() if len(a)>3 else
a.lower()
a = input("Write your name: ")
print(f(a))

Q16.
print((lambda x: x if(x > 10) else 10)
(12))

Q17.
Lambda Style:
print((lambda x: x * 10 if x > 10 else (x *
5 if x < 5 else x))(11))

Traditional:

def check_conditions(x):
if x > 10:
return x * 10
elif x < 5:
return x * 5
else:
return x

print(check_conditions(11))
Game Question:

Q1.
def function(n):
a = 10
r=4
if n ==1:
return a
else:
return r * function(n-1)

for i in range(1,4):
print(function(i))
Q2.
Lambda:
a = int(input("Choose a number: "))
n = int(input("Choose a number: "))

def myfunc(n):
return lambda a : a *n

mydoubler = myfunc(n)

print(mydoubler(a))
Traditional:

a = int(input("Choose a number: "))


n = int(input("Choose a number: "))

def myfunc(n):
return f(a)

def f(a):
return a*n

print(myfunc(n))
Q3.
Lambda style:
def myf(x, fn):
return fn(x)

print(myf(3, lambda x: x+5 ))

Traditional style:

def myf(x):
return fn(x)

def fn(x):
return x+5

print(myf(3))

Q4.
Traditional style:
def average(sequence):
return sum(sequence) /
len(sequence)

students = [
{"name": "Rolf", "grades": (67, 90,
95, 100)},
{"name": "Bob", "grades": (56, 78,
80, 90)},
{"name": "Jen", "grades": (98, 90, 95,
99)},
{"name": "Anne", "grades": (100,
100, 95, 100)},
]

for student in students:


print(average(student["grades"]))
#Lambda style:

average = lambda sequence:


sum(sequence) / len(sequence)

students = [
{"name": "Rolf", "grades": (67, 90,
95, 100)},
{"name": "Bob", "grades": (56, 78,
80, 90)},
{"name": "Jen", "grades": (98, 90, 95,
99)},
{"name": "Anne", "grades": (100,
100, 95, 100)},
]

for student in students:


print(average(student["grades"]))

You might also like