Skip to content

Commit 67dac98

Browse files
authored
Positional Arguments
1 parent 335830f commit 67dac98

File tree

1 file changed

+345
-0
lines changed

1 file changed

+345
-0
lines changed

Positional Arguments.ipynb

+345
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "11e148a4",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"def my_func(a, b, c):\n",
11+
" print(\"a={0}, b={1}, c={2}\".format(a, b, c))"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 2,
17+
"id": "458fc390",
18+
"metadata": {},
19+
"outputs": [
20+
{
21+
"name": "stdout",
22+
"output_type": "stream",
23+
"text": [
24+
"a=1, b=2, c=3\n"
25+
]
26+
}
27+
],
28+
"source": [
29+
"my_func(1, 2, 3)"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"id": "23f4ea96",
35+
"metadata": {},
36+
"source": [
37+
"## Default Values"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 3,
43+
"id": "46851f02",
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"def my_func(a, b=2, c=3):\n",
48+
" print(\"a={0}, b={1}, c={2}\".format(a, b, c))"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"id": "1cad99af",
54+
"metadata": {},
55+
"source": [
56+
"Note that once a parameter is assigned a default value, all parameters thereafter must be asigned a default value too!\n",
57+
"\n",
58+
"For example, this will not work:"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 4,
64+
"id": "f51f9f59",
65+
"metadata": {},
66+
"outputs": [
67+
{
68+
"ename": "SyntaxError",
69+
"evalue": "non-default argument follows default argument (3755199277.py, line 1)",
70+
"output_type": "error",
71+
"traceback": [
72+
"\u001b[1;36m Cell \u001b[1;32mIn[4], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m def fn(a, b=2, c):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-default argument follows default argument\n"
73+
]
74+
}
75+
],
76+
"source": [
77+
"def fn(a, b=2, c):\n",
78+
" print(a, b, c)"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 5,
84+
"id": "4e2043b6",
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"def my_func(a, b=2, c=3):\n",
89+
" print(\"a={0}, b={1}, c={2}\".format(a, b, c))"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 6,
95+
"id": "1d893fd0",
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"name": "stdout",
100+
"output_type": "stream",
101+
"text": [
102+
"a=10, b=20, c=30\n"
103+
]
104+
}
105+
],
106+
"source": [
107+
"my_func(10, 20, 30)"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 7,
113+
"id": "13848ec1",
114+
"metadata": {},
115+
"outputs": [
116+
{
117+
"name": "stdout",
118+
"output_type": "stream",
119+
"text": [
120+
"a=10, b=20, c=3\n"
121+
]
122+
}
123+
],
124+
"source": [
125+
"my_func(10, 20)"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 8,
131+
"id": "c6552429",
132+
"metadata": {},
133+
"outputs": [
134+
{
135+
"name": "stdout",
136+
"output_type": "stream",
137+
"text": [
138+
"a=10, b=2, c=3\n"
139+
]
140+
}
141+
],
142+
"source": [
143+
"my_func(10)"
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"id": "a1f916de",
149+
"metadata": {},
150+
"source": [
151+
"Since a does not have a default value, it must be specified:"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": 9,
157+
"id": "7248f6fd",
158+
"metadata": {},
159+
"outputs": [
160+
{
161+
"ename": "TypeError",
162+
"evalue": "my_func() missing 1 required positional argument: 'a'",
163+
"output_type": "error",
164+
"traceback": [
165+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
166+
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
167+
"Cell \u001b[1;32mIn[9], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mmy_func\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
168+
"\u001b[1;31mTypeError\u001b[0m: my_func() missing 1 required positional argument: 'a'"
169+
]
170+
}
171+
],
172+
"source": [
173+
"my_func()"
174+
]
175+
},
176+
{
177+
"cell_type": "markdown",
178+
"id": "066893e5",
179+
"metadata": {},
180+
"source": [
181+
"Keyword Arguments (named arguments)\n",
182+
"Positional arguments, can optionally, be specified using their corresponding parameter name.\n",
183+
"\n",
184+
"This allows us to pass the arguments without using the positional assignment:"
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": 10,
190+
"id": "ad1808f7",
191+
"metadata": {},
192+
"outputs": [],
193+
"source": [
194+
"def my_func(a, b=2, c=3):\n",
195+
" print(\"a={0}, b={1}, c={2}\".format(a, b, c))"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": 12,
201+
"id": "343a48fb",
202+
"metadata": {},
203+
"outputs": [
204+
{
205+
"name": "stdout",
206+
"output_type": "stream",
207+
"text": [
208+
"a=10, b=20, c=30\n"
209+
]
210+
}
211+
],
212+
"source": [
213+
"my_func(c=30, b=20, a=10)"
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": 13,
219+
"id": "7f75e039",
220+
"metadata": {},
221+
"outputs": [
222+
{
223+
"name": "stdout",
224+
"output_type": "stream",
225+
"text": [
226+
"a=10, b=20, c=30\n"
227+
]
228+
}
229+
],
230+
"source": [
231+
"my_func(10, c=30, b=20)"
232+
]
233+
},
234+
{
235+
"cell_type": "markdown",
236+
"id": "d24417f8",
237+
"metadata": {},
238+
"source": [
239+
"Note that once a keyword argument has been used, all arguments thereafter must also be named:"
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": 14,
245+
"id": "103530c8",
246+
"metadata": {},
247+
"outputs": [
248+
{
249+
"ename": "SyntaxError",
250+
"evalue": "positional argument follows keyword argument (3343971405.py, line 1)",
251+
"output_type": "error",
252+
"traceback": [
253+
"\u001b[1;36m Cell \u001b[1;32mIn[14], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m my_func(10, b=20, 30)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
254+
]
255+
}
256+
],
257+
"source": [
258+
"my_func(10, b=20, 30)"
259+
]
260+
},
261+
{
262+
"cell_type": "markdown",
263+
"id": "620e8932",
264+
"metadata": {},
265+
"source": [
266+
"However, if a parameter has a default value, it can be omitted from the argument list, named or not:"
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": 15,
272+
"id": "1487d510",
273+
"metadata": {},
274+
"outputs": [
275+
{
276+
"name": "stdout",
277+
"output_type": "stream",
278+
"text": [
279+
"a=10, b=2, c=30\n"
280+
]
281+
}
282+
],
283+
"source": [
284+
"my_func(10, c=30)"
285+
]
286+
},
287+
{
288+
"cell_type": "code",
289+
"execution_count": 16,
290+
"id": "9c9a93e1",
291+
"metadata": {},
292+
"outputs": [
293+
{
294+
"name": "stdout",
295+
"output_type": "stream",
296+
"text": [
297+
"a=30, b=2, c=10\n"
298+
]
299+
}
300+
],
301+
"source": [
302+
"my_func(a=30, c=10)"
303+
]
304+
},
305+
{
306+
"cell_type": "code",
307+
"execution_count": 17,
308+
"id": "948f2f97",
309+
"metadata": {},
310+
"outputs": [
311+
{
312+
"name": "stdout",
313+
"output_type": "stream",
314+
"text": [
315+
"a=30, b=2, c=10\n"
316+
]
317+
}
318+
],
319+
"source": [
320+
"my_func(c=10, a=30)"
321+
]
322+
}
323+
],
324+
"metadata": {
325+
"kernelspec": {
326+
"display_name": "Python 3 (ipykernel)",
327+
"language": "python",
328+
"name": "python3"
329+
},
330+
"language_info": {
331+
"codemirror_mode": {
332+
"name": "ipython",
333+
"version": 3
334+
},
335+
"file_extension": ".py",
336+
"mimetype": "text/x-python",
337+
"name": "python",
338+
"nbconvert_exporter": "python",
339+
"pygments_lexer": "ipython3",
340+
"version": "3.9.12"
341+
}
342+
},
343+
"nbformat": 4,
344+
"nbformat_minor": 5
345+
}

0 commit comments

Comments
 (0)