-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfunction_example.py
More file actions
127 lines (103 loc) · 2.08 KB
/
function_example.py
File metadata and controls
127 lines (103 loc) · 2.08 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def my_function() -> None:
"""
No argument, No Returns function.
>>> my_function()
hi
"""
print("hi")
def add(a: int, b: int) -> int:
"""
Add of a and b.
>>> add(3, 3)
6
>>> add(3, -3)
0
>>> add(99, 1)
100
"""
return a + b
def add_more(*args):
"""
Arbitrary Arguments
>>> add_more(1, 2, 3)
6
"""
return sum(args)
def calculate(a: int, operator: str, b: int) -> int:
"""
Simple calculator.
>>> calculate(a = 3, operator = "+", b = 4)
7
>>> calculate(a = 3, operator = "-", b = 4)
-1
>>> calculate(a = 3, operator = "*", b = 4)
12
>>> calculate(a = 3, operator = "/", b = 4)
0
>>> calculate(a = 3, operator = "!", b = 4)
Traceback (most recent call last):
...
ValueError: Invalid operator
"""
if operator == "+":
return a + b
elif operator == "-":
return a - b
elif operator == "*":
return a * b
elif operator == "/":
return a // b
else:
raise ValueError("Invalid operator")
def intro(**kwargs):
"""
>>> intro(name="Python", age = 29)
key=name, value=Python
key=age, value=29
"""
for (
key,
value,
) in kwargs.items():
print(f"key={key}, value={value}")
def print_number(number: int = 5) -> int:
"""
Default parameter.
>>> print_number(100)
100
>>> print_number()
5
"""
print(number)
def get_max(array):
"""
Pass list as parameter.
>>> get_max([1, 3, 5, 7, 9])
9
"""
max_value = array[0]
for item in array:
if item > max_value:
max_value = item
return max_value
def pass_function():
"""
>>> pass_function()
"""
pass # do anything.
def factorial(n: int) -> int:
"""
Recursion function.
>>> factorial(0)
1
>>> factorial(1)
1
>>> factorial(5)
120
>>> factorial(6)
720
"""
return 1 if n == 0 or n == 1 else n * factorial(n - 1)
if __name__ == "__main__":
from doctest import testmod
testmod()