Examples of Function - 2

Example 1:

add = lambda x,y: x+y
r = add(6,8)
print(r)

14

Example 2:

great = lambda p,q: q if q > p else p
print(great(5,3))

5

Example 3:

fact = lambda num : 1 if num == 1 else num*fact(num-1)
print(fact(int(input("Enter a number : "))))

Enter a number : 34

295232799039604140847618609643520000000

Example 4:

x = input().split()
n = []
for var in x :
    n.append(int(var))
print(x)
print(n)

1 2 3 4 5 6 5 3 4

['1', '2', '3', '4', '5', '6', '5', '3', '4']

[1, 2, 3, 4, 5, 6, 5, 3, 4]

Example 6:

x = input().split()
y = map(int,x)
y = list(y)
print(y)

1 2 3 4 5 6 7 8 9 10

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Example 7:

x = list(map(int,input().split()))
print(x)

1 2 3 4 5 6 7 8 9 10

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Example 8:

x = list(map(int,input().split()))
y = list(map(int,input().split()))
print(list(map(lambda x,y:x+y,x,y))

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Example 9:

x = map(int,input().split(','))
print(list(map(lambda num:False if num % 2 else True,x)))

1,2,3,4,5,6,7,8,9

False True False True False True False True False

Example 10:

l = [ 1 ,2 ,3 ,4, 5, 6, 7,8 ,9 ,10]
r  = map(lambda x: True if x % 2 == 0 else False , l)
print(list(r))

False True False True False True False True False True

Example 11:

l = [ 1,2,3,4,5,6,7,8,9,10]
r = filter(lambda x:False if x % 2 == 0 else True,l)
print(*r)

1 3 5 7 9

User Login Authentication:

users = {"mohit" : "lock1", "sanyam" : "lock2"}

def login(username, password):
    if username in users and users[username] == password:
        print("Login Success")
    else:
        print("Login failed")

login("sanyam", "lock2")

Login Success

Last updated