Skip to content

Commit f345305

Browse files
committed
Repository updated.
1 parent d1d5717 commit f345305

File tree

6 files changed

+1156
-0
lines changed

6 files changed

+1156
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "b9a2b9ad-2e5b-46b0-bad7-bc0d78d05d7e",
6+
"metadata": {},
7+
"source": [
8+
"## Concept of *args and **kwargs"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "b671e944-f2bb-4141-a09f-410f83793299",
14+
"metadata": {},
15+
"source": [
16+
"In Python, it's possible to define functions that accept a variable number of arguments. This can be useful when we don't know in advance how many arguments a function will need to handle, or when we want to make our code more flexible. Python uses these special symbols for passing arguments:\n",
17+
"\n",
18+
"* *args (For Non-Keyword Arguments)\n",
19+
"* **kwargs (For Keyword Arguments)"
20+
]
21+
},
22+
{
23+
"cell_type": "markdown",
24+
"id": "ad5d0706-85ce-4eb9-90ee-2dd03f41302e",
25+
"metadata": {},
26+
"source": [
27+
"### *args (For Non-Keyword Arguments)"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"id": "b4f2f2b3-c66f-41a9-8f8d-cce5d4669159",
33+
"metadata": {},
34+
"source": [
35+
"In Python, *args is a special syntax used to pass a variable number of positional arguments to a function. The *args notation allows us to pass any number of arguments to a function, and those arguments will be collected into a tuple.\n",
36+
"\n",
37+
"For example, if we want to make an addition function that supports taking any number of arguments and able to add them all together. In this case we can use *args.\n",
38+
"\n",
39+
"One important thing to note is that *args must come at the end of a function's parameter list, after any named arguments. This is because *args collects all positional arguments that are not matched to named parameters, so any named parameters that come after *args will be ignored"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 1,
45+
"id": "e7ed9a8d-7c98-4cc5-826d-27a3e79050a3",
46+
"metadata": {
47+
"tags": []
48+
},
49+
"outputs": [],
50+
"source": [
51+
"def sum(*args):\n",
52+
" s = 0\n",
53+
" for i in args:\n",
54+
" s += i\n",
55+
" print(s)"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 2,
61+
"id": "b84d279d-5c51-4885-a0d3-c6ad2b209e6c",
62+
"metadata": {
63+
"tags": []
64+
},
65+
"outputs": [
66+
{
67+
"name": "stdout",
68+
"output_type": "stream",
69+
"text": [
70+
"15\n"
71+
]
72+
}
73+
],
74+
"source": [
75+
"sum(1, 2, 3, 4, 5)"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"id": "9d851610-86d3-40ab-8be6-e486699829e1",
81+
"metadata": {},
82+
"source": [
83+
"### **kwargs (For Keyword Arguments)"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"id": "1c8a2150-01f5-48ef-a844-e5975797b361",
89+
"metadata": {},
90+
"source": [
91+
"In Python, **kwargs is a special syntax used to pass a variable number of keyword arguments to a function. The **kwargs notation allows us to pass any number of keyword arguments to a function, and those arguments will be collected into a dictionary.\n",
92+
"\n",
93+
"A keyword argument is where we provide a name to the variable as we pass it into the function.\n",
94+
"\n",
95+
"One important thing to note is that **kwargs must come after *args in a function's parameter list, if both are used. This is because *args collects all positional arguments that are not matched to named parameters, while **kwargs collects all keyword arguments that are not matched to named parameters."
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 3,
101+
"id": "cbe35c5b-8d5b-48ca-9fb2-67d3a7c2ac35",
102+
"metadata": {
103+
"tags": []
104+
},
105+
"outputs": [],
106+
"source": [
107+
"def myFunction(**kwargs):\n",
108+
" for key, value in kwargs.items():\n",
109+
" print(key, \":\", value)"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 4,
115+
"id": "493b7377-650e-4eda-9ffd-ef75b96245db",
116+
"metadata": {
117+
"tags": []
118+
},
119+
"outputs": [
120+
{
121+
"name": "stdout",
122+
"output_type": "stream",
123+
"text": [
124+
"name : Krishnagopal Halder\n",
125+
"age : 22\n"
126+
]
127+
}
128+
],
129+
"source": [
130+
"myFunction(name=\"Krishnagopal Halder\", age=22)"
131+
]
132+
},
133+
{
134+
"cell_type": "markdown",
135+
"id": "8158ea12-fc2b-4687-b4f0-57eb86941806",
136+
"metadata": {},
137+
"source": [
138+
"### Example with *args and **kwargs"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": 5,
144+
"id": "2303d1f2-7a93-4687-809e-db26d80d94f5",
145+
"metadata": {
146+
"tags": []
147+
},
148+
"outputs": [],
149+
"source": [
150+
"def personalInfo(name, age, *args, **kwargs):\n",
151+
" print(\"The name of the peron is\", name)\n",
152+
" print(\"The age of the person is\", age)\n",
153+
" print(\"Other informations:\")\n",
154+
" for key, value in kwargs.items():\n",
155+
" print(key, \":\", value)\n",
156+
" print(\"Unknown informations:\")\n",
157+
" for i in args:\n",
158+
" print(i) "
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": 6,
164+
"id": "792ef8be-ac49-415f-a455-eda94a2f2802",
165+
"metadata": {
166+
"tags": []
167+
},
168+
"outputs": [
169+
{
170+
"name": "stdout",
171+
"output_type": "stream",
172+
"text": [
173+
"The name of the peron is Krishnagopal Halder\n",
174+
"The age of the person is 22\n",
175+
"Other informations:\n",
176+
"state : WB\n",
177+
"country : India\n",
178+
"Unknown informations:\n",
179+
"First arg\n",
180+
"Second arg\n"
181+
]
182+
}
183+
],
184+
"source": [
185+
"personalInfo(\"Krishnagopal Halder\", 22, \"First arg\", \"Second arg\", state=\"WB\", country=\"India\")"
186+
]
187+
}
188+
],
189+
"metadata": {
190+
"kernelspec": {
191+
"display_name": "Python 3 (ipykernel)",
192+
"language": "python",
193+
"name": "python3"
194+
},
195+
"language_info": {
196+
"codemirror_mode": {
197+
"name": "ipython",
198+
"version": 3
199+
},
200+
"file_extension": ".py",
201+
"mimetype": "text/x-python",
202+
"name": "python",
203+
"nbconvert_exporter": "python",
204+
"pygments_lexer": "ipython3",
205+
"version": "3.9.16"
206+
}
207+
},
208+
"nbformat": 4,
209+
"nbformat_minor": 5
210+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "ece3abd7-f000-4f57-9260-d17d99213727",
6+
"metadata": {},
7+
"source": [
8+
"## Python Default Parameters"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "5dd720dc-a115-427b-8cf2-bcd22469c365",
14+
"metadata": {},
15+
"source": [
16+
"In Python, function parameters can have default values. We can provide a default value to a parameter by using the assignment(=) operator."
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 1,
22+
"id": "d586ae73-7353-4de5-b027-be8ba1c49a03",
23+
"metadata": {
24+
"tags": []
25+
},
26+
"outputs": [],
27+
"source": [
28+
"def programmer(name, age, coreLang=\"Python\"):\n",
29+
" print(\"The name of the programmer is\", name)\n",
30+
" print(\"The age of the programmer is\", age)\n",
31+
" print(\"The core programming language is\", coreLang)"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 4,
37+
"id": "e9773e39-f2e6-4d97-a190-915ef860ac79",
38+
"metadata": {
39+
"tags": []
40+
},
41+
"outputs": [
42+
{
43+
"name": "stdout",
44+
"output_type": "stream",
45+
"text": [
46+
"The name of the programmer is Krishnagopal Halder\n",
47+
"The age of the programmer is 22\n",
48+
"The core programming language is Python\n"
49+
]
50+
}
51+
],
52+
"source": [
53+
"# Calling function with the default value of coreLang parameter\n",
54+
"programmer(\"Krishnagopal Halder\", 22)"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 5,
60+
"id": "71289d37-2fa3-4459-85dc-7016a27c85df",
61+
"metadata": {
62+
"tags": []
63+
},
64+
"outputs": [
65+
{
66+
"name": "stdout",
67+
"output_type": "stream",
68+
"text": [
69+
"The name of the programmer is Arav Mahanti\n",
70+
"The age of the programmer is 25\n",
71+
"The core programming language is C++\n"
72+
]
73+
}
74+
],
75+
"source": [
76+
"# Calling function with the changed value of coreLang parameter\n",
77+
"programmer(\"Arav Mahanti\", 25, \"C++\")"
78+
]
79+
}
80+
],
81+
"metadata": {
82+
"kernelspec": {
83+
"display_name": "Python 3 (ipykernel)",
84+
"language": "python",
85+
"name": "python3"
86+
},
87+
"language_info": {
88+
"codemirror_mode": {
89+
"name": "ipython",
90+
"version": 3
91+
},
92+
"file_extension": ".py",
93+
"mimetype": "text/x-python",
94+
"name": "python",
95+
"nbconvert_exporter": "python",
96+
"pygments_lexer": "ipython3",
97+
"version": "3.9.16"
98+
}
99+
},
100+
"nbformat": 4,
101+
"nbformat_minor": 5
102+
}

0 commit comments

Comments
 (0)