Skip to content

Commit ad75202

Browse files
committed
python asterisk argument
1 parent 52ecdd9 commit ad75202

File tree

1 file changed

+385
-0
lines changed

1 file changed

+385
-0
lines changed
Lines changed: 385 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,385 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# What is the usage of * - asterisk in Python\n",
8+
"\n",
9+
"* For multiplication and power operations.\n",
10+
"* Extending collections\n",
11+
"* Unpacking\n",
12+
"* positional arguments and keyword arguments"
13+
]
14+
},
15+
{
16+
"cell_type": "markdown",
17+
"metadata": {},
18+
"source": [
19+
"## For multiplication and power operations."
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 1,
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"data": {
29+
"text/plain": [
30+
"30"
31+
]
32+
},
33+
"execution_count": 1,
34+
"metadata": {},
35+
"output_type": "execute_result"
36+
}
37+
],
38+
"source": [
39+
"5 * 6"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 2,
45+
"metadata": {},
46+
"outputs": [
47+
{
48+
"data": {
49+
"text/plain": [
50+
"4"
51+
]
52+
},
53+
"execution_count": 2,
54+
"metadata": {},
55+
"output_type": "execute_result"
56+
}
57+
],
58+
"source": [
59+
"2 ** 2"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": 3,
65+
"metadata": {},
66+
"outputs": [
67+
{
68+
"ename": "SyntaxError",
69+
"evalue": "invalid syntax (<ipython-input-3-51a6a06f7259>, line 1)",
70+
"output_type": "error",
71+
"traceback": [
72+
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-3-51a6a06f7259>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 2 *** 2\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
73+
]
74+
}
75+
],
76+
"source": [
77+
"2 *** 2"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 4,
83+
"metadata": {},
84+
"outputs": [
85+
{
86+
"data": {
87+
"text/plain": [
88+
"'aaaaa'"
89+
]
90+
},
91+
"execution_count": 4,
92+
"metadata": {},
93+
"output_type": "execute_result"
94+
}
95+
],
96+
"source": [
97+
"'a' * 5"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 5,
103+
"metadata": {},
104+
"outputs": [
105+
{
106+
"data": {
107+
"text/plain": [
108+
"'ffffff'"
109+
]
110+
},
111+
"execution_count": 5,
112+
"metadata": {},
113+
"output_type": "execute_result"
114+
}
115+
],
116+
"source": [
117+
"'fff' * 2"
118+
]
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"## Extending collections"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 6,
130+
"metadata": {},
131+
"outputs": [
132+
{
133+
"data": {
134+
"text/plain": [
135+
"[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"
136+
]
137+
},
138+
"execution_count": 6,
139+
"metadata": {},
140+
"output_type": "execute_result"
141+
}
142+
],
143+
"source": [
144+
"[0] * 20 "
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 7,
150+
"metadata": {},
151+
"outputs": [
152+
{
153+
"data": {
154+
"text/plain": [
155+
"[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]"
156+
]
157+
},
158+
"execution_count": 7,
159+
"metadata": {},
160+
"output_type": "execute_result"
161+
}
162+
],
163+
"source": [
164+
"[0, 1 , 2] * 5"
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": 8,
170+
"metadata": {},
171+
"outputs": [
172+
{
173+
"data": {
174+
"text/plain": [
175+
"[[0, 1, 2], [3], [0, 1, 2], [3]]"
176+
]
177+
},
178+
"execution_count": 8,
179+
"metadata": {},
180+
"output_type": "execute_result"
181+
}
182+
],
183+
"source": [
184+
"[[0, 1 , 2], [3]] * 2"
185+
]
186+
},
187+
{
188+
"cell_type": "markdown",
189+
"metadata": {},
190+
"source": [
191+
"## Unpacking"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": 9,
197+
"metadata": {},
198+
"outputs": [
199+
{
200+
"data": {
201+
"text/plain": [
202+
"[1, 3, 5, 7, 9]"
203+
]
204+
},
205+
"execution_count": 9,
206+
"metadata": {},
207+
"output_type": "execute_result"
208+
}
209+
],
210+
"source": [
211+
"odds = [1, 3, 5, 7, 9]\n",
212+
"*x, = odds\n",
213+
"x"
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": 10,
219+
"metadata": {},
220+
"outputs": [
221+
{
222+
"data": {
223+
"text/plain": [
224+
"[1, 3, 5, 7]"
225+
]
226+
},
227+
"execution_count": 10,
228+
"metadata": {},
229+
"output_type": "execute_result"
230+
}
231+
],
232+
"source": [
233+
"*x,y = odds\n",
234+
"x"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": 11,
240+
"metadata": {},
241+
"outputs": [
242+
{
243+
"data": {
244+
"text/plain": [
245+
"9"
246+
]
247+
},
248+
"execution_count": 11,
249+
"metadata": {},
250+
"output_type": "execute_result"
251+
}
252+
],
253+
"source": [
254+
"y"
255+
]
256+
},
257+
{
258+
"cell_type": "code",
259+
"execution_count": 12,
260+
"metadata": {},
261+
"outputs": [],
262+
"source": [
263+
"x, *y, z = odds"
264+
]
265+
},
266+
{
267+
"cell_type": "code",
268+
"execution_count": 13,
269+
"metadata": {},
270+
"outputs": [
271+
{
272+
"data": {
273+
"text/plain": [
274+
"[3, 5, 7]"
275+
]
276+
},
277+
"execution_count": 13,
278+
"metadata": {},
279+
"output_type": "execute_result"
280+
}
281+
],
282+
"source": [
283+
"y"
284+
]
285+
},
286+
{
287+
"cell_type": "code",
288+
"execution_count": 14,
289+
"metadata": {},
290+
"outputs": [
291+
{
292+
"name": "stdout",
293+
"output_type": "stream",
294+
"text": [
295+
"(1, 3, 5, 7, 9)\n",
296+
"([1, 3, 5, 7, 9],)\n"
297+
]
298+
}
299+
],
300+
"source": [
301+
"odds = [1, 3, 5, 7, 9]\n",
302+
"\n",
303+
"def sum_all(*numbers):\n",
304+
" print(numbers)\n",
305+
"\n",
306+
"sum_all(*odds)\n",
307+
"\n",
308+
"sum_all(odds)"
309+
]
310+
},
311+
{
312+
"cell_type": "markdown",
313+
"metadata": {},
314+
"source": [
315+
"## positional arguments and keyword arguments"
316+
]
317+
},
318+
{
319+
"cell_type": "code",
320+
"execution_count": 15,
321+
"metadata": {},
322+
"outputs": [
323+
{
324+
"name": "stdout",
325+
"output_type": "stream",
326+
"text": [
327+
"('x', 'y', 'z', 'w', 'v')\n"
328+
]
329+
}
330+
],
331+
"source": [
332+
"def print_all(*args):\n",
333+
" print(args) \n",
334+
"print_all('x', 'y', 'z', 'w', 'v')"
335+
]
336+
},
337+
{
338+
"cell_type": "code",
339+
"execution_count": 16,
340+
"metadata": {},
341+
"outputs": [
342+
{
343+
"name": "stdout",
344+
"output_type": "stream",
345+
"text": [
346+
"{'x': 'x', 'y': 'y', 'z': 'z', 'w': 'w', 'v': 'v'}\n"
347+
]
348+
}
349+
],
350+
"source": [
351+
"def print_all(**kwargs):\n",
352+
" print(kwargs)\n",
353+
"print_all(x='x', y='y', z='z', w='w', v='v')"
354+
]
355+
},
356+
{
357+
"cell_type": "code",
358+
"execution_count": null,
359+
"metadata": {},
360+
"outputs": [],
361+
"source": []
362+
}
363+
],
364+
"metadata": {
365+
"kernelspec": {
366+
"display_name": "Python 3",
367+
"language": "python",
368+
"name": "python3"
369+
},
370+
"language_info": {
371+
"codemirror_mode": {
372+
"name": "ipython",
373+
"version": 3
374+
},
375+
"file_extension": ".py",
376+
"mimetype": "text/x-python",
377+
"name": "python",
378+
"nbconvert_exporter": "python",
379+
"pygments_lexer": "ipython3",
380+
"version": "3.6.7"
381+
}
382+
},
383+
"nbformat": 4,
384+
"nbformat_minor": 2
385+
}

0 commit comments

Comments
 (0)