-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path014_functions.py
More file actions
50 lines (31 loc) · 813 Bytes
/
Copy path014_functions.py
File metadata and controls
50 lines (31 loc) · 813 Bytes
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
# functions!
def get_sum(a, b):
return a + b
print(get_sum(1, 2))
# if you dont know how many values you're gonna get
# just use *args
# remember to put it last
# ex: get_sum(a, b, *args)
def get_sum_2(*args):
sum = 0
for arg in args:
sum += arg
return sum
print(get_sum_2(1, 2, 3, 4, 5))
# get both values
def next_2(nums):
return num + 1, num + 2
ret_1, ret_2 = next_2(5)
print(ret_1, ret_2)
# create a function that returns a function
def mult_by(num):
return lambda x: x * num
print(" 3 * 5 =", (mult_by(3)(5)))
# pass a function to a function
def mult_list(list, func):
for x in list:
print(func(x))
mult_by_4 = mult_by(4)
mult_list(list(range(0, 5)), mult_by_4)
# you can create a list of function
power_list = [lambda x: x**2, lambda x: x**3]