-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter 54 - Lambda.py
51 lines (36 loc) · 1.15 KB
/
Chapter 54 - Lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# CHAPTER 54
# Lambda Function = function written in 1 line using lambda keyword
# accepts any number of arguments, but only has one expression
# (think of it as a shortcut)
# (useful if needed for a short period of time, throw-away)
# lambda parameters : expression
# Long Method
# def double(x):
# return x * 2
# Shortcut using Lambda Function
double = lambda x : x * 2
print(double(5))
# Long Method
# def multiply(x, y):
# return x * y
# Shortcut using Lambda Function
multiply = lambda x, y : x * y
print(multiply(5, 3))
# Long Method
# def add(x, y, z):
# return x + y + z
# Shortcut using Lambda Function
add = lambda x, y, z : x + y + z
print(add(3, 6, 9))
# Long Method
# def full_name(first_name, last_name):
# return first_name + " " + last_name
# Shortcut using Lambda Function
full_name = lambda first_name, last_name : first_name + " " + last_name
print(full_name("Emmar", "Caber"))
# Long Method
# def age_check(age):
# return True if age >= 18 else False
# Shortcut Method using Lambda Function
age_check = lambda age : True if age >= 18 else False
print(age_check(18))