-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathChapter_7th.py
197 lines (135 loc) · 2.78 KB
/
Chapter_7th.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Example 1
def my_function():
print("Hello from a function")
my_function()
# Output
# Hello from a function
# Example 2
def my_function(fname):
print(fname + " Robert")
my_function("Emily")
my_function("John")
my_function("Julia")
# Output
# Emily Robert
# John Robert
# Julia Robert
# Example 3
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emily", "Robert")
# Output
# Emily Robert
# Example 4
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emily")
# Output
# TypeError: my_function() missing 1 required positional argument: 'lname'
# Example 5
def my_function(country = "Norway"):
print("I am from " + country)
my_function("Pakistan")
my_function("Sweden")
my_function()
my_function("China")
# Output
# I am from Pakistan
# I am from Sweden
# I am from Norway
# I am from China
# Example 6
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Emily", "John", "Julia")
# Output
# The youngest child is Julia
# Example 7
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emily", child2 = "John", child3 = "Julia")
# Output
# The youngest child is Julia
# Example 8
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child3 = "Julia", child2 = "John",child1 = "Emily" )
# Output
# The youngest child is Julia
# Example 9
def my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname = "John", lname = "Robert")
# Output
# His last name is Robert
# Example 10
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
# Output
# apple
# banana
# cherry
# Example 11
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
# Output
# 15
# 25
# 45
# Example 12
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k - 1)
print(result)
else:
result = 0
return result
print("\n\nRecursion Example Results")
tri_recursion(6)
# Output
# Recursion Example Results
# 1
# 3
# 6
# 10
# 15
# 21
# Example 13
x = lambda a: a + 10
print(x(5))
# Output
# 15
# Example 14
x = lambda a, b: a * b
print(x(5, 6))
# Output
# 30
# Example 15
x = lambda a, b, c: a + b + c
print(x(5, 6, 2))
# Output
# 13
# Example 16
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11))
# Output
# 22
mytripler = myfunc(3)
print(mytripler(11))
# Output
# 33
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))
# Output
# 22
# 33