-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
112 lines (61 loc) · 1.58 KB
/
functions.py
File metadata and controls
112 lines (61 loc) · 1.58 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
# global variable
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
def myfuncglobal():
global x
x = "fantastic"
myfunc()
myfuncglobal()
print("Python is " + x)
# unlimited arguments
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Emil", "Tobias", "Linus")
# named arguments
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1="Emil", child2="Tobias", child3="Linus")
# unlimited named parameters
def my_function(**kid): # **kwargs
print("His last name is " + kid["lname"])
my_function(fname="Tobias", lname="Refsnes")
# default parameter value
def my_function(country="Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
# return values
def my_function(g):
return 5 * g
print(my_function(3))
print(my_function(5))
print(my_function(9))
# recursion
def tri_recursion(k):
print("k:" + str(k))
if k > 0:
output = tri_recursion(k - 1)
print("output:" + str(output))
result = k + output
print("result:" + str(result))
else:
result = 0
print("result:" + str(result))
return result
print("\n\nRecursion Example Results")
tri_recursion(2)
# scope for nested functions
def myfunc():
x = 300 # The local variable can be accessed from a function within the function:
def myinnerfunc():
print(x)
myinnerfunc()
myfunc()