forked from CalebCurry/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10-functions.py
More file actions
270 lines (172 loc) · 6.75 KB
/
Copy path10-functions.py
File metadata and controls
270 lines (172 loc) · 6.75 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#We are going to wrap back around to lists and loops
#but first functions
#functions are the only way to build scalable code
########## HOW TO CREATE A FUNCTION ##########
#This one is simple, but we go on.
def greet():
print("hello")
print("Welcome, Caleb")
#invoke
greet()
#Can invoke numerous times:
greet()
greet()
#Benefits: We can save lines of code if function is large
#One source of truth. No repeat of code. Need changes?
#update one spot
#Can improve code readability
########## Arguments and Parameters ##########
#parameter is a variable used within a function
def greet(name): #name is a parameter
print("hello")
print("Welcome, " + name)
greet("Sabrina") #Sabrina is an argument (value passed in to parameter variable)
#We can also use variables as arguments
new_guy = "Carl"
greet(new_guy)
#This gives the value "Carl" to the name parameter
########## RETURN ##########
#We can return (exit) from a function in certain situations
def greet(name):
if name == "Claire":
return
else:
print("hello")
print("Welcome " + name)
greet("Claire") #NOPE!
greet("Clarice") #YES!
#This code can be simplified by removing the else:
def greet(name):
if name == "Claire":
return
print("hello")
print("Welcome " + name)
#The concern without an else is that the line after the if is announced regardless of name
#This works, however, because there is never a case where name == "Claire" that is not returned
#We only need else if we are not returning!
########## RETURN VALUES ##########
#instead of printing inside the function, we can use the function to generate a message
#We can print outside of the function on the caller side
#caller side --> Where we invoke the function
def greet(name):
if name == "Claire":
return "Go away"
return "Hello " + name + "! Welcome to my app."
returned = greet("Sal")
print(returned)
#We can use the returned value directly...
print(greet("Claire"))
########## DEFAULT ARGUMENTS ##########
#We may want to create a method that does not require an input
def greet(name="User"):
if name == "Claire":
return "Go away"
return "Hello " + name + "! Welcome to my app."
print(greet()) #No name? Default to User
########## MULTIPLE ARGUMENTS / PARAMETERS ##########
def greet(name="User", be_nice=False):
if be_nice:
return "Hello " + name + "! Welcome to my app."
return "Go away " + name
print(greet("Caleb", True))
print(greet("Caleb"))
########## KEYWORD ARGUMENTS ###########
#This will showcase how to skip any particular parameter
def greet(name="User", be_nice=False):
if be_nice:
return "Hello " + name + "! Welcome to my app."
return "Go away " + name
print(greet(be_nice=True))
#NOTE if a parameter does not have a default value, you must specify it
#Either in order without a name, or assigning by name
########## POSITIONAL OR KEYWORD ARGUMENTS ##########
#With the function we have defined, we can pass arguments by position or by keyword
def greet(name, be_nice):
if be_nice:
return "Hello " + name + "! Welcome to my app."
return "Go away " + name
print(greet("Positional", True)) #Positional
print(greet(be_nice=True, name="Keyword")) #Keyword (NOTICE ORDER!)
#Positional arguments must follow keyword arguments
#ERROR:
#print(greet(be_nice=True, "Keyword"))
#NOTE that we can invoke by position or keyword. We can restrict to either though.
#If we want our method to have to be invoked using position, we can do that
#We can also restrict to keyword only as well.
########## POSITIONAL-ONLY ARGUMENTS ##########
def greet(name, be_nice, /): #Anything before the / must be positional
if be_nice:
return "Hello " + name + "! Welcome to my app."
return "Go away " + name
print(greet("positional", False))
#ERROR because invoking with keyword:
#print(greet("Positional", be_nice=True))
#We can also put the / between arguments
def greet(name, /, be_nice):
if be_nice:
return "Hello " + name + "! Welcome to my app."
return "Go away " + name
print(greet("Positional", be_nice=True)) #This works now.
print(greet("Positional", True)) #This works too.
#We can invoke with be_nice being named or positional
#Doesn't add a lot of value but nice when we have lots of arguments
#ERROR because invoking name by keyword:
#print(greet(name="Keyword", be_nice=True))
########## KEYWORD-ONLY ARGUMENTS ##########
#Place a *, before any arguments that are keyword-only
def greet(name, /, *, be_nice ): #be_nice is keyword only
if be_nice:
return "Hello " + name + "! Welcome to my app."
return "Go away " + name
print(greet("Positional", be_nice=True)) #be_nice= is required
#ERROR because positional
#print(greet("Positional", True))
########## SPECIAL PARAMETER SUMMARY ##########
#We've learned about positional-only arguments, and keyword-only arguments
#And of course the arguments that can be either.
#Here is full (but contrived) example
def do_nothing(pos1, pos2, pos3, /, either1, either2, *, keyword1, keyword2, keyword3):
print("pos1", pos1)
print("pos2", pos2)
print("pos3", pos3)
print("either1", either1)
print("either2", either2)
print("keyword1", keyword1)
print("keyword2", keyword2)
print("keyword3", keyword3)
do_nothing(1, 2, 3, 4, either2=5, keyword3=6, keyword1=7, keyword2=8)
#A very helpful rule to remember the positioning of this junk
#You must always invoke a method passing positional first
#Because if you passed them later, the position would be janked up
#The either parameters must be in the middle because they can also be positional
#The keyword are always last because you can mix their order
########## LIST PARAMETERS / FUNCTION TO ITERATE LIST #########
def greet_all(people):
for person in people:
print("Hello " + person)
people = ["Caleb", "Josh", "Austin"]
greet_all(people)
########## UNLIMITTED ARGUMENTS / PACKING ##########
#What if you are working with individual pieces of data?
#We can modify the function to "pack" the data with *
def greet_all(*people):
for person in people:
print("Hello " + person)
greet_all("Caleb", "Josh", "Austin")
########## UNPACKING ##########
def print_info(name, age, email):
print(name + " is " + str(age) + ". Reached at " + email)
info = ["Caleb", 12, "email@email.com"]
print_info(*info)
########## FUNCTIONS CALLING FUNCTIONS ##########
#Let's say we have this method and no way to edit it
def greet(name, be_nice=True):
if be_nice:
return "Hello " + name + "! Welcome to my app."
return "Go away " + name
#We can utilize it.
def greet_all(people):
for person in people:
print(greet(person)) #because it returns string
people_to_greet = ["Johnny", "Jimmy", "Jacob"]
greet_all(people_to_greet)