├── Day 04.1_Python_Keywords_and_Identifiers.ipynb
├── Day 04.2_Python_Statement_Indentation_Comments.ipynb
├── Day 05_Python_Variables_&_Constants.ipynb
├── Day 06.1_Python_Literals.ipynb
├── Day 07_Python_Data_Types.ipynb
├── Day 08.1_Python_Type_Conversion.ipynb
├── Day 09_Python_Input_Output_Import.ipynb
├── Day 10_Python_Operators.ipynb
├── Day 11.1_Python_Namespace_and_Scope.ipynb
├── Day 11.2_Python_Numbers.ipynb
├── Day 12 and 13_Python_String.ipynb
├── Day 14_Python_String Functions.ipynb
├── Day 15_Python_List.ipynb
├── Day 16_Python_List Operations.ipynb
├── Day 17_Python_List functions.ipynb
├── Day 18_Python_Tuple.ipynb
├── Day 19_Python_Tuple Methods.ipynb
├── Day 20_Python_Dictionary.ipynb
├── Day 21_Python_Dictionary Functions.ipynb
├── Day 22_Python_Sets.ipynb
├── Day 23_Python_Sets operations.ipynb
├── Day 24_Python_Flow Control and if_statement.ipynb
├── Day 25_Python_if_else_statement.ipynb
├── Day 26 Python_if_elif_nested_statement.ipynb
├── Day 27_Python_for_Loop.ipynb
├── Day 28_Python_for_Loop 2.ipynb
├── Day 29_Python_break_statements.ipynb
├── Day 29_Python_for_Loop 3.ipynb
├── Day 30_Python_while_Loop.ipynb
├── Day 31_Python_break_continue_pass_statements.ipynb
├── Day 32_Python_Functions.ipynb
├── Day 33_Python_Functions return statement.ipynb
├── Day 34_Python_Function_Global_Local_Nonlocal.ipynb
├── Day 35.1_Python_Function_global_Keywords.ipynb
├── Day 35.2_Python_User_defined_Functions.ipynb
├── Day 36_Python_Function_Arguments.ipynb
├── Day 37_Python_Function_Recursion.ipynb
├── Day 38_Python_Function_Anonymous.ipynb
├── Day 39 & 40_Python_Function_Module.ipynb
├── Day 41_Python_File_Input_Output.ipynb
├── Day 42_Python_File_Input_Output - Methods.ipynb
├── Day 43_Python_File_Directory.ipynb
├── Day 44_Python_Errors_and_Built-in_Exceptions.ipynb
├── Day 45_Python_Exceptions_Handling.ipynb
├── Day 46_Python_Exceptions_Handling - 2.ipynb
├── Day 47_Python_User_defined_Exceptions.ipynb
├── Day 48_Python_OOPs_Concepts.ipynb
├── Day 49_Python_OOPs_Concepts - 2.ipynb
├── Day 50 & 51_Python_Classes_and_Objects.ipynb
├── Day 52 & 53_Python_Inheritance.ipynb
├── Day 54_Method_Overriding_Operator_Overloading.ipynb
├── Day 55_Python_Iterators.ipynb
├── Day 56_Python_Generators.ipynb
├── Day 57_Python_Closure.ipynb
├── Day 58_Python_Decorators.ipynb
├── Day 59 & 60_Python_RegEx.ipynb
├── README.md
├── countries_data.py
├── countries_details_data.py
└── img
├── b1.png
├── b2.png
├── b4.png
├── b5.png
├── con1.png
├── con2.png
├── con3.png
├── con4.png
├── ctrl1.png
├── d0.png
├── dnld_rep.png
├── eif0.png
├── eif1.png
├── eif2.png
├── exlc.png
├── f1.png
├── f2.png
├── fcs.png
├── for0.png
├── for1.png
├── for10.png
├── for2.png
├── for3.png
├── for4.png
├── for5.png
├── for6.png
├── for7.png
├── for8.png
├── for9.png
├── forrange.png
├── if1.png
├── if2.png
├── if2_1.png
├── if2_2.png
├── if3.png
├── if4.png
├── ife0.png
├── ife1.png
├── ife2.png
├── ife3.png
├── l0.png
├── l1.png
├── l14_1.png
├── l6_1.png
├── l6_2.png
├── loop1.png
├── nd0.png
├── nforloop1.png
├── nforloop2.png
├── nif0.png
├── nif1.png
├── nif2.png
├── nif3.png
├── pa1.png
├── pa2.png
├── s0.png
├── s16.png
├── s3.png
├── set1.png
├── set10.png
├── set11.png
├── set12.png
├── set2.png
├── set3.png
├── set4.png
├── set5.png
├── set6.png
├── set7.png
├── set8.png
├── set9.png
├── t0.png
├── t5.png
├── t6.png
├── wh0.png
├── wh1.png
├── wh2.png
├── wh3.png
├── wh4.png
└── wh5.png
/Day 04.1_Python_Keywords_and_Identifiers.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Keywords and Identifiers\n",
8 | "\n",
9 | "In this session, you will learn about keywords (reserved words in Python) and identifiers (names given to variables, functions, etc.)."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "# 1. Python Keywords\n",
17 | "\n",
18 | "**Keywords in Python**\n",
19 | "\n",
20 | "| | | | |\n",
21 | "|:----|:----|:----|:----|\n",
22 | "| **`False`** | **`break`** | **`for`** | **`not`** |\n",
23 | "| **`None`** | **`class`** | **`from`** | **`or`** |\n",
24 | "| **`True`** | **`continue`** | **`global`** | **`pass`** |\n",
25 | "| **`__peg_parser__`** |**`def`** | **`if`** | **`raise`** |\n",
26 | "| **`and`** | **`del`** | **`import`** | **`return`** |\n",
27 | "| **`as`** | **`elif`** | **`in`** | **`try`** |\n",
28 | "| **`assert`** | **`else`** | **`is`** | **`while`** |\n",
29 | "| **`async`** | **`except`** | **`lambda`** | **`with`** |\n",
30 | "| **`await`** | **`finally`** | **`nonlocal`** | **`yield`** |\n"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 1,
36 | "metadata": {},
37 | "outputs": [],
38 | "source": [
39 | "#Invalid"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": 2,
45 | "metadata": {
46 | "ExecuteTime": {
47 | "end_time": "2021-06-22T14:51:40.668985Z",
48 | "start_time": "2021-06-22T14:51:40.658242Z"
49 | }
50 | },
51 | "outputs": [
52 | {
53 | "ename": "SyntaxError",
54 | "evalue": "invalid syntax (Temp/ipykernel_22400/2939134965.py, line 1)",
55 | "output_type": "error",
56 | "traceback": [
57 | "\u001b[1;36m File \u001b[1;32m\"C:\\Users\\AJANTHA\\AppData\\Local\\Temp/ipykernel_22400/2939134965.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m for = 6 # It will give error becasue \"for\" is keyword and we cannot use as a variable name.\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
58 | ]
59 | }
60 | ],
61 | "source": [
62 | "for = 6 # It will give error becasue \"for\" is keyword and we cannot use as a variable name."
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": 3,
68 | "metadata": {
69 | "ExecuteTime": {
70 | "end_time": "2021-06-22T14:51:41.967978Z",
71 | "start_time": "2021-06-22T14:51:41.945516Z"
72 | }
73 | },
74 | "outputs": [
75 | {
76 | "data": {
77 | "text/plain": [
78 | "6"
79 | ]
80 | },
81 | "execution_count": 3,
82 | "metadata": {},
83 | "output_type": "execute_result"
84 | }
85 | ],
86 | "source": [
87 | "For = 6 # \"for\" is keyword but \"For\" is not keyword so we can use it as variable name\n",
88 | "For"
89 | ]
90 | },
91 | {
92 | "cell_type": "markdown",
93 | "metadata": {},
94 | "source": [
95 | "# 2. Python Identifiers\n",
96 | "\n",
97 | "An **identifier** is a name given to entities like **class, functions, variables, etc**. It helps to differentiate one entity from another."
98 | ]
99 | },
100 | {
101 | "cell_type": "markdown",
102 | "metadata": {},
103 | "source": [
104 | "### Rules for writing identifiers\n",
105 | "\n",
106 | "1. **Identifiers** can be a combination of letters in lowercase **(a to z)** or uppercase **(A to Z)** or digits **(0 to 9)** or an underscore **`_`**. Names like **`myClass`**, **`var_1`** and **`print_this_to_screen`**, all are valid example. \n",
107 | "\n",
108 | "2. An identifier cannot start with a digit. **`1variable`** is invalid, but **`variable1`** is perfectly fine. \n",
109 | "\n",
110 | "3. Keywords cannot be used as identifiers\n"
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": 4,
116 | "metadata": {
117 | "ExecuteTime": {
118 | "end_time": "2021-06-22T14:51:45.004986Z",
119 | "start_time": "2021-06-22T14:51:44.995218Z"
120 | }
121 | },
122 | "outputs": [
123 | {
124 | "ename": "SyntaxError",
125 | "evalue": "invalid syntax (Temp/ipykernel_22400/288024902.py, line 1)",
126 | "output_type": "error",
127 | "traceback": [
128 | "\u001b[1;36m File \u001b[1;32m\"C:\\Users\\AJANTHA\\AppData\\Local\\Temp/ipykernel_22400/288024902.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m global = 3 # because \"global\" is a keyword\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
129 | ]
130 | }
131 | ],
132 | "source": [
133 | "global = 3 # because \"global\" is a keyword"
134 | ]
135 | },
136 | {
137 | "cell_type": "markdown",
138 | "metadata": {},
139 | "source": [
140 | "4. We cannot use special symbols like **!**, **@**, **#**, $, % , etc. in our identifier."
141 | ]
142 | },
143 | {
144 | "cell_type": "code",
145 | "execution_count": 5,
146 | "metadata": {
147 | "ExecuteTime": {
148 | "end_time": "2021-06-22T14:51:48.063967Z",
149 | "start_time": "2021-06-22T14:51:48.058109Z"
150 | }
151 | },
152 | "outputs": [
153 | {
154 | "ename": "SyntaxError",
155 | "evalue": "invalid syntax (Temp/ipykernel_22400/1790255783.py, line 1)",
156 | "output_type": "error",
157 | "traceback": [
158 | "\u001b[1;36m File \u001b[1;32m\"C:\\Users\\AJANTHA\\AppData\\Local\\Temp/ipykernel_22400/1790255783.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m m@ = 3\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
159 | ]
160 | }
161 | ],
162 | "source": [
163 | "m@ = 3"
164 | ]
165 | },
166 | {
167 | "cell_type": "markdown",
168 | "metadata": {},
169 | "source": [
170 | "## Things to Remember"
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": 6,
176 | "metadata": {
177 | "ExecuteTime": {
178 | "end_time": "2021-06-22T14:51:49.685264Z",
179 | "start_time": "2021-06-22T14:51:49.666710Z"
180 | }
181 | },
182 | "outputs": [
183 | {
184 | "data": {
185 | "text/plain": [
186 | "9"
187 | ]
188 | },
189 | "execution_count": 6,
190 | "metadata": {},
191 | "output_type": "execute_result"
192 | }
193 | ],
194 | "source": [
195 | "this_is_a_long_variable = 6+3\n",
196 | "this_is_a_long_variable"
197 | ]
198 | },
199 | {
200 | "cell_type": "code",
201 | "execution_count": 7,
202 | "metadata": {
203 | "ExecuteTime": {
204 | "end_time": "2021-06-22T14:51:50.580889Z",
205 | "start_time": "2021-06-22T14:51:50.571121Z"
206 | }
207 | },
208 | "outputs": [
209 | {
210 | "data": {
211 | "text/plain": [
212 | "9"
213 | ]
214 | },
215 | "execution_count": 7,
216 | "metadata": {},
217 | "output_type": "execute_result"
218 | }
219 | ],
220 | "source": [
221 | "add_6_and_3 = 6+3\n",
222 | "add_6_and_3"
223 | ]
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": null,
228 | "metadata": {},
229 | "outputs": [],
230 | "source": []
231 | }
232 | ],
233 | "metadata": {
234 | "gist": {
235 | "data": {
236 | "description": "01_Learn_Python4Data/01_Python_Introduction/005_Python_Keywords_and_Identifiers.ipynb",
237 | "public": true
238 | },
239 | "id": ""
240 | },
241 | "hide_input": false,
242 | "kernelspec": {
243 | "display_name": "Python 3 (ipykernel)",
244 | "language": "python",
245 | "name": "python3"
246 | },
247 | "language_info": {
248 | "codemirror_mode": {
249 | "name": "ipython",
250 | "version": 3
251 | },
252 | "file_extension": ".py",
253 | "mimetype": "text/x-python",
254 | "name": "python",
255 | "nbconvert_exporter": "python",
256 | "pygments_lexer": "ipython3",
257 | "version": "3.9.7"
258 | },
259 | "toc": {
260 | "base_numbering": 1,
261 | "nav_menu": {},
262 | "number_sections": true,
263 | "sideBar": true,
264 | "skip_h1_title": false,
265 | "title_cell": "Table of Contents",
266 | "title_sidebar": "Contents",
267 | "toc_cell": false,
268 | "toc_position": {},
269 | "toc_section_display": true,
270 | "toc_window_display": false
271 | },
272 | "varInspector": {
273 | "cols": {
274 | "lenName": 16,
275 | "lenType": 16,
276 | "lenVar": 40
277 | },
278 | "kernels_config": {
279 | "python": {
280 | "delete_cmd_postfix": "",
281 | "delete_cmd_prefix": "del ",
282 | "library": "var_list.py",
283 | "varRefreshCmd": "print(var_dic_list())"
284 | },
285 | "r": {
286 | "delete_cmd_postfix": ") ",
287 | "delete_cmd_prefix": "rm(",
288 | "library": "var_list.r",
289 | "varRefreshCmd": "cat(var_dic_list()) "
290 | }
291 | },
292 | "types_to_exclude": [
293 | "module",
294 | "function",
295 | "builtin_function_or_method",
296 | "instance",
297 | "_Feature"
298 | ],
299 | "window_display": false
300 | }
301 | },
302 | "nbformat": 4,
303 | "nbformat_minor": 2
304 | }
305 |
--------------------------------------------------------------------------------
/Day 06.1_Python_Literals.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Literals\n",
8 | "\n",
9 | "**Literal** is a raw data given in a **variable** or **constant**. In Python, there are various types of literals they are as follows:"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## 1. Numeric Literals\n",
17 | "\n",
18 | "Numeric Literals are **immutable**.\n",
19 | "\n",
20 | "
\n",
21 | "

\n",
22 | "
"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": 1,
28 | "metadata": {
29 | "ExecuteTime": {
30 | "end_time": "2021-10-02T07:56:41.965572Z",
31 | "start_time": "2021-10-02T07:56:41.949947Z"
32 | }
33 | },
34 | "outputs": [
35 | {
36 | "name": "stdout",
37 | "output_type": "stream",
38 | "text": [
39 | "10 100 200 300\n",
40 | "10 99\n",
41 | "10.5 150.0\n",
42 | "3.14j 3.14 0.0\n"
43 | ]
44 | }
45 | ],
46 | "source": [
47 | "# Example:\n",
48 | "\n",
49 | "a = 0b1010 #Binary Literals\n",
50 | "b = 100 #Decimal Literal\n",
51 | "c = 0o310 #Octal Literal\n",
52 | "d = 0x12c #Hexadecimal Literal\n",
53 | "\n",
54 | "# Integer Literal\n",
55 | "int_1 = 10\n",
56 | "int_2 = 99\n",
57 | "\n",
58 | "\n",
59 | "# Float Literal\n",
60 | "float_1 = 10.5\n",
61 | "float_2 = 1.5e2\n",
62 | "\n",
63 | "\n",
64 | "# Complex Literal\n",
65 | "x = 3.14j\n",
66 | "\n",
67 | "print(a, b, c, d)\n",
68 | "print(int_1, int_2)\n",
69 | "print(float_1, float_2)\n",
70 | "print(x, x.imag, x.real)"
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "### Type Conversion of Numbers in Python\n"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": 2,
83 | "metadata": {
84 | "ExecuteTime": {
85 | "end_time": "2021-10-02T07:56:41.980222Z",
86 | "start_time": "2021-10-02T07:56:41.968501Z"
87 | }
88 | },
89 | "outputs": [
90 | {
91 | "name": "stdout",
92 | "output_type": "stream",
93 | "text": [
94 | "19\n",
95 | "3.7\n",
96 | "(3.7+0j)\n",
97 | "(19.16+0j)\n",
98 | "(3.7+19.16j)\n"
99 | ]
100 | }
101 | ],
102 | "source": [
103 | "# Example:\n",
104 | "\n",
105 | "a = 3.7\n",
106 | "b = 19.16\n",
107 | "c = 3 + 27j\n",
108 | "\n",
109 | "#converting float to int\n",
110 | "print (int(b))\n",
111 | "\n",
112 | "#converting int to float\n",
113 | "print (float(a))\n",
114 | "\n",
115 | "#converting int to complex\n",
116 | "print (complex(a))\n",
117 | "\n",
118 | "#converting float to complex\n",
119 | "print (complex(b))\n",
120 | "\n",
121 | "#converting to complex\n",
122 | "print (complex(a, b))"
123 | ]
124 | },
125 | {
126 | "cell_type": "markdown",
127 | "metadata": {},
128 | "source": [
129 | "## 2. String literals (unicode character strings)\n"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": 3,
135 | "metadata": {
136 | "ExecuteTime": {
137 | "end_time": "2021-10-02T07:56:42.487053Z",
138 | "start_time": "2021-10-02T07:56:41.990964Z"
139 | }
140 | },
141 | "outputs": [
142 | {
143 | "ename": "NameError",
144 | "evalue": "name 'Apple' is not defined",
145 | "output_type": "error",
146 | "traceback": [
147 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
148 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
149 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_25552/165315220.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0md\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Apple\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0me\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mApple\u001b[0m \u001b[1;31m# cannot write string with out quotes ('', \" \", \"\"\" \"\"\", ''' ''')\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 12\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
150 | "\u001b[1;31mNameError\u001b[0m: name 'Apple' is not defined"
151 | ]
152 | }
153 | ],
154 | "source": [
155 | "# Example:\n",
156 | "\n",
157 | "a = '''Apple'''\n",
158 | "\n",
159 | "b = \"\"\"Apple\"\"\"\n",
160 | "\n",
161 | "c = 'Apple'\n",
162 | "\n",
163 | "d = \"Apple\"\n",
164 | "\n",
165 | "e = Apple # cannot write string with out quotes ('', \" \", \"\"\" \"\"\", ''' ''')\n",
166 | "\n",
167 | "print(a)\n",
168 | "print(b)\n",
169 | "print(c)\n",
170 | "print(d)\n",
171 | "print(e)"
172 | ]
173 | },
174 | {
175 | "cell_type": "code",
176 | "execution_count": 4,
177 | "metadata": {
178 | "ExecuteTime": {
179 | "end_time": "2021-10-02T07:56:53.085188Z",
180 | "start_time": "2021-10-02T07:56:53.065659Z"
181 | },
182 | "scrolled": true
183 | },
184 | "outputs": [
185 | {
186 | "name": "stdout",
187 | "output_type": "stream",
188 | "text": [
189 | "This is Python\n",
190 | "C\n",
191 | "This is a multiline string with more than one line code.\n",
192 | "Ünicöde\n",
193 | "raw \\n string\n"
194 | ]
195 | }
196 | ],
197 | "source": [
198 | "# Example:\n",
199 | "\n",
200 | "strings = \"This is Python\"\n",
201 | "char = \"C\"\n",
202 | "multiline_str = \"\"\"This is a multiline string with more than one line code.\"\"\"\n",
203 | "unicode = u\"\\u00dcnic\\u00f6de\"\n",
204 | "raw_str = r\"raw \\n string\"\n",
205 | "\n",
206 | "print(strings)\n",
207 | "print(char)\n",
208 | "print(multiline_str)\n",
209 | "print(unicode)\n",
210 | "print(raw_str)"
211 | ]
212 | },
213 | {
214 | "cell_type": "markdown",
215 | "metadata": {},
216 | "source": [
217 | "## 3. Boolean literals\n",
218 | "\n",
219 | "A Boolean literal can have any of the two values: **`True`** or **`False`**."
220 | ]
221 | },
222 | {
223 | "cell_type": "code",
224 | "execution_count": 5,
225 | "metadata": {
226 | "ExecuteTime": {
227 | "end_time": "2021-10-02T07:56:53.639879Z",
228 | "start_time": "2021-10-02T07:56:53.626204Z"
229 | },
230 | "scrolled": true
231 | },
232 | "outputs": [
233 | {
234 | "name": "stdout",
235 | "output_type": "stream",
236 | "text": [
237 | "x is True\n",
238 | "y is False\n",
239 | "a: 7\n",
240 | "b: 90\n"
241 | ]
242 | }
243 | ],
244 | "source": [
245 | "# Example:\n",
246 | "\n",
247 | "#REMEMBER True == 1 False == 0\n",
248 | "\n",
249 | "x = (1 == True)\n",
250 | "y = (1 == False)\n",
251 | "a = True + 6\n",
252 | "b = False + 90\n",
253 | "\n",
254 | "print(\"x is\", x)\n",
255 | "print(\"y is\", y)\n",
256 | "print(\"a:\", a)\n",
257 | "print(\"b:\", b)"
258 | ]
259 | },
260 | {
261 | "cell_type": "code",
262 | "execution_count": 6,
263 | "metadata": {
264 | "ExecuteTime": {
265 | "end_time": "2021-10-02T07:56:54.023664Z",
266 | "start_time": "2021-10-02T07:56:54.009994Z"
267 | }
268 | },
269 | "outputs": [
270 | {
271 | "name": "stdout",
272 | "output_type": "stream",
273 | "text": [
274 | "Triple quotes (also with '''), allow strings to break over multiple lines.\n",
275 | "Alternatively \n",
276 | " is a newline character (\t for tab, \\ is a single backslash)\n"
277 | ]
278 | }
279 | ],
280 | "source": [
281 | "# Example:\n",
282 | "\n",
283 | "9.0 # a simple floating point number\n",
284 | "1e100 # a googol as floating point number\n",
285 | "-1234567890 # an integer\n",
286 | "True or False # the two possible boolean values\n",
287 | "'This is a string'\n",
288 | "\"It's another string\"\n",
289 | "print(\"\"\"Triple quotes (also with '''), allow strings to break over multiple lines.\n",
290 | "Alternatively \\n is a newline character (\\t for tab, \\\\ is a single backslash)\"\"\")"
291 | ]
292 | },
293 | {
294 | "cell_type": "code",
295 | "execution_count": 7,
296 | "metadata": {
297 | "ExecuteTime": {
298 | "end_time": "2021-10-02T07:56:54.425520Z",
299 | "start_time": "2021-10-02T07:56:54.416732Z"
300 | }
301 | },
302 | "outputs": [
303 | {
304 | "name": "stdout",
305 | "output_type": "stream",
306 | "text": [
307 | "(1+2j)\n",
308 | "(1+2j)\n"
309 | ]
310 | }
311 | ],
312 | "source": [
313 | "complex_number1 = complex(1,2)\n",
314 | "print(complex_number1)\n",
315 | "\n",
316 | "complex_number2 = (1.0+2j) # the same number as above\n",
317 | "print(complex_number2)"
318 | ]
319 | },
320 | {
321 | "cell_type": "markdown",
322 | "metadata": {},
323 | "source": [
324 | "## 4. Special literals\n",
325 | "\n",
326 | "Python contains one **special** literal i.e., **`None`**. We use it to specify to that field that is not created."
327 | ]
328 | },
329 | {
330 | "cell_type": "code",
331 | "execution_count": 8,
332 | "metadata": {
333 | "ExecuteTime": {
334 | "end_time": "2021-10-02T07:56:54.823957Z",
335 | "start_time": "2021-10-02T07:56:54.812240Z"
336 | },
337 | "scrolled": true
338 | },
339 | "outputs": [
340 | {
341 | "name": "stdout",
342 | "output_type": "stream",
343 | "text": [
344 | "Available\n",
345 | "None\n"
346 | ]
347 | }
348 | ],
349 | "source": [
350 | "# Example:\n",
351 | "\n",
352 | "juice = \"Available\"\n",
353 | "soup = None\n",
354 | "def menu(x):\n",
355 | " if x == juice:\n",
356 | " print(juice)\n",
357 | " else:\n",
358 | " print(soup)\n",
359 | "menu(juice)\n",
360 | "menu(soup)"
361 | ]
362 | },
363 | {
364 | "cell_type": "markdown",
365 | "metadata": {},
366 | "source": [
367 | "## 5. Literal Collections\n",
368 | "\n",
369 | "There are four different literal collections **List literals, Tuple literals, Dict literals**, and **Set literals**."
370 | ]
371 | },
372 | {
373 | "cell_type": "code",
374 | "execution_count": 9,
375 | "metadata": {
376 | "ExecuteTime": {
377 | "end_time": "2021-10-02T07:56:55.311263Z",
378 | "start_time": "2021-10-02T07:56:55.288834Z"
379 | }
380 | },
381 | "outputs": [
382 | {
383 | "name": "stdout",
384 | "output_type": "stream",
385 | "text": [
386 | "('Banana', 'Apple', 'Strawberry')\n",
387 | "['Banana', 'Apple', 'Strawberry']\n",
388 | "{'Strawberry', 'Banana', 'Apple'}\n",
389 | "{'1': 'Banana', '2': 'Apple', '3': 'Strawberry'}\n"
390 | ]
391 | }
392 | ],
393 | "source": [
394 | "# Example:\n",
395 | "\n",
396 | "fruits1 = (\"Banana\", \"Apple\", \"Strawberry\") # tuple ()\n",
397 | "fruits2 = [\"Banana\", \"Apple\", \"Strawberry\"] # list []\n",
398 | "fruits3 = {\"Banana\", \"Apple\", \"Strawberry\"} # set {}\n",
399 | "fruits4 = {\"1\":\"Banana\", \"2\":\"Apple\", \"3\":\"Strawberry\"} # dictionary {\"Key\":\"Value\"}\n",
400 | "\n",
401 | "print(fruits1)\n",
402 | "print(fruits2)\n",
403 | "print(fruits3)\n",
404 | "print(fruits4)"
405 | ]
406 | },
407 | {
408 | "cell_type": "code",
409 | "execution_count": 10,
410 | "metadata": {
411 | "ExecuteTime": {
412 | "end_time": "2021-10-02T07:56:55.723859Z",
413 | "start_time": "2021-10-02T07:56:55.715559Z"
414 | }
415 | },
416 | "outputs": [
417 | {
418 | "name": "stdout",
419 | "output_type": "stream",
420 | "text": [
421 | "['apple', 'mango', 'orange']\n",
422 | "(1, 2, 3)\n",
423 | "{'a': 'apple', 'b': 'ball', 'c': 'cat'}\n",
424 | "{'a', 'e', 'o', 'i', 'u'}\n"
425 | ]
426 | }
427 | ],
428 | "source": [
429 | "# Example:\n",
430 | "\n",
431 | "fruits = [\"apple\", \"mango\", \"orange\"] #list\n",
432 | "numbers = (1, 2, 3) #tuple\n",
433 | "alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} #dictionary\n",
434 | "vowels = {'a', 'e', 'i' , 'o', 'u'} #set\n",
435 | "\n",
436 | "print(fruits)\n",
437 | "print(numbers)\n",
438 | "print(alphabets)\n",
439 | "print(vowels)"
440 | ]
441 | },
442 | {
443 | "cell_type": "code",
444 | "execution_count": null,
445 | "metadata": {},
446 | "outputs": [],
447 | "source": []
448 | }
449 | ],
450 | "metadata": {
451 | "hide_input": false,
452 | "kernelspec": {
453 | "display_name": "Python 3 (ipykernel)",
454 | "language": "python",
455 | "name": "python3"
456 | },
457 | "language_info": {
458 | "codemirror_mode": {
459 | "name": "ipython",
460 | "version": 3
461 | },
462 | "file_extension": ".py",
463 | "mimetype": "text/x-python",
464 | "name": "python",
465 | "nbconvert_exporter": "python",
466 | "pygments_lexer": "ipython3",
467 | "version": "3.9.7"
468 | },
469 | "toc": {
470 | "base_numbering": 1,
471 | "nav_menu": {},
472 | "number_sections": true,
473 | "sideBar": true,
474 | "skip_h1_title": false,
475 | "title_cell": "Table of Contents",
476 | "title_sidebar": "Contents",
477 | "toc_cell": false,
478 | "toc_position": {},
479 | "toc_section_display": true,
480 | "toc_window_display": false
481 | },
482 | "varInspector": {
483 | "cols": {
484 | "lenName": 16,
485 | "lenType": 16,
486 | "lenVar": 40
487 | },
488 | "kernels_config": {
489 | "python": {
490 | "delete_cmd_postfix": "",
491 | "delete_cmd_prefix": "del ",
492 | "library": "var_list.py",
493 | "varRefreshCmd": "print(var_dic_list())"
494 | },
495 | "r": {
496 | "delete_cmd_postfix": ") ",
497 | "delete_cmd_prefix": "rm(",
498 | "library": "var_list.r",
499 | "varRefreshCmd": "cat(var_dic_list()) "
500 | }
501 | },
502 | "types_to_exclude": [
503 | "module",
504 | "function",
505 | "builtin_function_or_method",
506 | "instance",
507 | "_Feature"
508 | ],
509 | "window_display": false
510 | }
511 | },
512 | "nbformat": 4,
513 | "nbformat_minor": 2
514 | }
515 |
--------------------------------------------------------------------------------
/Day 11.1_Python_Namespace_and_Scope.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Namespace and Scope\n",
8 | "\n",
9 | "In this session, we will learn about namespace, mapping from names to objects, and scope of a variable."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## What is Name in Python?\n",
17 | "\n"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": 1,
23 | "metadata": {
24 | "ExecuteTime": {
25 | "end_time": "2021-06-22T17:09:01.664271Z",
26 | "start_time": "2021-06-22T17:09:01.657435Z"
27 | }
28 | },
29 | "outputs": [
30 | {
31 | "name": "stdout",
32 | "output_type": "stream",
33 | "text": [
34 | "id(9) = 1725776947760\n",
35 | "id(a) = 1725776947760\n"
36 | ]
37 | }
38 | ],
39 | "source": [
40 | "# Note: You may get different values for the id\n",
41 | "\n",
42 | "a = 9\n",
43 | "print('id(9) =', id(9))\n",
44 | "\n",
45 | "print('id(a) =', id(a))"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": 2,
51 | "metadata": {
52 | "ExecuteTime": {
53 | "end_time": "2021-06-22T17:09:07.161795Z",
54 | "start_time": "2021-06-22T17:09:07.144217Z"
55 | }
56 | },
57 | "outputs": [
58 | {
59 | "name": "stdout",
60 | "output_type": "stream",
61 | "text": [
62 | "id(a) = 1725776947536\n",
63 | "id(a) = 1725776947568\n",
64 | "id(3) = 1725776947568\n",
65 | "id(b) = 1725776947536\n",
66 | "id(2) = 1725776947536\n"
67 | ]
68 | }
69 | ],
70 | "source": [
71 | "# Note: You may get different values for the id\n",
72 | "\n",
73 | "a = 2\n",
74 | "print('id(a) =', id(a))\n",
75 | "\n",
76 | "a = a+1\n",
77 | "print('id(a) =', id(a))\n",
78 | "\n",
79 | "print('id(3) =', id(3))\n",
80 | "\n",
81 | "b = 2\n",
82 | "print('id(b) =', id(b))\n",
83 | "print('id(2) =', id(2))"
84 | ]
85 | },
86 | {
87 | "cell_type": "markdown",
88 | "metadata": {},
89 | "source": [
90 | "What is happening in the above sequence of steps? Let's use a diagram to explain this:\n",
91 | "\n",
92 | "**Memory diagram of variables in Python**:\n",
93 | "\n",
94 | "\n",
95 | "

\n",
96 | "
"
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 3,
102 | "metadata": {
103 | "ExecuteTime": {
104 | "end_time": "2021-06-22T17:09:29.115275Z",
105 | "start_time": "2021-06-22T17:09:29.105508Z"
106 | }
107 | },
108 | "outputs": [
109 | {
110 | "name": "stdout",
111 | "output_type": "stream",
112 | "text": [
113 | "Hello\n"
114 | ]
115 | }
116 | ],
117 | "source": [
118 | "def printHello():\n",
119 | " print(\"Hello\")\n",
120 | "\n",
121 | "a = printHello\n",
122 | "\n",
123 | "a()"
124 | ]
125 | },
126 | {
127 | "cell_type": "markdown",
128 | "metadata": {
129 | "heading_collapsed": true
130 | },
131 | "source": [
132 | "## What is a Namespace in Python?\n",
133 | "\n",
134 | "\n",
135 | "\n",
136 | "

\n",
137 | "
"
138 | ]
139 | },
140 | {
141 | "cell_type": "markdown",
142 | "metadata": {},
143 | "source": [
144 | "## Python Variable Scope\n",
145 | "\n"
146 | ]
147 | },
148 | {
149 | "cell_type": "markdown",
150 | "metadata": {},
151 | "source": [
152 | "Example of Scope and Namespace in Python\n",
153 | "\n",
154 | "```python\n",
155 | ">>>def outer_function():\n",
156 | ">>> b = 30\n",
157 | ">>> def inner_func():\n",
158 | ">>> c = 60\n",
159 | "\n",
160 | ">>>a = 90\n",
161 | "```\n",
162 | "\n"
163 | ]
164 | },
165 | {
166 | "cell_type": "code",
167 | "execution_count": 4,
168 | "metadata": {
169 | "ExecuteTime": {
170 | "end_time": "2021-06-22T17:11:16.576081Z",
171 | "start_time": "2021-06-22T17:11:16.565338Z"
172 | }
173 | },
174 | "outputs": [
175 | {
176 | "name": "stdout",
177 | "output_type": "stream",
178 | "text": [
179 | "a = 60\n",
180 | "a = 30\n",
181 | "a = 90\n"
182 | ]
183 | }
184 | ],
185 | "source": [
186 | "def outer_function():\n",
187 | " a = 30\n",
188 | "\n",
189 | " def inner_function():\n",
190 | " a = 60\n",
191 | " print('a =', a)\n",
192 | "\n",
193 | " inner_function()\n",
194 | " print('a =', a)\n",
195 | "\n",
196 | "\n",
197 | "a = 90\n",
198 | "outer_function()\n",
199 | "print('a =', a)"
200 | ]
201 | },
202 | {
203 | "cell_type": "code",
204 | "execution_count": 5,
205 | "metadata": {
206 | "ExecuteTime": {
207 | "end_time": "2021-06-22T17:11:21.608276Z",
208 | "start_time": "2021-06-22T17:11:21.588748Z"
209 | }
210 | },
211 | "outputs": [
212 | {
213 | "name": "stdout",
214 | "output_type": "stream",
215 | "text": [
216 | "a = 60\n",
217 | "a = 60\n",
218 | "a = 60\n"
219 | ]
220 | }
221 | ],
222 | "source": [
223 | "def outer_function():\n",
224 | " global a\n",
225 | " a = 30\n",
226 | "\n",
227 | " def inner_function():\n",
228 | " global a\n",
229 | " a = 60\n",
230 | " print('a =', a)\n",
231 | "\n",
232 | " inner_function()\n",
233 | " print('a =', a)\n",
234 | "\n",
235 | "\n",
236 | "a = 90\n",
237 | "outer_function()\n",
238 | "print('a =', a)"
239 | ]
240 | },
241 | {
242 | "cell_type": "code",
243 | "execution_count": null,
244 | "metadata": {},
245 | "outputs": [],
246 | "source": []
247 | }
248 | ],
249 | "metadata": {
250 | "hide_input": false,
251 | "kernelspec": {
252 | "display_name": "Python 3 (ipykernel)",
253 | "language": "python",
254 | "name": "python3"
255 | },
256 | "language_info": {
257 | "codemirror_mode": {
258 | "name": "ipython",
259 | "version": 3
260 | },
261 | "file_extension": ".py",
262 | "mimetype": "text/x-python",
263 | "name": "python",
264 | "nbconvert_exporter": "python",
265 | "pygments_lexer": "ipython3",
266 | "version": "3.9.7"
267 | },
268 | "toc": {
269 | "base_numbering": 1,
270 | "nav_menu": {},
271 | "number_sections": true,
272 | "sideBar": true,
273 | "skip_h1_title": false,
274 | "title_cell": "Table of Contents",
275 | "title_sidebar": "Contents",
276 | "toc_cell": false,
277 | "toc_position": {},
278 | "toc_section_display": true,
279 | "toc_window_display": false
280 | },
281 | "varInspector": {
282 | "cols": {
283 | "lenName": 16,
284 | "lenType": 16,
285 | "lenVar": 40
286 | },
287 | "kernels_config": {
288 | "python": {
289 | "delete_cmd_postfix": "",
290 | "delete_cmd_prefix": "del ",
291 | "library": "var_list.py",
292 | "varRefreshCmd": "print(var_dic_list())"
293 | },
294 | "r": {
295 | "delete_cmd_postfix": ") ",
296 | "delete_cmd_prefix": "rm(",
297 | "library": "var_list.r",
298 | "varRefreshCmd": "cat(var_dic_list()) "
299 | }
300 | },
301 | "types_to_exclude": [
302 | "module",
303 | "function",
304 | "builtin_function_or_method",
305 | "instance",
306 | "_Feature"
307 | ],
308 | "window_display": false
309 | }
310 | },
311 | "nbformat": 4,
312 | "nbformat_minor": 2
313 | }
314 |
--------------------------------------------------------------------------------
/Day 16_Python_List Operations.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python List\n",
8 | "\n",
9 | "In this session, we'll learn everything about Python lists Operations\n"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## Python List Operations\n",
17 | "\n"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "### Add/Change List Elements\n"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": 1,
30 | "metadata": {
31 | "ExecuteTime": {
32 | "end_time": "2021-10-03T09:17:25.010562Z",
33 | "start_time": "2021-10-03T09:17:24.992012Z"
34 | }
35 | },
36 | "outputs": [
37 | {
38 | "name": "stdout",
39 | "output_type": "stream",
40 | "text": [
41 | "[1, 4, 6, 8]\n",
42 | "[1, 3, 5, 7]\n"
43 | ]
44 | }
45 | ],
46 | "source": [
47 | "# Example: Correcting mistake values in a list\n",
48 | "\n",
49 | "odd = [2, 4, 6, 8]\n",
50 | " \n",
51 | "odd[0] = 1 # ▶ [1, 4, 6, 8] ∵ update 1st element \n",
52 | "print(odd)\n",
53 | "\n",
54 | "odd[1:4] = [3, 5, 7] # ▶ [1, 3, 5, 7] ∵ update 2nd to 4th elements\n",
55 | "print(odd) "
56 | ]
57 | },
58 | {
59 | "cell_type": "code",
60 | "execution_count": 2,
61 | "metadata": {
62 | "ExecuteTime": {
63 | "end_time": "2021-10-03T09:17:25.296700Z",
64 | "start_time": "2021-10-03T09:17:25.276192Z"
65 | },
66 | "scrolled": false
67 | },
68 | "outputs": [
69 | {
70 | "name": "stdout",
71 | "output_type": "stream",
72 | "text": [
73 | "[5, 10, 15, 20, 25]\n",
74 | "[5, 10, 'Multiple of 5', 20, 25]\n"
75 | ]
76 | }
77 | ],
78 | "source": [
79 | "# Example:\n",
80 | "\n",
81 | "data1=[5,10,15,20,25]\n",
82 | "print(data1) # ▶ [5, 10, 15, 20, 25]\n",
83 | "\n",
84 | "data1[2]=\"Multiple of 5\" # we are modifying the 3rd element using its index [2] \n",
85 | "print(data1) # ▶ [5, 10, 'Multiple of 5', 20, 25]"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": 3,
91 | "metadata": {
92 | "ExecuteTime": {
93 | "end_time": "2021-10-03T09:17:26.102368Z",
94 | "start_time": "2021-10-03T09:17:26.088696Z"
95 | },
96 | "scrolled": true
97 | },
98 | "outputs": [
99 | {
100 | "name": "stdout",
101 | "output_type": "stream",
102 | "text": [
103 | "[1, 3, 5, 7]\n",
104 | "[1, 3, 5, 7, 9, 11, 13]\n"
105 | ]
106 | }
107 | ],
108 | "source": [
109 | "# Example: Appending and Extending lists in Python\n",
110 | "\n",
111 | "odd = [1, 3, 5]\n",
112 | "\n",
113 | "odd.append(7)\n",
114 | "print(odd) # ▶ [1, 3, 5, 7]\n",
115 | "\n",
116 | "odd.extend([9, 11, 13])\n",
117 | "print(odd) # ▶ [1, 3, 5, 7, 9, 11, 13]"
118 | ]
119 | },
120 | {
121 | "cell_type": "code",
122 | "execution_count": 4,
123 | "metadata": {
124 | "ExecuteTime": {
125 | "end_time": "2021-10-03T09:17:26.411450Z",
126 | "start_time": "2021-10-03T09:17:26.383133Z"
127 | }
128 | },
129 | "outputs": [
130 | {
131 | "name": "stdout",
132 | "output_type": "stream",
133 | "text": [
134 | "['a', 'b', 'c', 1.5, 'x', ['y', 'z']]\n"
135 | ]
136 | }
137 | ],
138 | "source": [
139 | "# Example:\n",
140 | "\n",
141 | "list1=['a','b','c']\n",
142 | "list1.append(1.5)\n",
143 | "list1.append('x')\n",
144 | "list1.append(['y','z']) # append list into list as single object\n",
145 | "print(list1) # ▶ ['a', 'b', 'c', 1.5, 'x', ['y', 'z']]"
146 | ]
147 | },
148 | {
149 | "cell_type": "code",
150 | "execution_count": 5,
151 | "metadata": {
152 | "ExecuteTime": {
153 | "end_time": "2021-10-03T09:17:27.189775Z",
154 | "start_time": "2021-10-03T09:17:27.169273Z"
155 | }
156 | },
157 | "outputs": [
158 | {
159 | "name": "stdout",
160 | "output_type": "stream",
161 | "text": [
162 | "[1, 3, 5, 9, 7, 5]\n"
163 | ]
164 | }
165 | ],
166 | "source": [
167 | "# Example: Concatenating and repeating lists\n",
168 | "\n",
169 | "odd = [1, 3, 5]\n",
170 | "print(odd + [9, 7, 5]) # ▶ [1, 3, 5, 9, 7, 5] "
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": 6,
176 | "metadata": {
177 | "ExecuteTime": {
178 | "end_time": "2021-10-03T09:17:27.402669Z",
179 | "start_time": "2021-10-03T09:17:27.383145Z"
180 | },
181 | "scrolled": false
182 | },
183 | "outputs": [
184 | {
185 | "name": "stdout",
186 | "output_type": "stream",
187 | "text": [
188 | "['a', 'b', 'c', 'x', 'y', 'z']\n"
189 | ]
190 | }
191 | ],
192 | "source": [
193 | "# Example:\n",
194 | "\n",
195 | "list1=['a','b','c']\n",
196 | "list2=['x','y','z']\n",
197 | "list3=list1+list2\n",
198 | "print(list3) # ▶ ['a', 'b', 'c', 'x', 'y', 'z']"
199 | ]
200 | },
201 | {
202 | "cell_type": "code",
203 | "execution_count": 7,
204 | "metadata": {
205 | "ExecuteTime": {
206 | "end_time": "2021-10-03T09:17:27.828941Z",
207 | "start_time": "2021-10-03T09:17:27.750817Z"
208 | },
209 | "scrolled": true
210 | },
211 | "outputs": [
212 | {
213 | "ename": "TypeError",
214 | "evalue": "can only concatenate list (not \"str\") to list",
215 | "output_type": "error",
216 | "traceback": [
217 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
218 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
219 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_17120/1305928048.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mlist1\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'a'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'b'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'c'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'x'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlist1\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# ▶ TypeError: can only concatenate list (not \"str\") to list\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
220 | "\u001b[1;31mTypeError\u001b[0m: can only concatenate list (not \"str\") to list"
221 | ]
222 | }
223 | ],
224 | "source": [
225 | "# Example:\n",
226 | "\n",
227 | "list1=['a','b','c']\n",
228 | "a='x'\n",
229 | "print(list1+a) # ▶ TypeError: can only concatenate list (not \"str\") to list"
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "execution_count": 8,
235 | "metadata": {
236 | "ExecuteTime": {
237 | "end_time": "2021-10-03T09:17:28.334314Z",
238 | "start_time": "2021-10-03T09:17:28.322599Z"
239 | }
240 | },
241 | "outputs": [
242 | {
243 | "name": "stdout",
244 | "output_type": "stream",
245 | "text": [
246 | "[1, 3, 5, 9, 7, 5]\n",
247 | "['re', 're', 're']\n"
248 | ]
249 | }
250 | ],
251 | "source": [
252 | "# Example: Concatenating and repeating lists\n",
253 | "\n",
254 | "odd = [1, 3, 5]\n",
255 | "\n",
256 | "print(odd + [9, 7, 5]) # ▶ [1, 3, 5, 9, 7, 5]\n",
257 | "print([\"re\"] * 3) # ▶ ['re', 're', 're']"
258 | ]
259 | },
260 | {
261 | "cell_type": "code",
262 | "execution_count": 9,
263 | "metadata": {
264 | "ExecuteTime": {
265 | "end_time": "2021-10-03T09:17:28.534510Z",
266 | "start_time": "2021-10-03T09:17:28.519867Z"
267 | }
268 | },
269 | "outputs": [
270 | {
271 | "name": "stdout",
272 | "output_type": "stream",
273 | "text": [
274 | "['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']\n",
275 | "['re', 're', 're']\n"
276 | ]
277 | }
278 | ],
279 | "source": [
280 | "# Example:\n",
281 | "\n",
282 | "list1=['a','b','c']\n",
283 | "print(list1*3) # ▶ quux ∵ \n",
284 | "print([\"re\"] * 3) # ▶ quux ∵ "
285 | ]
286 | },
287 | {
288 | "cell_type": "code",
289 | "execution_count": 10,
290 | "metadata": {
291 | "ExecuteTime": {
292 | "end_time": "2021-10-03T09:17:29.052094Z",
293 | "start_time": "2021-10-03T09:17:29.043306Z"
294 | }
295 | },
296 | "outputs": [
297 | {
298 | "name": "stdout",
299 | "output_type": "stream",
300 | "text": [
301 | "[1, 3, 9]\n",
302 | "[1, 3, 5, 7, 9]\n"
303 | ]
304 | }
305 | ],
306 | "source": [
307 | "# Example: Demonstration of list insert() method\n",
308 | "\n",
309 | "odd = [1, 9]\n",
310 | "odd.insert(1,3)\n",
311 | "print(odd) # ▶ [1, 3, 9] \n",
312 | "\n",
313 | "odd[2:2] = [5, 7]\n",
314 | "print(odd) # ▶ [1, 3, 5, 7, 9]"
315 | ]
316 | },
317 | {
318 | "cell_type": "markdown",
319 | "metadata": {},
320 | "source": [
321 | "### Delete/Remove List Elements\n",
322 | "\n"
323 | ]
324 | },
325 | {
326 | "cell_type": "code",
327 | "execution_count": 11,
328 | "metadata": {
329 | "ExecuteTime": {
330 | "end_time": "2021-10-03T09:17:29.481784Z",
331 | "start_time": "2021-10-03T09:17:29.461278Z"
332 | },
333 | "scrolled": true
334 | },
335 | "outputs": [
336 | {
337 | "name": "stdout",
338 | "output_type": "stream",
339 | "text": [
340 | "['p', 'r', 'b', 'l', 'e', 'm']\n",
341 | "['p', 'm']\n"
342 | ]
343 | },
344 | {
345 | "ename": "NameError",
346 | "evalue": "name 'my_list' is not defined",
347 | "output_type": "error",
348 | "traceback": [
349 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
350 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
351 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_17120/2528350536.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mmy_list\u001b[0m \u001b[1;31m# delete entire list\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmy_list\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# ▶ NameError: name 'my_list' is not defined\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
352 | "\u001b[1;31mNameError\u001b[0m: name 'my_list' is not defined"
353 | ]
354 | }
355 | ],
356 | "source": [
357 | "# Example: Deleting list items\n",
358 | "\n",
359 | "my_list = ['p', 'r', 'o', 'b', 'l', 'e', 'm']\n",
360 | "\n",
361 | "del my_list[2] # delete one item\n",
362 | "print(my_list) # ▶ ['p', 'r', 'b', 'l', 'e', 'm'] \n",
363 | "\n",
364 | "del my_list[1:5] # delete multiple items\n",
365 | "print(my_list) # ▶ ['p', 'm']\n",
366 | "\n",
367 | "del my_list # delete entire list\n",
368 | "print(my_list) # ▶ NameError: name 'my_list' is not defined"
369 | ]
370 | },
371 | {
372 | "cell_type": "code",
373 | "execution_count": 12,
374 | "metadata": {
375 | "ExecuteTime": {
376 | "end_time": "2021-10-03T09:17:29.698583Z",
377 | "start_time": "2021-10-03T09:17:29.687842Z"
378 | },
379 | "scrolled": true
380 | },
381 | "outputs": [
382 | {
383 | "name": "stdout",
384 | "output_type": "stream",
385 | "text": [
386 | "data in list : ['a', 'b', 'c']\n",
387 | "new data in list : ['a', 'b']\n"
388 | ]
389 | }
390 | ],
391 | "source": [
392 | "# Example:\n",
393 | "\n",
394 | "list1=['a','b','c']\n",
395 | "print(\"data in list : \",list1) # ▶ data in list : ['a', 'b', 'c']\n",
396 | "\n",
397 | "del(list1[2])\n",
398 | "print(\"new data in list : \",list1) # ▶ new data in list : ['a', 'b']"
399 | ]
400 | },
401 | {
402 | "cell_type": "code",
403 | "execution_count": 13,
404 | "metadata": {
405 | "ExecuteTime": {
406 | "end_time": "2021-10-03T09:17:30.111668Z",
407 | "start_time": "2021-10-03T09:17:30.093604Z"
408 | }
409 | },
410 | "outputs": [
411 | {
412 | "name": "stdout",
413 | "output_type": "stream",
414 | "text": [
415 | "['r', 'o', 'b', 'l', 'e', 'm']\n",
416 | "o\n",
417 | "['r', 'b', 'l', 'e', 'm']\n",
418 | "m\n",
419 | "['r', 'b', 'l', 'e']\n",
420 | "[]\n"
421 | ]
422 | }
423 | ],
424 | "source": [
425 | "# Example:\n",
426 | "\n",
427 | "my_list = ['p','r','o','b','l','e','m']\n",
428 | "my_list.remove('p')\n",
429 | "\n",
430 | "print(my_list) # ▶ ['r', 'o', 'b', 'l', 'e', 'm']\n",
431 | "print(my_list.pop(1)) # ▶ 'o'\n",
432 | "print(my_list) # ▶ ['r', 'b', 'l', 'e', 'm']\n",
433 | "print(my_list.pop()) # ▶ 'm'\n",
434 | "print(my_list) # ▶ ['r', 'b', 'l', 'e']\n",
435 | "\n",
436 | "my_list.clear()\n",
437 | "print(my_list) # ▶ []"
438 | ]
439 | },
440 | {
441 | "cell_type": "code",
442 | "execution_count": 14,
443 | "metadata": {
444 | "ExecuteTime": {
445 | "end_time": "2021-10-03T09:17:30.608254Z",
446 | "start_time": "2021-10-03T09:17:30.586772Z"
447 | }
448 | },
449 | "outputs": [
450 | {
451 | "name": "stdout",
452 | "output_type": "stream",
453 | "text": [
454 | "['p', 'r', 'b', 'l', 'e', 'm']\n",
455 | "['p', 'r', 'e', 'm']\n"
456 | ]
457 | }
458 | ],
459 | "source": [
460 | "# Example:\n",
461 | "\n",
462 | "my_list = ['p','r','o','b','l','e','m']\n",
463 | "\n",
464 | "my_list[2:3] = []\n",
465 | "print(my_list) # ▶ ['p', 'r', 'b', 'l', 'e', 'm']\n",
466 | "\n",
467 | "my_list[2:-2] = []\n",
468 | "print(my_list) # ▶ ['p', 'r', 'e', 'm']"
469 | ]
470 | },
471 | {
472 | "cell_type": "code",
473 | "execution_count": null,
474 | "metadata": {},
475 | "outputs": [],
476 | "source": []
477 | }
478 | ],
479 | "metadata": {
480 | "hide_input": false,
481 | "kernelspec": {
482 | "display_name": "Python 3 (ipykernel)",
483 | "language": "python",
484 | "name": "python3"
485 | },
486 | "language_info": {
487 | "codemirror_mode": {
488 | "name": "ipython",
489 | "version": 3
490 | },
491 | "file_extension": ".py",
492 | "mimetype": "text/x-python",
493 | "name": "python",
494 | "nbconvert_exporter": "python",
495 | "pygments_lexer": "ipython3",
496 | "version": "3.9.13"
497 | },
498 | "toc": {
499 | "base_numbering": 1,
500 | "nav_menu": {},
501 | "number_sections": true,
502 | "sideBar": true,
503 | "skip_h1_title": false,
504 | "title_cell": "Table of Contents",
505 | "title_sidebar": "Contents",
506 | "toc_cell": false,
507 | "toc_position": {},
508 | "toc_section_display": true,
509 | "toc_window_display": false
510 | },
511 | "varInspector": {
512 | "cols": {
513 | "lenName": 16,
514 | "lenType": 16,
515 | "lenVar": 40
516 | },
517 | "kernels_config": {
518 | "python": {
519 | "delete_cmd_postfix": "",
520 | "delete_cmd_prefix": "del ",
521 | "library": "var_list.py",
522 | "varRefreshCmd": "print(var_dic_list())"
523 | },
524 | "r": {
525 | "delete_cmd_postfix": ") ",
526 | "delete_cmd_prefix": "rm(",
527 | "library": "var_list.r",
528 | "varRefreshCmd": "cat(var_dic_list()) "
529 | }
530 | },
531 | "types_to_exclude": [
532 | "module",
533 | "function",
534 | "builtin_function_or_method",
535 | "instance",
536 | "_Feature"
537 | ],
538 | "window_display": false
539 | }
540 | },
541 | "nbformat": 4,
542 | "nbformat_minor": 2
543 | }
544 |
--------------------------------------------------------------------------------
/Day 20_Python_Dictionary.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Dictionary\n",
8 | "\n",
9 | "In this session, you'll learn everything about Python dictionaries."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## What is Dictionary in Python?\n",
17 | "\n",
18 | "\n",
19 | "\n",
20 | "\n",
21 | "

\n",
22 | "
\n",
23 | "\n",
24 | "**Example:**\n",
25 | "\n",
26 | "```python\n",
27 | ">>> dict = { } #empty dictionary\n",
28 | ">>> dict = {1:'Python',2:'Java',3:'C++'}\n",
29 | "```\n",
30 | "\n"
31 | ]
32 | },
33 | {
34 | "cell_type": "markdown",
35 | "metadata": {},
36 | "source": [
37 | "\n",
38 | "\n",
39 | "| Data types | Type | |\n",
40 | "| :------------: | :-----------: |:------: |\n",
41 | "| **String** | **immutable** | |\n",
42 | "| **List** | **mutable** |✎|\n",
43 | "| **Tuple** | **immutable** | |\n",
44 | "| **Dictionary** | **mutable** |✎|"
45 | ]
46 | },
47 | {
48 | "cell_type": "markdown",
49 | "metadata": {},
50 | "source": [
51 | "## Creating Python Dictionary\n",
52 | "\n"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 1,
58 | "metadata": {
59 | "ExecuteTime": {
60 | "end_time": "2021-10-03T15:24:34.175014Z",
61 | "start_time": "2021-10-03T15:24:34.150602Z"
62 | }
63 | },
64 | "outputs": [
65 | {
66 | "name": "stdout",
67 | "output_type": "stream",
68 | "text": [
69 | "{} [] () \n",
70 | " \n",
71 | "{1: 'apple', 2: 'ball'}\n",
72 | "{1: 'hi', 'name': 666, 1.5: ('yes', 'very much'), 9: [3, 6, 9]}\n",
73 | "\n",
74 | "{1: 'apple', 2: 'ball'}\n",
75 | "2\n"
76 | ]
77 | }
78 | ],
79 | "source": [
80 | "# Example: empty dictionary\n",
81 | "\n",
82 | "d = {}; l = []; t = (); st = \"\"\n",
83 | "print(d,l,t,st) # ▶ {} [] () \n",
84 | "print(type(d),type(l),type(t), type(st)) \n",
85 | "# ▶ \n",
86 | "\n",
87 | "# dictionary with integer keys\n",
88 | "my_dict1 = {1: 'apple', 2: 'ball'}\n",
89 | "print(my_dict1) # ▶ {1: 'apple', 2: 'ball'}\n",
90 | "\n",
91 | "# dictionary with mixed keys\n",
92 | "my_dict2 = {1:\"hi\", \"name\":666, 1.5:(\"yes\",\"very much\"), 9: [3, 6, 9]}\n",
93 | "print(my_dict2) # ▶ {1: 'hi', 'name': 666, 1.5: ('yes', 'very much'), 9: [3, 6, 9]}\n",
94 | "\n",
95 | "# from sequence having each item as a pair\n",
96 | "my_dict3 = dict([(1,'apple'), (2,'ball')])\n",
97 | "print(type([(1,'apple'), (2,'ball')])) # nested list\n",
98 | "print(my_dict3) # ▶ {1: 'apple', 2: 'ball'}\n",
99 | "print(len(my_dict3)) # ▶ 2"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 2,
105 | "metadata": {
106 | "scrolled": true
107 | },
108 | "outputs": [
109 | {
110 | "data": {
111 | "text/plain": [
112 | "({1: 100, 'bar': 200}, {'foo': 100, 'bar': 200})"
113 | ]
114 | },
115 | "execution_count": 2,
116 | "metadata": {},
117 | "output_type": "execute_result"
118 | }
119 | ],
120 | "source": [
121 | "dict([(1, 100), ('bar', 200)]), dict(foo=100, bar=200)"
122 | ]
123 | },
124 | {
125 | "cell_type": "markdown",
126 | "metadata": {},
127 | "source": [
128 | "## Accessing Elements from Dictionary\n",
129 | "\n"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": 3,
135 | "metadata": {
136 | "ExecuteTime": {
137 | "end_time": "2021-10-03T15:24:34.899634Z",
138 | "start_time": "2021-10-03T15:24:34.177947Z"
139 | }
140 | },
141 | "outputs": [
142 | {
143 | "name": "stdout",
144 | "output_type": "stream",
145 | "text": [
146 | "Python\n",
147 | "Gods language\n",
148 | "None\n"
149 | ]
150 | },
151 | {
152 | "ename": "KeyError",
153 | "evalue": "4",
154 | "output_type": "error",
155 | "traceback": [
156 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
157 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
158 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_13616/2094585495.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmy_dict\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# ▶ None\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmy_dict\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# ▶ KeyError!\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
159 | "\u001b[1;31mKeyError\u001b[0m: 4"
160 | ]
161 | }
162 | ],
163 | "source": [
164 | "# Example: get vs [] for retrieving elements\n",
165 | "\n",
166 | "my_dict = {1:'Python', 2:'Java', 3:'C++', 'c': 'Gods language'}\n",
167 | "\n",
168 | "# method: 1\n",
169 | "print(my_dict[1]) # ▶ Python\n",
170 | "\n",
171 | "# method: 2\n",
172 | "print(my_dict.get('c')) # ▶ Gods language\n",
173 | "\n",
174 | "# Trying to access keys which doesn't exist in dictionary:\n",
175 | "print(my_dict.get(4)) # ▶ None\n",
176 | "\n",
177 | "print(my_dict[4]) # ▶ KeyError!"
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "execution_count": null,
183 | "metadata": {
184 | "ExecuteTime": {
185 | "end_time": "2021-10-03T15:24:46.489676Z",
186 | "start_time": "2021-10-03T15:24:46.473079Z"
187 | },
188 | "scrolled": true
189 | },
190 | "outputs": [],
191 | "source": [
192 | "# Example:\n",
193 | "\n",
194 | "dict = {1:'Python', 2:'Java', 3:'C++', 'c': 'Gods language'}\n",
195 | "print(dict.keys())\n",
196 | "for x in dict.keys():\n",
197 | " print(dict[x])"
198 | ]
199 | },
200 | {
201 | "cell_type": "markdown",
202 | "metadata": {},
203 | "source": [
204 | "## Changing and Adding Dictionary elements\n",
205 | "\n"
206 | ]
207 | },
208 | {
209 | "cell_type": "code",
210 | "execution_count": 4,
211 | "metadata": {
212 | "ExecuteTime": {
213 | "end_time": "2021-10-03T15:24:47.367623Z",
214 | "start_time": "2021-10-03T15:24:47.345164Z"
215 | }
216 | },
217 | "outputs": [
218 | {
219 | "name": "stdout",
220 | "output_type": "stream",
221 | "text": [
222 | "{'name': 'Arthur', 'age': 25}\n",
223 | "{'name': 'Arthur', 'age': 25, 'address': 'Downtown'}\n"
224 | ]
225 | }
226 | ],
227 | "source": [
228 | "# Example 1: Changing and adding Dictionary Elements\n",
229 | "\n",
230 | "my_dict = {'name':'Arthur', 'age':24}\n",
231 | "\n",
232 | "my_dict['age'] = 25 # update value\n",
233 | "print(my_dict) # ▶ {'age': 25, 'name': 'Arthur'}\n",
234 | "\n",
235 | "my_dict['address'] = 'Downtown' # add item\n",
236 | "print(my_dict) # ▶ {'name': 'Arthur', 'age': 25, 'address': 'Downtown'}"
237 | ]
238 | },
239 | {
240 | "cell_type": "code",
241 | "execution_count": 5,
242 | "metadata": {
243 | "ExecuteTime": {
244 | "end_time": "2021-10-03T15:24:47.707473Z",
245 | "start_time": "2021-10-03T15:24:47.698687Z"
246 | }
247 | },
248 | "outputs": [
249 | {
250 | "name": "stdout",
251 | "output_type": "stream",
252 | "text": [
253 | "{1: 'Python', 2: 'Java', 3: 'R', 4: 'PHP'}\n"
254 | ]
255 | }
256 | ],
257 | "source": [
258 | "# Example 2: \n",
259 | "\n",
260 | "my_dict = {1:'Python', 2:'Java', 3:'C++'}\n",
261 | "my_dict[3]=\"R\" # update value\n",
262 | "my_dict[4]=\"PHP\" # insert new value\n",
263 | "print(my_dict) # ▶ {1: 'Python', 2: 'Java', 3: 'R', 4: 'PHP'}"
264 | ]
265 | },
266 | {
267 | "cell_type": "markdown",
268 | "metadata": {},
269 | "source": [
270 | "## Removing elements from Dictionary\n",
271 | "\n"
272 | ]
273 | },
274 | {
275 | "cell_type": "code",
276 | "execution_count": 6,
277 | "metadata": {
278 | "ExecuteTime": {
279 | "end_time": "2021-10-03T15:24:48.339320Z",
280 | "start_time": "2021-10-03T15:24:48.319789Z"
281 | }
282 | },
283 | "outputs": [
284 | {
285 | "name": "stdout",
286 | "output_type": "stream",
287 | "text": [
288 | "16\n",
289 | "{1: 1, 2: 4, 3: 9, 5: 25}\n",
290 | "(5, 25)\n",
291 | "{1: 1, 2: 4, 3: 9}\n",
292 | "{}\n"
293 | ]
294 | },
295 | {
296 | "ename": "NameError",
297 | "evalue": "name 'squares' is not defined",
298 | "output_type": "error",
299 | "traceback": [
300 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
301 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
302 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_13616/829455253.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0msquares\u001b[0m \u001b[1;31m# delete the dictionary itself\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msquares\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# ▶ NameError!!!\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
303 | "\u001b[1;31mNameError\u001b[0m: name 'squares' is not defined"
304 | ]
305 | }
306 | ],
307 | "source": [
308 | "# Example: Removing elements from a dictionary\n",
309 | "\n",
310 | "\n",
311 | "squares = {1:1, 2:4, 3:9, 4:16, 5:25} # create a dictionary\n",
312 | "\n",
313 | "# remove a particular item, returns its value\n",
314 | "print(squares.pop(4)) # ▶ 16\n",
315 | "print(squares) # ▶ {1: 1, 2: 4, 3: 9, 5: 25}\n",
316 | "\n",
317 | "# remove an arbitrary item, return (key,value)\n",
318 | "print(squares.popitem()) # ▶ (5, 25)\n",
319 | "print(squares) # ▶ {1: 1, 2: 4, 3: 9}\n",
320 | "\n",
321 | "squares.clear() # remove all items\n",
322 | "print(squares) # ▶ {}\n",
323 | "\n",
324 | "del squares # delete the dictionary itself\n",
325 | "print(squares) # ▶ NameError!!!"
326 | ]
327 | },
328 | {
329 | "cell_type": "code",
330 | "execution_count": 7,
331 | "metadata": {
332 | "ExecuteTime": {
333 | "end_time": "2021-10-03T15:24:48.676242Z",
334 | "start_time": "2021-10-03T15:24:48.649873Z"
335 | },
336 | "scrolled": true
337 | },
338 | "outputs": [
339 | {
340 | "name": "stdout",
341 | "output_type": "stream",
342 | "text": [
343 | "{1: 'Python', 2: 'Java', 4: 'PHP'}\n",
344 | "my_dict : {}\n"
345 | ]
346 | },
347 | {
348 | "ename": "NameError",
349 | "evalue": "name 'my_dict' is not defined",
350 | "output_type": "error",
351 | "traceback": [
352 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
353 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
354 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_13616/2219691608.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mmy_dict\u001b[0m \u001b[1;31m# delete entire dictionary\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmy_dict\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# ▶ NameError!!!\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
355 | "\u001b[1;31mNameError\u001b[0m: name 'my_dict' is not defined"
356 | ]
357 | }
358 | ],
359 | "source": [
360 | "# Example:\n",
361 | "\n",
362 | "my_dict = {1:'Python', 2:'Java', 3:'C++', 4:'PHP'}\n",
363 | "del my_dict[3] # ▶ {1: 'Python', 2: 'Java', 4: 'PHP'} ∵ remove entry with key '3'\n",
364 | "print(my_dict) # ▶ {}\n",
365 | "\n",
366 | "my_dict.clear() # remove all entries in dict\n",
367 | "print(\"my_dict : \",my_dict) # ▶ my_dict : {}\n",
368 | "\n",
369 | "del my_dict # delete entire dictionary\n",
370 | "print(my_dict[2]) # ▶ NameError!!!"
371 | ]
372 | },
373 | {
374 | "cell_type": "code",
375 | "execution_count": null,
376 | "metadata": {},
377 | "outputs": [],
378 | "source": []
379 | }
380 | ],
381 | "metadata": {
382 | "hide_input": false,
383 | "kernelspec": {
384 | "display_name": "Python 3 (ipykernel)",
385 | "language": "python",
386 | "name": "python3"
387 | },
388 | "language_info": {
389 | "codemirror_mode": {
390 | "name": "ipython",
391 | "version": 3
392 | },
393 | "file_extension": ".py",
394 | "mimetype": "text/x-python",
395 | "name": "python",
396 | "nbconvert_exporter": "python",
397 | "pygments_lexer": "ipython3",
398 | "version": "3.9.13"
399 | },
400 | "toc": {
401 | "base_numbering": 1,
402 | "nav_menu": {},
403 | "number_sections": true,
404 | "sideBar": true,
405 | "skip_h1_title": false,
406 | "title_cell": "Table of Contents",
407 | "title_sidebar": "Contents",
408 | "toc_cell": false,
409 | "toc_position": {},
410 | "toc_section_display": true,
411 | "toc_window_display": false
412 | },
413 | "varInspector": {
414 | "cols": {
415 | "lenName": 16,
416 | "lenType": 16,
417 | "lenVar": 40
418 | },
419 | "kernels_config": {
420 | "python": {
421 | "delete_cmd_postfix": "",
422 | "delete_cmd_prefix": "del ",
423 | "library": "var_list.py",
424 | "varRefreshCmd": "print(var_dic_list())"
425 | },
426 | "r": {
427 | "delete_cmd_postfix": ") ",
428 | "delete_cmd_prefix": "rm(",
429 | "library": "var_list.r",
430 | "varRefreshCmd": "cat(var_dic_list()) "
431 | }
432 | },
433 | "types_to_exclude": [
434 | "module",
435 | "function",
436 | "builtin_function_or_method",
437 | "instance",
438 | "_Feature"
439 | ],
440 | "window_display": false
441 | }
442 | },
443 | "nbformat": 4,
444 | "nbformat_minor": 2
445 | }
446 |
--------------------------------------------------------------------------------
/Day 22_Python_Sets.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Sets\n",
8 | "\n",
9 | "In this session, you'll learn everything about Python sets."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## What is Set in Python?\n",
17 | "\n"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "\n",
25 | "\n",
26 | "| Data types | Type | |\n",
27 | "| :------------: | :-----------: |:------: |\n",
28 | "| **String** | **immutable** | |\n",
29 | "| **List** | **mutable** |✎|\n",
30 | "| **Tuple** | **immutable** | |\n",
31 | "| **Dictionary** | **mutable** |✎|\n",
32 | "| **Set** | **immutable** | |"
33 | ]
34 | },
35 | {
36 | "cell_type": "markdown",
37 | "metadata": {},
38 | "source": [
39 | "## Creating Python Sets\n",
40 | "\n"
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "execution_count": 1,
46 | "metadata": {
47 | "ExecuteTime": {
48 | "end_time": "2021-10-03T14:22:15.713135Z",
49 | "start_time": "2021-10-03T14:22:15.702393Z"
50 | }
51 | },
52 | "outputs": [
53 | {
54 | "name": "stdout",
55 | "output_type": "stream",
56 | "text": [
57 | "{1, 2, 3, 5, 7}\n",
58 | "{1.0, (1, 2, 3), 'Hello'}\n"
59 | ]
60 | }
61 | ],
62 | "source": [
63 | "# Example 1: Different types of sets in Python\n",
64 | "\n",
65 | "# set of integers\n",
66 | "my_set = {5, 7, 1, 2, 3} \n",
67 | "print(my_set) # ▶ {1, 2, 3, 5, 7}\n",
68 | " \n",
69 | "# set of mixed datatypes\n",
70 | "my_set = {1.0, \"Hello\", (1, 2, 3)} \n",
71 | "print(my_set) # ▶ {1.0, 'Hello', (1, 2, 3)}"
72 | ]
73 | },
74 | {
75 | "cell_type": "code",
76 | "execution_count": 2,
77 | "metadata": {
78 | "ExecuteTime": {
79 | "end_time": "2021-10-03T14:22:16.310326Z",
80 | "start_time": "2021-10-03T14:22:15.727787Z"
81 | },
82 | "scrolled": false
83 | },
84 | "outputs": [
85 | {
86 | "name": "stdout",
87 | "output_type": "stream",
88 | "text": [
89 | "{1, 2, 3, 4}\n",
90 | "{1, 2, 3}\n",
91 | "{1, 2, (3, 4)}\n"
92 | ]
93 | },
94 | {
95 | "ename": "TypeError",
96 | "evalue": "unhashable type: 'list'",
97 | "output_type": "error",
98 | "traceback": [
99 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
100 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
101 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_22104/2060923627.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[1;31m# here [3, 4] is a mutable list\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[0mmy_set\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m}\u001b[0m \u001b[1;31m# this will cause an error ∵ list is mutable\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
102 | "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'"
103 | ]
104 | }
105 | ],
106 | "source": [
107 | "# Example 2: Different types of sets in Python\n",
108 | "\n",
109 | "# set cannot have duplicates\n",
110 | "my_set = {1, 2, 3, 4, 3, 2}\n",
111 | "print(my_set) # ▶ {1, 2, 3, 4}\n",
112 | "\n",
113 | "# we can make set from a list\n",
114 | "my_set = set([1, 2, 3, 2])\n",
115 | "print(my_set) # ▶ {1, 2, 3}\n",
116 | "\n",
117 | "# set can have immutable items\n",
118 | "# here (3, 4) is a immutable list\n",
119 | "\n",
120 | "my_set = {1, 2, (3, 4)} \n",
121 | "print(my_set) # ▶ {1, 2, (3, 4)} ∵ tuple is immutable\n",
122 | "\n",
123 | "# set cannot have mutable items\n",
124 | "# here [3, 4] is a mutable list\n",
125 | "\n",
126 | "my_set = {1, 2, [3, 4]} # this will cause an error ∵ list is mutable"
127 | ]
128 | },
129 | {
130 | "cell_type": "markdown",
131 | "metadata": {},
132 | "source": [
133 | "#### Creating an empty set is a bit tricky.\n",
134 | "\n"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": 3,
140 | "metadata": {
141 | "ExecuteTime": {
142 | "end_time": "2021-10-03T14:22:27.149150Z",
143 | "start_time": "2021-10-03T14:22:27.142314Z"
144 | }
145 | },
146 | "outputs": [
147 | {
148 | "name": "stdout",
149 | "output_type": "stream",
150 | "text": [
151 | "\n",
152 | "\n"
153 | ]
154 | }
155 | ],
156 | "source": [
157 | "# Example: Distinguish set and dictionary while creating empty set\n",
158 | "\n",
159 | "# initialize a with {}\n",
160 | "a = {}\n",
161 | "\n",
162 | "# check data type of a\n",
163 | "print(type(a)) # ▶ ∵ dict also use {}\n",
164 | "\n",
165 | "# initialize a with set()\n",
166 | "a = set()\n",
167 | "\n",
168 | "# check data type of a\n",
169 | "print(type(a)) # ▶ "
170 | ]
171 | },
172 | {
173 | "cell_type": "markdown",
174 | "metadata": {},
175 | "source": [
176 | "## Modifying a set in Python\n",
177 | "\n"
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "execution_count": 4,
183 | "metadata": {
184 | "ExecuteTime": {
185 | "end_time": "2021-10-03T14:22:27.982190Z",
186 | "start_time": "2021-10-03T14:22:27.971452Z"
187 | },
188 | "scrolled": true
189 | },
190 | "outputs": [
191 | {
192 | "name": "stdout",
193 | "output_type": "stream",
194 | "text": [
195 | "{1, 3}\n",
196 | "{1, 2, 3}\n",
197 | "{1, 2, 3, 4}\n",
198 | "{1, 2, 3, 4, 5, 7, 8, 9, 10}\n"
199 | ]
200 | }
201 | ],
202 | "source": [
203 | "# Example: \n",
204 | "\n",
205 | "# initialize my_set\n",
206 | "my_set = {1, 3}\n",
207 | "print(my_set) # ▶ {1, 3}\n",
208 | "\n",
209 | "# my_set[0]\n",
210 | "# if you uncomment above line, you will get an error\n",
211 | "# TypeError: 'set' object does not support indexing\n",
212 | "\n",
213 | "# add an element\n",
214 | "my_set.add(2)\n",
215 | "print(my_set) # ▶ {1, 2, 3}\n",
216 | "\n",
217 | "# add multiple elements\n",
218 | "my_set.update([2, 3, 4])\n",
219 | "print(my_set) # ▶ {1, 2, 3, 4}\n",
220 | "\n",
221 | "# add list and set\n",
222 | "my_set.update([1,7],(8,9,10),{1,5,9,10})\n",
223 | "print(my_set) # ▶ {1, 2, 3, 4, 5, 7, 8, 9, 10}"
224 | ]
225 | },
226 | {
227 | "cell_type": "markdown",
228 | "metadata": {},
229 | "source": [
230 | "## Removing elements from a set\n",
231 | "\n"
232 | ]
233 | },
234 | {
235 | "cell_type": "code",
236 | "execution_count": 5,
237 | "metadata": {
238 | "ExecuteTime": {
239 | "end_time": "2021-10-03T14:22:28.800585Z",
240 | "start_time": "2021-10-03T14:22:28.731249Z"
241 | }
242 | },
243 | "outputs": [
244 | {
245 | "name": "stdout",
246 | "output_type": "stream",
247 | "text": [
248 | "{1, 3, 4, 5, 6}\n",
249 | "{1, 3, 5, 6}\n",
250 | "{1, 3, 5}\n",
251 | "{1, 3, 5}\n"
252 | ]
253 | },
254 | {
255 | "ename": "KeyError",
256 | "evalue": "2",
257 | "output_type": "error",
258 | "traceback": [
259 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
260 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
261 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_22104/2721775954.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;31m# remove an element not present in my_set you will get an error.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[0mmy_set\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# ▶ KeyError!!!\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
262 | "\u001b[1;31mKeyError\u001b[0m: 2"
263 | ]
264 | }
265 | ],
266 | "source": [
267 | "# Example: Difference between discard() and remove()\n",
268 | "\n",
269 | "# initialize my_set\n",
270 | "my_set = {1, 3, 4, 5, 6}\n",
271 | "print(my_set) # ▶ {1, 3, 4, 5, 6}\n",
272 | "\n",
273 | "# discard an element\n",
274 | "my_set.discard(4)\n",
275 | "print(my_set) # ▶ {1, 3, 5, 6}\n",
276 | "\n",
277 | "# remove an element\n",
278 | "my_set.remove(6)\n",
279 | "print(my_set) # ▶ {1, 3, 5}\n",
280 | "\n",
281 | "# discard an element not present in my_set\n",
282 | "my_set.discard(2)\n",
283 | "print(my_set) # ▶ {1, 3, 5} NO ERROR!\n",
284 | "\n",
285 | "# remove an element not present in my_set you will get an error.\n",
286 | "my_set.remove(2) # ▶ KeyError!!!"
287 | ]
288 | },
289 | {
290 | "cell_type": "code",
291 | "execution_count": 6,
292 | "metadata": {
293 | "ExecuteTime": {
294 | "end_time": "2021-10-03T14:22:29.430491Z",
295 | "start_time": "2021-10-03T14:22:29.419752Z"
296 | }
297 | },
298 | "outputs": [
299 | {
300 | "name": "stdout",
301 | "output_type": "stream",
302 | "text": [
303 | "{'l', 'r', 'e', 'o', 'd', 'H', 'W'}\n",
304 | "l\n",
305 | "{'e', 'o', 'd', 'H', 'W'}\n",
306 | "set()\n"
307 | ]
308 | }
309 | ],
310 | "source": [
311 | "# Example:\n",
312 | "\n",
313 | "# initialize my_set\n",
314 | "my_set = set(\"HelloWorld\")\n",
315 | "print(my_set) # ▶ unorderd set of unique elements\n",
316 | "\n",
317 | "# pop an element\n",
318 | "print(my_set.pop()) # ▶ removes a random element\n",
319 | "\n",
320 | "# pop another element\n",
321 | "my_set.pop()\n",
322 | "print(my_set)\n",
323 | "\n",
324 | "# clear my_set\n",
325 | "my_set.clear()\n",
326 | "print(my_set) # ▶ set()"
327 | ]
328 | },
329 | {
330 | "cell_type": "code",
331 | "execution_count": null,
332 | "metadata": {},
333 | "outputs": [],
334 | "source": []
335 | }
336 | ],
337 | "metadata": {
338 | "hide_input": false,
339 | "kernelspec": {
340 | "display_name": "Python 3 (ipykernel)",
341 | "language": "python",
342 | "name": "python3"
343 | },
344 | "language_info": {
345 | "codemirror_mode": {
346 | "name": "ipython",
347 | "version": 3
348 | },
349 | "file_extension": ".py",
350 | "mimetype": "text/x-python",
351 | "name": "python",
352 | "nbconvert_exporter": "python",
353 | "pygments_lexer": "ipython3",
354 | "version": "3.9.13"
355 | },
356 | "toc": {
357 | "base_numbering": 1,
358 | "nav_menu": {},
359 | "number_sections": true,
360 | "sideBar": true,
361 | "skip_h1_title": false,
362 | "title_cell": "Table of Contents",
363 | "title_sidebar": "Contents",
364 | "toc_cell": false,
365 | "toc_position": {},
366 | "toc_section_display": true,
367 | "toc_window_display": false
368 | },
369 | "varInspector": {
370 | "cols": {
371 | "lenName": 16,
372 | "lenType": 16,
373 | "lenVar": 40
374 | },
375 | "kernels_config": {
376 | "python": {
377 | "delete_cmd_postfix": "",
378 | "delete_cmd_prefix": "del ",
379 | "library": "var_list.py",
380 | "varRefreshCmd": "print(var_dic_list())"
381 | },
382 | "r": {
383 | "delete_cmd_postfix": ") ",
384 | "delete_cmd_prefix": "rm(",
385 | "library": "var_list.r",
386 | "varRefreshCmd": "cat(var_dic_list()) "
387 | }
388 | },
389 | "types_to_exclude": [
390 | "module",
391 | "function",
392 | "builtin_function_or_method",
393 | "instance",
394 | "_Feature"
395 | ],
396 | "window_display": false
397 | }
398 | },
399 | "nbformat": 4,
400 | "nbformat_minor": 2
401 | }
402 |
--------------------------------------------------------------------------------
/Day 24_Python_Flow Control and if_statement.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Flow-Control (Decision-Making) Statements\n",
8 | "\n"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | "# Control Flow Statements\n",
16 | "\n",
17 | "The flow control statements are divided into **three** categories:\n",
18 | "\n",
19 | "1. **Conditional statements**\n",
20 | "2. **Iterative statements**\n",
21 | "3. **Transfer/Control statements**\n",
22 | "\n",
23 | "\n",
24 | "

\n",
25 | "
"
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {},
31 | "source": [
32 | "# Python `if` statement\n",
33 | "\n"
34 | ]
35 | },
36 | {
37 | "cell_type": "markdown",
38 | "metadata": {},
39 | "source": [
40 | "### Syntax:\n",
41 | "\n",
42 | "```python\n",
43 | "if(condition):\n",
44 | " statement 1\n",
45 | " statement 2\n",
46 | " statement n\n",
47 | "```\n",
48 | "\n",
49 | "\n",
50 | "\n",
51 | "\n",
52 | "

\n",
53 | "
"
54 | ]
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {},
59 | "source": [
60 | "**Example: of `if` statement:**\n",
61 | "\n",
62 | "```python\n",
63 | ">>> grade = 70\n",
64 | ">>> if grade >= 65:\n",
65 | ">>> print(\"Passing grade\")\n",
66 | "\n",
67 | "Passing grade\n",
68 | "```\n"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": 1,
74 | "metadata": {
75 | "ExecuteTime": {
76 | "end_time": "2021-06-10T20:03:33.690723Z",
77 | "start_time": "2021-06-10T20:03:33.674172Z"
78 | }
79 | },
80 | "outputs": [
81 | {
82 | "name": "stdout",
83 | "output_type": "stream",
84 | "text": [
85 | "Passing grade\n"
86 | ]
87 | }
88 | ],
89 | "source": [
90 | "# Example 1:\n",
91 | "grade = 70\n",
92 | "if grade >= 65:\n",
93 | " print(\"Passing grade\")"
94 | ]
95 | },
96 | {
97 | "cell_type": "code",
98 | "execution_count": 2,
99 | "metadata": {
100 | "ExecuteTime": {
101 | "end_time": "2021-06-10T20:03:34.440566Z",
102 | "start_time": "2021-06-10T20:03:34.422066Z"
103 | }
104 | },
105 | "outputs": [
106 | {
107 | "name": "stdout",
108 | "output_type": "stream",
109 | "text": [
110 | "3 is a positive number.\n",
111 | "This is always printed.\n",
112 | "This is also always printed.\n"
113 | ]
114 | }
115 | ],
116 | "source": [
117 | "# Example 2: If the number is positive, we print an appropriate message\n",
118 | "\n",
119 | "num = 3\n",
120 | "if (num > 0): # if condition is TRUE: enter the body of if\n",
121 | " print(num, \"is a positive number.\")\n",
122 | "print(\"This is always printed.\")\n",
123 | "\n",
124 | "num = -1\n",
125 | "if num > 0: # if condition is FALSE: do not enter the body of if\n",
126 | " print(num, \"is a negative number.\")\n",
127 | "print(\"This is also always printed.\")"
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "execution_count": 3,
133 | "metadata": {
134 | "ExecuteTime": {
135 | "end_time": "2021-06-10T20:03:35.580430Z",
136 | "start_time": "2021-06-10T20:03:35.571663Z"
137 | }
138 | },
139 | "outputs": [
140 | {
141 | "name": "stdout",
142 | "output_type": "stream",
143 | "text": [
144 | "81\n",
145 | "Next lines of code\n"
146 | ]
147 | }
148 | ],
149 | "source": [
150 | "# Example 3: Calculate the square of a number if it greater than 6\n",
151 | "\n",
152 | "number = 9\n",
153 | "if number > 6:\n",
154 | " # Calculate square\n",
155 | " print(number * number)\n",
156 | "print('Next lines of code')"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": 4,
162 | "metadata": {
163 | "ExecuteTime": {
164 | "end_time": "2021-06-10T20:03:36.158393Z",
165 | "start_time": "2021-06-10T20:03:36.145731Z"
166 | }
167 | },
168 | "outputs": [
169 | {
170 | "name": "stdout",
171 | "output_type": "stream",
172 | "text": [
173 | "num1 is less than num2\n"
174 | ]
175 | }
176 | ],
177 | "source": [
178 | "# Example 4: Check if num1 is less than num2\n",
179 | "\n",
180 | "num1, num2 = 6, 9\n",
181 | "if(num1 < num2):\n",
182 | " print(\"num1 is less than num2\")"
183 | ]
184 | },
185 | {
186 | "cell_type": "code",
187 | "execution_count": 5,
188 | "metadata": {
189 | "ExecuteTime": {
190 | "end_time": "2021-06-10T20:03:36.548892Z",
191 | "start_time": "2021-06-10T20:03:36.529417Z"
192 | }
193 | },
194 | "outputs": [
195 | {
196 | "name": "stdout",
197 | "output_type": "stream",
198 | "text": [
199 | "Hello\n"
200 | ]
201 | }
202 | ],
203 | "source": [
204 | "x = 12\n",
205 | "if x > 10:\n",
206 | " print(\"Hello\")"
207 | ]
208 | },
209 | {
210 | "cell_type": "code",
211 | "execution_count": 6,
212 | "metadata": {
213 | "ExecuteTime": {
214 | "end_time": "2021-06-10T20:03:37.032393Z",
215 | "start_time": "2021-06-10T20:03:37.017791Z"
216 | }
217 | },
218 | "outputs": [
219 | {
220 | "name": "stdout",
221 | "output_type": "stream",
222 | "text": [
223 | "Hooray!\n"
224 | ]
225 | }
226 | ],
227 | "source": [
228 | "if 5 != 3 * 6:\n",
229 | " print (\"Hooray!\")"
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "execution_count": 7,
235 | "metadata": {
236 | "ExecuteTime": {
237 | "end_time": "2021-06-10T20:03:38.014494Z",
238 | "start_time": "2021-06-10T20:03:37.995993Z"
239 | }
240 | },
241 | "outputs": [
242 | {
243 | "name": "stdout",
244 | "output_type": "stream",
245 | "text": [
246 | "Horray!\n"
247 | ]
248 | }
249 | ],
250 | "source": [
251 | "if 5 == 15/3:\n",
252 | " print (\"Horray!\")"
253 | ]
254 | },
255 | {
256 | "cell_type": "markdown",
257 | "metadata": {},
258 | "source": [
259 | "### Shortcut for `if` statement (Short-hand `if` or one-line `if`)\n",
260 | "\n"
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "execution_count": 8,
266 | "metadata": {
267 | "ExecuteTime": {
268 | "end_time": "2021-06-10T20:03:38.795011Z",
269 | "start_time": "2021-06-10T20:03:38.778459Z"
270 | }
271 | },
272 | "outputs": [
273 | {
274 | "name": "stdout",
275 | "output_type": "stream",
276 | "text": [
277 | "num1 is less than num2\n"
278 | ]
279 | }
280 | ],
281 | "source": [
282 | "num1, num2 = 5, 6\n",
283 | "if(num1 < num2): print(\"num1 is less than num2\")"
284 | ]
285 | },
286 | {
287 | "cell_type": "markdown",
288 | "metadata": {},
289 | "source": [
290 | "### The Difference....\n",
291 | "\n"
292 | ]
293 | },
294 | {
295 | "cell_type": "code",
296 | "execution_count": 9,
297 | "metadata": {
298 | "ExecuteTime": {
299 | "end_time": "2021-06-10T20:03:44.293701Z",
300 | "start_time": "2021-06-10T20:03:44.275199Z"
301 | }
302 | },
303 | "outputs": [],
304 | "source": [
305 | "#program to check if num1 is less than num 2\n",
306 | "num1, num2 = 6, 5\n",
307 | "if (num1 < num2):\n",
308 | " print(\"num1 is less than num2\")"
309 | ]
310 | },
311 | {
312 | "cell_type": "code",
313 | "execution_count": 10,
314 | "metadata": {
315 | "ExecuteTime": {
316 | "end_time": "2021-06-10T20:03:48.495255Z",
317 | "start_time": "2021-06-10T20:03:48.478701Z"
318 | }
319 | },
320 | "outputs": [],
321 | "source": [
322 | "if 5 == 18/3:\n",
323 | " print (\"Hooray!\")"
324 | ]
325 | },
326 | {
327 | "cell_type": "code",
328 | "execution_count": null,
329 | "metadata": {},
330 | "outputs": [],
331 | "source": []
332 | }
333 | ],
334 | "metadata": {
335 | "hide_input": false,
336 | "kernelspec": {
337 | "display_name": "Python 3 (ipykernel)",
338 | "language": "python",
339 | "name": "python3"
340 | },
341 | "language_info": {
342 | "codemirror_mode": {
343 | "name": "ipython",
344 | "version": 3
345 | },
346 | "file_extension": ".py",
347 | "mimetype": "text/x-python",
348 | "name": "python",
349 | "nbconvert_exporter": "python",
350 | "pygments_lexer": "ipython3",
351 | "version": "3.9.13"
352 | },
353 | "toc": {
354 | "base_numbering": 1,
355 | "nav_menu": {},
356 | "number_sections": true,
357 | "sideBar": true,
358 | "skip_h1_title": false,
359 | "title_cell": "Table of Contents",
360 | "title_sidebar": "Contents",
361 | "toc_cell": false,
362 | "toc_position": {},
363 | "toc_section_display": true,
364 | "toc_window_display": false
365 | },
366 | "varInspector": {
367 | "cols": {
368 | "lenName": 16,
369 | "lenType": 16,
370 | "lenVar": 40
371 | },
372 | "kernels_config": {
373 | "python": {
374 | "delete_cmd_postfix": "",
375 | "delete_cmd_prefix": "del ",
376 | "library": "var_list.py",
377 | "varRefreshCmd": "print(var_dic_list())"
378 | },
379 | "r": {
380 | "delete_cmd_postfix": ") ",
381 | "delete_cmd_prefix": "rm(",
382 | "library": "var_list.r",
383 | "varRefreshCmd": "cat(var_dic_list()) "
384 | }
385 | },
386 | "types_to_exclude": [
387 | "module",
388 | "function",
389 | "builtin_function_or_method",
390 | "instance",
391 | "_Feature"
392 | ],
393 | "window_display": false
394 | }
395 | },
396 | "nbformat": 4,
397 | "nbformat_minor": 2
398 | }
399 |
--------------------------------------------------------------------------------
/Day 25_Python_if_else_statement.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python `if-else` statement\n",
8 | "\n"
9 | ]
10 | },
11 | {
12 | "cell_type": "code",
13 | "execution_count": 1,
14 | "metadata": {
15 | "ExecuteTime": {
16 | "end_time": "2021-10-04T07:16:58.412652Z",
17 | "start_time": "2021-10-04T07:16:58.381404Z"
18 | },
19 | "scrolled": true
20 | },
21 | "outputs": [
22 | {
23 | "name": "stdout",
24 | "output_type": "stream",
25 | "text": [
26 | "Case 2\n"
27 | ]
28 | }
29 | ],
30 | "source": [
31 | "x = 1\n",
32 | "\n",
33 | "if x > 3:\n",
34 | " print (\"Case 1\")\n",
35 | "if x <= 3:\n",
36 | " print (\"Case 2\")"
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {},
42 | "source": [
43 | "### Syntax :\n",
44 | "\n",
45 | "```python\n",
46 | "if condition:\n",
47 | " statement 1\n",
48 | "else:\n",
49 | " statement 2\n",
50 | "```\n",
51 | "\n",
52 | "\n",
53 | "

\n",
54 | "
\n",
55 | "\n"
56 | ]
57 | },
58 | {
59 | "cell_type": "code",
60 | "execution_count": 2,
61 | "metadata": {
62 | "ExecuteTime": {
63 | "end_time": "2021-10-04T07:16:58.535698Z",
64 | "start_time": "2021-10-04T07:16:58.415580Z"
65 | }
66 | },
67 | "outputs": [
68 | {
69 | "name": "stdout",
70 | "output_type": "stream",
71 | "text": [
72 | "Failing grade\n"
73 | ]
74 | }
75 | ],
76 | "source": [
77 | "# Example 1:\n",
78 | "\n",
79 | "grade = 60\n",
80 | " \n",
81 | "if grade >= 65:\n",
82 | " print(\"Passing grade\")\n",
83 | "else:\n",
84 | " print(\"Failing grade\")"
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": 3,
90 | "metadata": {
91 | "ExecuteTime": {
92 | "end_time": "2021-10-04T07:16:58.644099Z",
93 | "start_time": "2021-10-04T07:16:58.540581Z"
94 | }
95 | },
96 | "outputs": [
97 | {
98 | "name": "stdout",
99 | "output_type": "stream",
100 | "text": [
101 | "Positive or Zero\n"
102 | ]
103 | }
104 | ],
105 | "source": [
106 | "# Example 2: Program checks if the number is positive or negative and displays an appropriate message\n",
107 | "\n",
108 | "num = 3\n",
109 | "\n",
110 | "# Try these two variations as well. \n",
111 | "# num = -5\n",
112 | "# num = 0\n",
113 | "\n",
114 | "if num >= 0:\n",
115 | " print(\"Positive or Zero\")\n",
116 | "else:\n",
117 | " print(\"Negative number\")"
118 | ]
119 | },
120 | {
121 | "cell_type": "code",
122 | "execution_count": 4,
123 | "metadata": {
124 | "ExecuteTime": {
125 | "end_time": "2021-10-04T07:16:58.659725Z",
126 | "start_time": "2021-10-04T07:16:58.648003Z"
127 | }
128 | },
129 | "outputs": [
130 | {
131 | "name": "stdout",
132 | "output_type": "stream",
133 | "text": [
134 | "num2 is less than num1\n"
135 | ]
136 | }
137 | ],
138 | "source": [
139 | "# Example 3: program to check if a num1 is less than num2\n",
140 | "\n",
141 | "num1, num2 = 6, 5\n",
142 | "if (num1 < num2):\n",
143 | " print(\"num1 is less than num2\")\n",
144 | "else:\n",
145 | " print(\"num2 is less than num1\")"
146 | ]
147 | },
148 | {
149 | "cell_type": "code",
150 | "execution_count": null,
151 | "metadata": {
152 | "ExecuteTime": {
153 | "end_time": "2021-10-04T07:16:58.798390Z",
154 | "start_time": "2021-10-04T07:16:58.662651Z"
155 | }
156 | },
157 | "outputs": [],
158 | "source": [
159 | "# Example 4:\n",
160 | "\n",
161 | "def password_check(password):\n",
162 | " if password == \"Python@99>\":\n",
163 | " print(\"Correct password\")\n",
164 | " else:\n",
165 | " print(\"Incorrect Password\")\n",
166 | "\n",
167 | "password_check(\"Python@99>\")\n",
168 | "# Output Correct password\n",
169 | "\n",
170 | "password_check(\"Python99\")\n",
171 | "# Output Incorrect Password"
172 | ]
173 | },
174 | {
175 | "cell_type": "markdown",
176 | "metadata": {},
177 | "source": [
178 | "### Shortcut of `if else` (Short Hand if ... else or One line `if else`)\n",
179 | "\n"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": 5,
185 | "metadata": {
186 | "ExecuteTime": {
187 | "end_time": "2021-10-04T07:16:58.920462Z",
188 | "start_time": "2021-10-04T07:16:58.806208Z"
189 | }
190 | },
191 | "outputs": [
192 | {
193 | "name": "stdout",
194 | "output_type": "stream",
195 | "text": [
196 | "Feed the bear now!\n"
197 | ]
198 | }
199 | ],
200 | "source": [
201 | "hungry = True\n",
202 | "x = 'Feed the bear now!' if hungry else 'Do not feed the bear.'\n",
203 | "print(x)"
204 | ]
205 | },
206 | {
207 | "cell_type": "code",
208 | "execution_count": 6,
209 | "metadata": {
210 | "ExecuteTime": {
211 | "end_time": "2021-10-04T07:16:59.059135Z",
212 | "start_time": "2021-10-04T07:16:58.931207Z"
213 | }
214 | },
215 | "outputs": [
216 | {
217 | "name": "stdout",
218 | "output_type": "stream",
219 | "text": [
220 | "A is positive\n"
221 | ]
222 | }
223 | ],
224 | "source": [
225 | "a = 3\n",
226 | "print('A is positive') if a > 0 else print('A is negative') # first condition met, 'A is positive' will be printed"
227 | ]
228 | },
229 | {
230 | "cell_type": "code",
231 | "execution_count": 7,
232 | "metadata": {
233 | "ExecuteTime": {
234 | "end_time": "2021-10-04T07:16:59.183158Z",
235 | "start_time": "2021-10-04T07:16:59.070854Z"
236 | }
237 | },
238 | "outputs": [
239 | {
240 | "name": "stdout",
241 | "output_type": "stream",
242 | "text": [
243 | "num2 is less than num1\n"
244 | ]
245 | }
246 | ],
247 | "source": [
248 | "num1, num2 = 6, 5\n",
249 | "print(\"num1 is less than num2\") if (num1 < num2) else print(\"num2 is less than num1\")"
250 | ]
251 | },
252 | {
253 | "cell_type": "code",
254 | "execution_count": 8,
255 | "metadata": {
256 | "ExecuteTime": {
257 | "end_time": "2021-10-04T07:16:59.325248Z",
258 | "start_time": "2021-10-04T07:16:59.189017Z"
259 | }
260 | },
261 | "outputs": [
262 | {
263 | "name": "stdout",
264 | "output_type": "stream",
265 | "text": [
266 | "positive\n"
267 | ]
268 | }
269 | ],
270 | "source": [
271 | "number = 96\n",
272 | "if number > 0: print(\"positive\") \n",
273 | "else: print(\"negative\")"
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": 9,
279 | "metadata": {
280 | "ExecuteTime": {
281 | "end_time": "2021-10-04T07:16:59.429251Z",
282 | "start_time": "2021-10-04T07:16:59.328177Z"
283 | }
284 | },
285 | "outputs": [
286 | {
287 | "name": "stdout",
288 | "output_type": "stream",
289 | "text": [
290 | "world\n"
291 | ]
292 | }
293 | ],
294 | "source": [
295 | "x = 12\n",
296 | "if 10 < x < 11:\n",
297 | " print(\"hello\")\n",
298 | "else:\n",
299 | " print(\"world\")"
300 | ]
301 | },
302 | {
303 | "cell_type": "code",
304 | "execution_count": 10,
305 | "metadata": {
306 | "ExecuteTime": {
307 | "end_time": "2021-10-04T07:16:59.538139Z",
308 | "start_time": "2021-10-04T07:16:59.437065Z"
309 | }
310 | },
311 | "outputs": [
312 | {
313 | "name": "stdout",
314 | "output_type": "stream",
315 | "text": [
316 | "Case 2\n"
317 | ]
318 | }
319 | ],
320 | "source": [
321 | "'''\n",
322 | "x = 1\n",
323 | "\n",
324 | "if x > 3:\n",
325 | " print (\"Case 1\")\n",
326 | "if x <= 3:\n",
327 | " print (\"Case 2\")\n",
328 | "'''\n",
329 | "\n",
330 | "x = 1\n",
331 | "\n",
332 | "if x > 3:\n",
333 | " print (\"Case 1\")\n",
334 | "else:\n",
335 | " print (\"Case 2\")"
336 | ]
337 | },
338 | {
339 | "cell_type": "code",
340 | "execution_count": 11,
341 | "metadata": {
342 | "ExecuteTime": {
343 | "end_time": "2021-10-04T07:16:59.660693Z",
344 | "start_time": "2021-10-04T07:16:59.542045Z"
345 | }
346 | },
347 | "outputs": [
348 | {
349 | "name": "stdout",
350 | "output_type": "stream",
351 | "text": [
352 | "Incorrect\n"
353 | ]
354 | }
355 | ],
356 | "source": [
357 | "x = 2\n",
358 | "\n",
359 | "if x > 4:\n",
360 | " print (\"Correct\")\n",
361 | "else:\n",
362 | " print(\"Incorrect\")"
363 | ]
364 | },
365 | {
366 | "cell_type": "markdown",
367 | "metadata": {},
368 | "source": [
369 | "### The difference....\n",
370 | "\n"
371 | ]
372 | },
373 | {
374 | "cell_type": "code",
375 | "execution_count": 12,
376 | "metadata": {
377 | "ExecuteTime": {
378 | "end_time": "2021-10-04T07:16:59.814505Z",
379 | "start_time": "2021-10-04T07:16:59.664116Z"
380 | }
381 | },
382 | "outputs": [
383 | {
384 | "name": "stdout",
385 | "output_type": "stream",
386 | "text": [
387 | "num2 is less than num1\n"
388 | ]
389 | },
390 | {
391 | "data": {
392 | "text/plain": [
393 | "'\\nOutput: num2 is lesser than num1\\n'"
394 | ]
395 | },
396 | "execution_count": 12,
397 | "metadata": {},
398 | "output_type": "execute_result"
399 | }
400 | ],
401 | "source": [
402 | "#program to check if num1 is less than num2\n",
403 | "\n",
404 | "num1, num2 = 5, 5\n",
405 | "if (num1 < num2):\n",
406 | " print(\"num1 is less than num2\")\n",
407 | "else:\n",
408 | " print(\"num2 is less than num1\")\n",
409 | "\n",
410 | "'''\n",
411 | "Output: num2 is lesser than num1\n",
412 | "'''"
413 | ]
414 | },
415 | {
416 | "cell_type": "markdown",
417 | "metadata": {},
418 | "source": [
419 | "## 💻 Exercises ➞ List\n",
420 | "\n",
421 | "1. Get user input using **`input(“Enter your age: ”)`**. If user is 18 or older, give feedback: **`You are old enough to drive`**. If below 18 give feedback to **`wait for the missing amount of years`**. \n",
422 | "\n",
423 | "Output:\n",
424 | "\n",
425 | "```py\n",
426 | " Enter your age: 30\n",
427 | " You are old enough to learn to drive.\n",
428 | " Output:\n",
429 | " Enter your age: 15\n",
430 | " You need 3 more years to learn to drive.\n",
431 | "```\n",
432 | "\n",
433 | "2. Compare the values of my_age and your_age using **`if-else`**. Who is older (me or you)? Use **`input(“Enter your age: ”)`** to get the age as input. You can use a nested condition to print **`'year'`** for 1 year difference in age, **`'years'`** for bigger differences, and a custom text if **`my_age = your_age`**. \n",
434 | "\n",
435 | "Output:\n",
436 | "\n",
437 | "```py\n",
438 | " Enter your age: 30\n",
439 | " You are 5 years older than me.\n",
440 | "```"
441 | ]
442 | },
443 | {
444 | "cell_type": "code",
445 | "execution_count": null,
446 | "metadata": {},
447 | "outputs": [],
448 | "source": []
449 | }
450 | ],
451 | "metadata": {
452 | "hide_input": false,
453 | "kernelspec": {
454 | "display_name": "Python 3 (ipykernel)",
455 | "language": "python",
456 | "name": "python3"
457 | },
458 | "language_info": {
459 | "codemirror_mode": {
460 | "name": "ipython",
461 | "version": 3
462 | },
463 | "file_extension": ".py",
464 | "mimetype": "text/x-python",
465 | "name": "python",
466 | "nbconvert_exporter": "python",
467 | "pygments_lexer": "ipython3",
468 | "version": "3.9.13"
469 | },
470 | "toc": {
471 | "base_numbering": 1,
472 | "nav_menu": {},
473 | "number_sections": true,
474 | "sideBar": true,
475 | "skip_h1_title": false,
476 | "title_cell": "Table of Contents",
477 | "title_sidebar": "Contents",
478 | "toc_cell": false,
479 | "toc_position": {},
480 | "toc_section_display": true,
481 | "toc_window_display": false
482 | },
483 | "varInspector": {
484 | "cols": {
485 | "lenName": 16,
486 | "lenType": 16,
487 | "lenVar": 40
488 | },
489 | "kernels_config": {
490 | "python": {
491 | "delete_cmd_postfix": "",
492 | "delete_cmd_prefix": "del ",
493 | "library": "var_list.py",
494 | "varRefreshCmd": "print(var_dic_list())"
495 | },
496 | "r": {
497 | "delete_cmd_postfix": ") ",
498 | "delete_cmd_prefix": "rm(",
499 | "library": "var_list.r",
500 | "varRefreshCmd": "cat(var_dic_list()) "
501 | }
502 | },
503 | "types_to_exclude": [
504 | "module",
505 | "function",
506 | "builtin_function_or_method",
507 | "instance",
508 | "_Feature"
509 | ],
510 | "window_display": false
511 | }
512 | },
513 | "nbformat": 4,
514 | "nbformat_minor": 2
515 | }
516 |
--------------------------------------------------------------------------------
/Day 35.1_Python_Function_global_Keywords.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python `global` Keyword\n",
8 | "\n",
9 | "In this Session, you’ll learn about the **`global`** keyword, global variable and when to use **`global`** keyword.\n"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "# What is `global` keyword?\n"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "## Use of `global` Keyword"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": 1,
29 | "metadata": {
30 | "ExecuteTime": {
31 | "end_time": "2021-06-13T14:27:02.042929Z",
32 | "start_time": "2021-06-13T14:27:02.025355Z"
33 | }
34 | },
35 | "outputs": [
36 | {
37 | "name": "stdout",
38 | "output_type": "stream",
39 | "text": [
40 | "1\n"
41 | ]
42 | }
43 | ],
44 | "source": [
45 | "# Example 1: Accessing global Variable From Inside a Function\n",
46 | "\n",
47 | "a = 1 # global variable\n",
48 | "\n",
49 | "def add():\n",
50 | " print(a)\n",
51 | "\n",
52 | "add()"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 2,
58 | "metadata": {
59 | "ExecuteTime": {
60 | "end_time": "2021-06-13T14:27:05.239195Z",
61 | "start_time": "2021-06-13T14:27:04.904238Z"
62 | }
63 | },
64 | "outputs": [
65 | {
66 | "ename": "UnboundLocalError",
67 | "evalue": "local variable 'a' referenced before assignment",
68 | "output_type": "error",
69 | "traceback": [
70 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
71 | "\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)",
72 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_21172/3341413406.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
73 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_21172/3341413406.py\u001b[0m in \u001b[0;36madd\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m3\u001b[0m \u001b[1;31m# increment a by 3 (modifying my global variable)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
74 | "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'a' referenced before assignment"
75 | ]
76 | }
77 | ],
78 | "source": [
79 | "# Example 2: Modifying Global Variable From Inside the Function\n",
80 | "\n",
81 | "a = 1 # global variable\n",
82 | " \n",
83 | "def add():\n",
84 | " a = a + 3 # increment a by 3 (modifying my global variable)\n",
85 | " print(a)\n",
86 | "\n",
87 | "add()"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": 3,
93 | "metadata": {
94 | "ExecuteTime": {
95 | "end_time": "2021-06-13T14:27:06.885175Z",
96 | "start_time": "2021-06-13T14:27:06.863695Z"
97 | }
98 | },
99 | "outputs": [
100 | {
101 | "name": "stdout",
102 | "output_type": "stream",
103 | "text": [
104 | "Inside add(): 3\n",
105 | "In main: 3\n"
106 | ]
107 | }
108 | ],
109 | "source": [
110 | "# Example 2: Changing Global Variable From Inside a Function using global\n",
111 | "\n",
112 | "a = 0 # global variable\n",
113 | "\n",
114 | "def add():\n",
115 | " global a # using global variable inside my function\n",
116 | " a = a + 3 # increment by 3\n",
117 | " print(\"Inside add():\", a)\n",
118 | "\n",
119 | "add()\n",
120 | "print(\"In main:\", a)"
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": 4,
126 | "metadata": {
127 | "ExecuteTime": {
128 | "end_time": "2021-06-13T14:27:09.262597Z",
129 | "start_time": "2021-06-13T14:27:09.239165Z"
130 | }
131 | },
132 | "outputs": [
133 | {
134 | "name": "stdout",
135 | "output_type": "stream",
136 | "text": [
137 | "Value in 1st function: 9\n",
138 | "Value in 2nd function: 999\n",
139 | "Value in 3rd function: 9\n"
140 | ]
141 | }
142 | ],
143 | "source": [
144 | "# Exercise 3: without `global` keyword.\n",
145 | "\n",
146 | "global_var = 9 # Global variable\n",
147 | "\n",
148 | "def fun1():\n",
149 | " print(\"Value in 1st function:\", global_var)\n",
150 | "\n",
151 | "def fun2():\n",
152 | " # Modify global variable\n",
153 | " # function will treat it as a local variable\n",
154 | " global_var = 999\n",
155 | " print(\"Value in 2nd function:\", global_var)\n",
156 | "\n",
157 | "def fun3():\n",
158 | " print(\"Value in 3rd function:\", global_var)\n",
159 | "\n",
160 | "fun1()\n",
161 | "fun2()\n",
162 | "fun3()"
163 | ]
164 | },
165 | {
166 | "cell_type": "code",
167 | "execution_count": 5,
168 | "metadata": {
169 | "ExecuteTime": {
170 | "end_time": "2021-06-13T14:27:11.122937Z",
171 | "start_time": "2021-06-13T14:27:11.113174Z"
172 | }
173 | },
174 | "outputs": [
175 | {
176 | "name": "stdout",
177 | "output_type": "stream",
178 | "text": [
179 | "Value in 1st function: 9\n",
180 | "Value in 2nd function: 999\n",
181 | "Value in 3rd function: 999\n"
182 | ]
183 | }
184 | ],
185 | "source": [
186 | "# Exercise 3: use the `global` keyword.\n",
187 | "\n",
188 | "\n",
189 | "x = 9 # Global variable\n",
190 | "\n",
191 | "# defining 1st function\n",
192 | "def fun1():\n",
193 | " print(\"Value in 1st function:\", x)\n",
194 | "\n",
195 | "# defining 2nd function\n",
196 | "def fun2():\n",
197 | " # Modify global variable using global keyword\n",
198 | " global x\n",
199 | " x = 999\n",
200 | " print(\"Value in 2nd function:\", x)\n",
201 | "\n",
202 | "# defining 3rd function\n",
203 | "def fun3():\n",
204 | " print(\"Value in 3rd function:\", x)\n",
205 | "\n",
206 | "fun1()\n",
207 | "fun2()\n",
208 | "fun3()"
209 | ]
210 | },
211 | {
212 | "cell_type": "markdown",
213 | "metadata": {},
214 | "source": [
215 | "## Global Variables Across Python Modules\n",
216 | "\n"
217 | ]
218 | },
219 | {
220 | "cell_type": "markdown",
221 | "metadata": {},
222 | "source": [
223 | "### Example : Share a global Variable Across Python Modules\n",
224 | "\n",
225 | "Create a **config.py** file, to store global variables\n",
226 | "\n",
227 | "```python\n",
228 | ">>> a = 0\n",
229 | ">>> b = \"empty\"\n",
230 | "```\n",
231 | "\n",
232 | "Create a **update.py** file, to change global variables\n",
233 | "\n",
234 | "```python\n",
235 | ">>> import config # import config.py here\n",
236 | "\n",
237 | ">>> config.a = 10 # change the value of 'a' from 0 to 10\n",
238 | ">>> config.b = \"alphabet\" # change the value of 'b' from \"empty\" to \"alphabet\"\n",
239 | "```\n",
240 | "\n",
241 | "Create a **main1.py** file, to test changes in value\n",
242 | "\n",
243 | "```python\n",
244 | ">>> import config # import config.py here\n",
245 | ">>> import update # import update.py here\n",
246 | "\n",
247 | ">>> print(config.a) # print the updated value of 'a'\n",
248 | ">>> print(config.b) # print the updated value of 'b'\n",
249 | "```\n",
250 | "\n",
251 | "When we run the **main.py** file, the output will be\n",
252 | "\n",
253 | "`\n",
254 | "10\n",
255 | "alphabet\n",
256 | "`\n",
257 | "\n"
258 | ]
259 | },
260 | {
261 | "cell_type": "markdown",
262 | "metadata": {},
263 | "source": [
264 | "## Global in Nested Functions\n",
265 | "\n"
266 | ]
267 | },
268 | {
269 | "cell_type": "code",
270 | "execution_count": 6,
271 | "metadata": {
272 | "ExecuteTime": {
273 | "end_time": "2021-06-13T14:27:16.594081Z",
274 | "start_time": "2021-06-13T14:27:16.570649Z"
275 | }
276 | },
277 | "outputs": [
278 | {
279 | "name": "stdout",
280 | "output_type": "stream",
281 | "text": [
282 | "Before calling bar function: 30\n",
283 | "Calling bar function now\n",
284 | "After calling bar function: 30\n",
285 | "x in main function: 66\n"
286 | ]
287 | }
288 | ],
289 | "source": [
290 | "# Example 1: Using a Global Variable in Nested Function\n",
291 | "\n",
292 | "def fun(): # main function\n",
293 | " x = 30\n",
294 | "\n",
295 | " def day(): # nested fucntion\n",
296 | " global x\n",
297 | " x = 66\n",
298 | " \n",
299 | " print(\"Before calling bar function:\", x) # check indentation\n",
300 | " print(\"Calling bar function now\")\n",
301 | " day()\n",
302 | " print(\"After calling bar function:\", x)\n",
303 | "\n",
304 | "fun()\n",
305 | "print(\"x in main function:\", x)"
306 | ]
307 | },
308 | {
309 | "cell_type": "code",
310 | "execution_count": null,
311 | "metadata": {},
312 | "outputs": [],
313 | "source": []
314 | }
315 | ],
316 | "metadata": {
317 | "hide_input": false,
318 | "kernelspec": {
319 | "display_name": "Python 3 (ipykernel)",
320 | "language": "python",
321 | "name": "python3"
322 | },
323 | "language_info": {
324 | "codemirror_mode": {
325 | "name": "ipython",
326 | "version": 3
327 | },
328 | "file_extension": ".py",
329 | "mimetype": "text/x-python",
330 | "name": "python",
331 | "nbconvert_exporter": "python",
332 | "pygments_lexer": "ipython3",
333 | "version": "3.9.13"
334 | },
335 | "toc": {
336 | "base_numbering": 1,
337 | "nav_menu": {},
338 | "number_sections": true,
339 | "sideBar": true,
340 | "skip_h1_title": false,
341 | "title_cell": "Table of Contents",
342 | "title_sidebar": "Contents",
343 | "toc_cell": false,
344 | "toc_position": {},
345 | "toc_section_display": true,
346 | "toc_window_display": false
347 | },
348 | "varInspector": {
349 | "cols": {
350 | "lenName": 16,
351 | "lenType": 16,
352 | "lenVar": 40
353 | },
354 | "kernels_config": {
355 | "python": {
356 | "delete_cmd_postfix": "",
357 | "delete_cmd_prefix": "del ",
358 | "library": "var_list.py",
359 | "varRefreshCmd": "print(var_dic_list())"
360 | },
361 | "r": {
362 | "delete_cmd_postfix": ") ",
363 | "delete_cmd_prefix": "rm(",
364 | "library": "var_list.r",
365 | "varRefreshCmd": "cat(var_dic_list()) "
366 | }
367 | },
368 | "types_to_exclude": [
369 | "module",
370 | "function",
371 | "builtin_function_or_method",
372 | "instance",
373 | "_Feature"
374 | ],
375 | "window_display": false
376 | }
377 | },
378 | "nbformat": 4,
379 | "nbformat_minor": 2
380 | }
381 |
--------------------------------------------------------------------------------
/Day 35.2_Python_User_defined_Functions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python User-defined Functions\n",
8 | "\n",
9 | "In this Session, you will find the advantages of using user-defined functions and best practices to follow."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## What are user-defined functions in Python?\n",
17 | "\n"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "## Advantages of user-defined functions\n"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": 1,
30 | "metadata": {
31 | "ExecuteTime": {
32 | "end_time": "2021-06-25T05:28:47.996956Z",
33 | "start_time": "2021-06-25T05:28:39.880301Z"
34 | }
35 | },
36 | "outputs": [
37 | {
38 | "name": "stdout",
39 | "output_type": "stream",
40 | "text": [
41 | "Enter first number: 9.3\n",
42 | "Enter second number: 6.9\n",
43 | "The sum is 16.200000000000003\n"
44 | ]
45 | }
46 | ],
47 | "source": [
48 | "# Example 1: illustrate the use of user-defined functions\n",
49 | "\n",
50 | "num1 = input('Enter first number: ')\n",
51 | "num2 = input('Enter second number: ')\n",
52 | "\n",
53 | "def my_addition(num1,num2):\n",
54 | " sum = float(num1) + float(num2)\n",
55 | " return sum\n",
56 | "\n",
57 | "#num1 = 9.3\n",
58 | "#num2 = 6.9\n",
59 | "\n",
60 | "print(\"The sum is\", my_addition(num1, num2))"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": null,
66 | "metadata": {},
67 | "outputs": [],
68 | "source": []
69 | }
70 | ],
71 | "metadata": {
72 | "hide_input": false,
73 | "kernelspec": {
74 | "display_name": "Python 3 (ipykernel)",
75 | "language": "python",
76 | "name": "python3"
77 | },
78 | "language_info": {
79 | "codemirror_mode": {
80 | "name": "ipython",
81 | "version": 3
82 | },
83 | "file_extension": ".py",
84 | "mimetype": "text/x-python",
85 | "name": "python",
86 | "nbconvert_exporter": "python",
87 | "pygments_lexer": "ipython3",
88 | "version": "3.9.13"
89 | },
90 | "toc": {
91 | "base_numbering": 1,
92 | "nav_menu": {},
93 | "number_sections": true,
94 | "sideBar": true,
95 | "skip_h1_title": false,
96 | "title_cell": "Table of Contents",
97 | "title_sidebar": "Contents",
98 | "toc_cell": false,
99 | "toc_position": {},
100 | "toc_section_display": true,
101 | "toc_window_display": false
102 | },
103 | "varInspector": {
104 | "cols": {
105 | "lenName": 16,
106 | "lenType": 16,
107 | "lenVar": 40
108 | },
109 | "kernels_config": {
110 | "python": {
111 | "delete_cmd_postfix": "",
112 | "delete_cmd_prefix": "del ",
113 | "library": "var_list.py",
114 | "varRefreshCmd": "print(var_dic_list())"
115 | },
116 | "r": {
117 | "delete_cmd_postfix": ") ",
118 | "delete_cmd_prefix": "rm(",
119 | "library": "var_list.r",
120 | "varRefreshCmd": "cat(var_dic_list()) "
121 | }
122 | },
123 | "types_to_exclude": [
124 | "module",
125 | "function",
126 | "builtin_function_or_method",
127 | "instance",
128 | "_Feature"
129 | ],
130 | "window_display": false
131 | }
132 | },
133 | "nbformat": 4,
134 | "nbformat_minor": 2
135 | }
136 |
--------------------------------------------------------------------------------
/Day 37_Python_Function_Recursion.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Recursion\n",
8 | "\n",
9 | "In this session, you will learn to create a recursive function (a function that calls itself again and again)."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "# What is recursion?\n"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "## Python Recursive Function\n",
24 | "\n",
25 | "\n",
26 | "The following image shows the working of a recursive function called **`recurse`**.\n",
27 | "\n",
28 | "\n",
29 | "

\n",
30 | "
\n",
31 | "\n",
32 | "\n",
33 | " For example, the factorial of 6 (denoted as 6!) is \n",
34 | "\n",
35 | "```python\n",
36 | "1*2*3*4*5*6 = 720.\n",
37 | "```"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": null,
43 | "metadata": {
44 | "ExecuteTime": {
45 | "end_time": "2021-06-13T16:03:47.995819Z",
46 | "start_time": "2021-06-13T16:03:47.984100Z"
47 | }
48 | },
49 | "outputs": [],
50 | "source": [
51 | "# Example 1:\n",
52 | "\n",
53 | "def factorial(n):\n",
54 | " \"\"\"This is a recursive function to find the factorial of an integer\"\"\"\n",
55 | "\n",
56 | " if n == 1:\n",
57 | " return 1\n",
58 | " else:\n",
59 | " return (n * factorial(n-1)) # 3 * 2 * 1 = 6\n",
60 | "\n",
61 | "\n",
62 | "num = 3\n",
63 | "print(\"The factorial of\", num, \"is\", factorial(num))"
64 | ]
65 | },
66 | {
67 | "cell_type": "markdown",
68 | "metadata": {},
69 | "source": [
70 | "\n",
71 | "\n",
72 | "```python\n",
73 | "factorial(3) # 1st call with 3\n",
74 | "3 * factorial(2) # 2nd call with 2\n",
75 | "3 * 2 * factorial(1) # 3rd call with 1\n",
76 | "3 * 2 * 1 # return from 3rd call as number=1\n",
77 | "3 * 2 # return from 2nd call\n",
78 | "6 # return from 1st call\n",
79 | "```\n",
80 | "\n",
81 | "\n",
82 | "\n",
83 | "\n",
84 | "

\n",
85 | "
"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": null,
91 | "metadata": {
92 | "ExecuteTime": {
93 | "end_time": "2021-06-13T16:04:11.723151Z",
94 | "start_time": "2021-06-13T16:04:05.119692Z"
95 | }
96 | },
97 | "outputs": [],
98 | "source": [
99 | "def recursor():\n",
100 | " recursor()\n",
101 | "recursor()"
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "execution_count": null,
107 | "metadata": {},
108 | "outputs": [],
109 | "source": []
110 | }
111 | ],
112 | "metadata": {
113 | "hide_input": false,
114 | "kernelspec": {
115 | "display_name": "Python 3 (ipykernel)",
116 | "language": "python",
117 | "name": "python3"
118 | },
119 | "language_info": {
120 | "codemirror_mode": {
121 | "name": "ipython",
122 | "version": 3
123 | },
124 | "file_extension": ".py",
125 | "mimetype": "text/x-python",
126 | "name": "python",
127 | "nbconvert_exporter": "python",
128 | "pygments_lexer": "ipython3",
129 | "version": "3.9.13"
130 | },
131 | "toc": {
132 | "base_numbering": 1,
133 | "nav_menu": {},
134 | "number_sections": true,
135 | "sideBar": true,
136 | "skip_h1_title": false,
137 | "title_cell": "Table of Contents",
138 | "title_sidebar": "Contents",
139 | "toc_cell": false,
140 | "toc_position": {},
141 | "toc_section_display": true,
142 | "toc_window_display": false
143 | },
144 | "varInspector": {
145 | "cols": {
146 | "lenName": 16,
147 | "lenType": 16,
148 | "lenVar": 40
149 | },
150 | "kernels_config": {
151 | "python": {
152 | "delete_cmd_postfix": "",
153 | "delete_cmd_prefix": "del ",
154 | "library": "var_list.py",
155 | "varRefreshCmd": "print(var_dic_list())"
156 | },
157 | "r": {
158 | "delete_cmd_postfix": ") ",
159 | "delete_cmd_prefix": "rm(",
160 | "library": "var_list.r",
161 | "varRefreshCmd": "cat(var_dic_list()) "
162 | }
163 | },
164 | "types_to_exclude": [
165 | "module",
166 | "function",
167 | "builtin_function_or_method",
168 | "instance",
169 | "_Feature"
170 | ],
171 | "window_display": false
172 | }
173 | },
174 | "nbformat": 4,
175 | "nbformat_minor": 2
176 | }
177 |
--------------------------------------------------------------------------------
/Day 38_Python_Function_Anonymous.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Anonymous/**`lambda`** Function\n",
8 | "\n",
9 | "In this session, you'll learn about the anonymous function, also known as **`lambda`** functions."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## What are **`lambda`** functions in Python?\n",
17 | "\n",
18 | "\n",
19 | "\n",
20 | "**Syntax:** \n",
21 | "\n",
22 | "```python\n",
23 | "lambda argument_list: expression\n",
24 | "```"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": 1,
30 | "metadata": {
31 | "ExecuteTime": {
32 | "end_time": "2021-06-13T16:39:39.033426Z",
33 | "start_time": "2021-06-13T16:39:39.014872Z"
34 | }
35 | },
36 | "outputs": [
37 | {
38 | "name": "stdout",
39 | "output_type": "stream",
40 | "text": [
41 | "Even numbers are: [10, 16, 78, 2]\n"
42 | ]
43 | }
44 | ],
45 | "source": [
46 | "# Example 1: Program for even numbers without lambda function\n",
47 | "\n",
48 | "def even_numbers(nums):\n",
49 | " even_list = []\n",
50 | " for n in nums:\n",
51 | " if n % 2 == 0:\n",
52 | " even_list.append(n)\n",
53 | " return even_list\n",
54 | "\n",
55 | "num_list = [10, 9, 16, 78, 2, 3, 7, 1]\n",
56 | "ans = even_numbers(num_list)\n",
57 | "print(\"Even numbers are:\", ans)"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 2,
63 | "metadata": {
64 | "ExecuteTime": {
65 | "end_time": "2021-06-13T16:39:39.494358Z",
66 | "start_time": "2021-06-13T16:39:39.474831Z"
67 | }
68 | },
69 | "outputs": [
70 | {
71 | "name": "stdout",
72 | "output_type": "stream",
73 | "text": [
74 | "Even numbers are: [10, 16, 78, 2]\n"
75 | ]
76 | }
77 | ],
78 | "source": [
79 | "# Example 1: Program for even number with a lambda function\n",
80 | "\n",
81 | "l = [10, 9, 16, 78, 2, 3, 7, 1]\n",
82 | "even_nos = list(filter(lambda x: x % 2 == 0, l))\n",
83 | "print(\"Even numbers are: \", even_nos)"
84 | ]
85 | },
86 | {
87 | "cell_type": "code",
88 | "execution_count": 3,
89 | "metadata": {
90 | "ExecuteTime": {
91 | "end_time": "2021-06-13T16:39:39.879120Z",
92 | "start_time": "2021-06-13T16:39:39.860569Z"
93 | }
94 | },
95 | "outputs": [
96 | {
97 | "name": "stdout",
98 | "output_type": "stream",
99 | "text": [
100 | "12\n"
101 | ]
102 | }
103 | ],
104 | "source": [
105 | "# Example 2: Program to show the use of lambda functions\n",
106 | "\n",
107 | "double = lambda x: x * 2\n",
108 | "\n",
109 | "print(double(6))"
110 | ]
111 | },
112 | {
113 | "cell_type": "markdown",
114 | "metadata": {},
115 | "source": [
116 | "The statement\n",
117 | "\n",
118 | "```python\n",
119 | ">>> double = lambda x: x * 2\n",
120 | "```\n",
121 | "\n",
122 | "is nearly the same as:\n",
123 | "\n",
124 | "```python\n",
125 | ">>> def double(x):\n",
126 | ">>> return x * 2\n",
127 | "```"
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "execution_count": 4,
133 | "metadata": {
134 | "ExecuteTime": {
135 | "end_time": "2021-06-13T16:39:40.863492Z",
136 | "start_time": "2021-06-13T16:39:40.847866Z"
137 | }
138 | },
139 | "outputs": [
140 | {
141 | "name": "stdout",
142 | "output_type": "stream",
143 | "text": [
144 | "Square of number is 100\n",
145 | "Square of number is 4\n"
146 | ]
147 | }
148 | ],
149 | "source": [
150 | "# Example 3: Normal Function definition is here\n",
151 | "\n",
152 | "def square(x):\n",
153 | " return x*x\n",
154 | "\n",
155 | "# anonymous function\n",
156 | "sqr = lambda x: x*x\n",
157 | "\n",
158 | "#Calling square function\n",
159 | "print(\"Square of number is\",square(10)) #call normal function\n",
160 | "print(\"Square of number is\",sqr(2)) #call anonymous function"
161 | ]
162 | },
163 | {
164 | "cell_type": "markdown",
165 | "metadata": {},
166 | "source": [
167 | "## Use of **`lambda`** Function in python\n",
168 | "\n"
169 | ]
170 | },
171 | {
172 | "cell_type": "markdown",
173 | "metadata": {},
174 | "source": [
175 | "### `lambda` function use with `filter()`\n"
176 | ]
177 | },
178 | {
179 | "cell_type": "code",
180 | "execution_count": 5,
181 | "metadata": {
182 | "ExecuteTime": {
183 | "end_time": "2021-06-13T16:39:42.617383Z",
184 | "start_time": "2021-06-13T16:39:42.602735Z"
185 | }
186 | },
187 | "outputs": [
188 | {
189 | "name": "stdout",
190 | "output_type": "stream",
191 | "text": [
192 | "Even numbers are: [4, 6, 8, 12]\n"
193 | ]
194 | }
195 | ],
196 | "source": [
197 | "# Example 1: Program to filter out only the even items from a list\n",
198 | "\n",
199 | "my_list = [1, 5, 4, 6, 8, 11, 3, 12] # total 8 elements\n",
200 | "\n",
201 | "new_list = list(filter(lambda x: (x%2 == 0), my_list)) # returns the output in form of a list\n",
202 | "\n",
203 | "print(\"Even numbers are: \", new_list)"
204 | ]
205 | },
206 | {
207 | "cell_type": "code",
208 | "execution_count": 6,
209 | "metadata": {
210 | "ExecuteTime": {
211 | "end_time": "2021-06-13T16:39:43.496282Z",
212 | "start_time": "2021-06-13T16:39:43.478704Z"
213 | }
214 | },
215 | "outputs": [
216 | {
217 | "name": "stdout",
218 | "output_type": "stream",
219 | "text": [
220 | "Positive numbers are: [5, 12, 6, 9]\n"
221 | ]
222 | }
223 | ],
224 | "source": [
225 | "# Example 2: \n",
226 | "\n",
227 | "l = [-10, 5, 12, -78, 6, -1, -7, 9]\n",
228 | "positive_nos = list(filter(lambda x: x > 0, l))\n",
229 | "print(\"Positive numbers are: \", positive_nos)"
230 | ]
231 | },
232 | {
233 | "cell_type": "markdown",
234 | "metadata": {},
235 | "source": [
236 | "### `lambda` function with `map()`\n",
237 | "\n"
238 | ]
239 | },
240 | {
241 | "cell_type": "code",
242 | "execution_count": 7,
243 | "metadata": {
244 | "ExecuteTime": {
245 | "end_time": "2021-06-13T16:39:45.093927Z",
246 | "start_time": "2021-06-13T16:39:45.078304Z"
247 | }
248 | },
249 | "outputs": [
250 | {
251 | "name": "stdout",
252 | "output_type": "stream",
253 | "text": [
254 | "Double values are: [2, 10, 8, 12, 16, 22, 6, 24]\n"
255 | ]
256 | }
257 | ],
258 | "source": [
259 | "# Example 1: Program to double each item in a list using map()\n",
260 | "\n",
261 | "my_list = [1, 5, 4, 6, 8, 11, 3, 12] # total 8 elements\n",
262 | "new_list = list(map(lambda x: x * 2, my_list)) # returns the output in form of a list\n",
263 | "print(\"Double values are: \", new_list)"
264 | ]
265 | },
266 | {
267 | "cell_type": "code",
268 | "execution_count": 8,
269 | "metadata": {
270 | "ExecuteTime": {
271 | "end_time": "2021-06-13T16:39:45.524588Z",
272 | "start_time": "2021-06-13T16:39:45.512870Z"
273 | },
274 | "scrolled": true
275 | },
276 | "outputs": [
277 | {
278 | "name": "stdout",
279 | "output_type": "stream",
280 | "text": [
281 | "Cube values are: [8, 27, 64, 512, 729]\n"
282 | ]
283 | }
284 | ],
285 | "source": [
286 | "# Example 2:\n",
287 | "\n",
288 | "list1 = [2, 3, 4, 8, 9]\n",
289 | "list2 = list(map(lambda x: x*x*x, list1))\n",
290 | "print(\"Cube values are:\", list2)"
291 | ]
292 | },
293 | {
294 | "cell_type": "markdown",
295 | "metadata": {},
296 | "source": [
297 | "### `lambda` function with `reduce()`\n"
298 | ]
299 | },
300 | {
301 | "cell_type": "code",
302 | "execution_count": 9,
303 | "metadata": {
304 | "ExecuteTime": {
305 | "end_time": "2021-06-13T16:39:46.968912Z",
306 | "start_time": "2021-06-13T16:39:46.960127Z"
307 | }
308 | },
309 | "outputs": [
310 | {
311 | "name": "stdout",
312 | "output_type": "stream",
313 | "text": [
314 | "Addition of all list elements is : 54\n"
315 | ]
316 | }
317 | ],
318 | "source": [
319 | "# Example 1:\n",
320 | "\n",
321 | "from functools import reduce\n",
322 | "list1 = [20, 13, 4, 8, 9]\n",
323 | "add = reduce(lambda x, y: x+y, list1)\n",
324 | "print(\"Addition of all list elements is : \", add)"
325 | ]
326 | },
327 | {
328 | "cell_type": "code",
329 | "execution_count": null,
330 | "metadata": {},
331 | "outputs": [],
332 | "source": []
333 | }
334 | ],
335 | "metadata": {
336 | "hide_input": false,
337 | "kernelspec": {
338 | "display_name": "Python 3 (ipykernel)",
339 | "language": "python",
340 | "name": "python3"
341 | },
342 | "language_info": {
343 | "codemirror_mode": {
344 | "name": "ipython",
345 | "version": 3
346 | },
347 | "file_extension": ".py",
348 | "mimetype": "text/x-python",
349 | "name": "python",
350 | "nbconvert_exporter": "python",
351 | "pygments_lexer": "ipython3",
352 | "version": "3.9.13"
353 | },
354 | "toc": {
355 | "base_numbering": 1,
356 | "nav_menu": {},
357 | "number_sections": true,
358 | "sideBar": true,
359 | "skip_h1_title": false,
360 | "title_cell": "Table of Contents",
361 | "title_sidebar": "Contents",
362 | "toc_cell": false,
363 | "toc_position": {},
364 | "toc_section_display": true,
365 | "toc_window_display": false
366 | },
367 | "varInspector": {
368 | "cols": {
369 | "lenName": 16,
370 | "lenType": 16,
371 | "lenVar": 40
372 | },
373 | "kernels_config": {
374 | "python": {
375 | "delete_cmd_postfix": "",
376 | "delete_cmd_prefix": "del ",
377 | "library": "var_list.py",
378 | "varRefreshCmd": "print(var_dic_list())"
379 | },
380 | "r": {
381 | "delete_cmd_postfix": ") ",
382 | "delete_cmd_prefix": "rm(",
383 | "library": "var_list.r",
384 | "varRefreshCmd": "cat(var_dic_list()) "
385 | }
386 | },
387 | "types_to_exclude": [
388 | "module",
389 | "function",
390 | "builtin_function_or_method",
391 | "instance",
392 | "_Feature"
393 | ],
394 | "window_display": false
395 | }
396 | },
397 | "nbformat": 4,
398 | "nbformat_minor": 2
399 | }
400 |
--------------------------------------------------------------------------------
/Day 46_Python_Exceptions_Handling - 2.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Exceptions Handling Using `try`, `except` and `finally` statement\n",
8 | "\n",
9 | "In this session, you'll learn how to handle exceptions in your Python program using try, except and finally statements with the help of examples."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## Python `try`-`finally` Clause\n",
17 | "\n",
18 | "The syntax of the **try-finally** statement is this −\n",
19 | "\n",
20 | "```python\n",
21 | ">>> try:\n",
22 | ">>> You do your operations here;\n",
23 | ">>> ......................\n",
24 | ">>> Due to any exception, this may be skipped.\n",
25 | ">>> finally:\n",
26 | ">>> This would always be executed.\n",
27 | ">>> ......................\n",
28 | "```\n",
29 | "\n"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": 1,
35 | "metadata": {
36 | "ExecuteTime": {
37 | "end_time": "2021-10-18T18:23:19.191606Z",
38 | "start_time": "2021-10-18T18:23:19.177935Z"
39 | }
40 | },
41 | "outputs": [
42 | {
43 | "name": "stdout",
44 | "output_type": "stream",
45 | "text": [
46 | "Error: can't find file or read data\n"
47 | ]
48 | }
49 | ],
50 | "source": [
51 | "try:\n",
52 | " fh = open(\"testfile.txt\", \"w\")\n",
53 | " fh.write(\"This is my test file for exception handling!!\")\n",
54 | "finally:\n",
55 | " print (\"Error: can\\'t find file or read data\")\n",
56 | " fh.close()\n",
57 | " \n",
58 | "# If you do not have permission to open the file in writing mode, \n",
59 | "# then this will produce the following result −"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": 2,
65 | "metadata": {
66 | "ExecuteTime": {
67 | "end_time": "2021-10-18T18:23:30.807811Z",
68 | "start_time": "2021-10-18T18:23:30.786329Z"
69 | }
70 | },
71 | "outputs": [],
72 | "source": [
73 | "try:\n",
74 | " f = open(\"testfile.txt\",encoding = 'utf-8')\n",
75 | " # perform file operations\n",
76 | "finally:\n",
77 | " f.close()"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": 3,
83 | "metadata": {
84 | "ExecuteTime": {
85 | "end_time": "2021-10-18T18:23:52.480166Z",
86 | "start_time": "2021-10-18T18:23:45.979682Z"
87 | },
88 | "scrolled": true
89 | },
90 | "outputs": [
91 | {
92 | "name": "stdout",
93 | "output_type": "stream",
94 | "text": [
95 | "Enter your name: Ajantha\n",
96 | "Year you born: 1926\n",
97 | "You are Ajantha. And your age is 96.\n",
98 | "I usually run with the try block\n",
99 | "I alway run.\n"
100 | ]
101 | }
102 | ],
103 | "source": [
104 | "# Example:\n",
105 | "\n",
106 | "try:\n",
107 | " name = input('Enter your name: ')\n",
108 | " year_born = input('Year you born: ')\n",
109 | " age = 2022 - int(year_born)\n",
110 | " print(f'You are {name}. And your age is {age}.')\n",
111 | "except TypeError:\n",
112 | " print('Type error occur')\n",
113 | "except ValueError:\n",
114 | " print('Value error occur')\n",
115 | "except ZeroDivisionError:\n",
116 | " print('zero division error occur')\n",
117 | "else:\n",
118 | " print('I usually run with the try block')\n",
119 | "finally:\n",
120 | " print('I alway run.')"
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": 4,
126 | "metadata": {
127 | "ExecuteTime": {
128 | "end_time": "2021-10-18T18:24:01.871273Z",
129 | "start_time": "2021-10-18T18:23:56.755068Z"
130 | }
131 | },
132 | "outputs": [
133 | {
134 | "name": "stdout",
135 | "output_type": "stream",
136 | "text": [
137 | "Enter your name: Ajantha\n",
138 | "Year you born: 1926\n",
139 | "You are Ajantha. And your age is 96.\n"
140 | ]
141 | }
142 | ],
143 | "source": [
144 | "try:\n",
145 | " name = input('Enter your name: ')\n",
146 | " year_born = input('Year you born: ')\n",
147 | " age = 2022 - int(year_born)\n",
148 | " print(f'You are {name}. And your age is {age}.')\n",
149 | "except Exception as e:\n",
150 | " print(e)"
151 | ]
152 | },
153 | {
154 | "cell_type": "markdown",
155 | "metadata": {},
156 | "source": [
157 | "## Argument of an Exception\n",
158 | "\n"
159 | ]
160 | },
161 | {
162 | "cell_type": "code",
163 | "execution_count": 5,
164 | "metadata": {
165 | "ExecuteTime": {
166 | "end_time": "2021-10-18T18:24:05.502131Z",
167 | "start_time": "2021-10-18T18:24:05.484558Z"
168 | }
169 | },
170 | "outputs": [
171 | {
172 | "name": "stdout",
173 | "output_type": "stream",
174 | "text": [
175 | "The argument does not contain numbers\n",
176 | " invalid literal for int() with base 10: 'xyz'\n"
177 | ]
178 | }
179 | ],
180 | "source": [
181 | "# Define a function here.\n",
182 | "def temp_convert(var):\n",
183 | " try:\n",
184 | " return int(var)\n",
185 | " except ValueError as Argument:\n",
186 | " print (\"The argument does not contain numbers\\n\", Argument)\n",
187 | "\n",
188 | "# Call above function here.\n",
189 | "temp_convert(\"xyz\")"
190 | ]
191 | },
192 | {
193 | "cell_type": "markdown",
194 | "metadata": {},
195 | "source": [
196 | "## Raising an Exception\n",
197 | "\n",
198 | "\n",
199 | "\n",
200 | "The general syntax for the **`raise`** statement is as follows:\n",
201 | "\n",
202 | "```python\n",
203 | "raise [Exception [, args [, traceback]]]\n",
204 | "```\n"
205 | ]
206 | },
207 | {
208 | "cell_type": "code",
209 | "execution_count": 6,
210 | "metadata": {
211 | "ExecuteTime": {
212 | "end_time": "2021-10-18T18:24:08.211606Z",
213 | "start_time": "2021-10-18T18:24:08.203795Z"
214 | }
215 | },
216 | "outputs": [
217 | {
218 | "name": "stdout",
219 | "output_type": "stream",
220 | "text": [
221 | "error in level argument -10\n"
222 | ]
223 | }
224 | ],
225 | "source": [
226 | "def functionName( level ):\n",
227 | " if level < 1:\n",
228 | " raise Exception(level)\n",
229 | " # The code below to this would not be executed\n",
230 | " # if we raise the exception\n",
231 | " return level\n",
232 | "\n",
233 | "try:\n",
234 | " l = functionName(-10)\n",
235 | " print (\"level = \",l)\n",
236 | "except Exception as e:\n",
237 | " print (\"error in level argument\",e.args[0])"
238 | ]
239 | },
240 | {
241 | "cell_type": "markdown",
242 | "metadata": {},
243 | "source": [
244 | "## Writing the “Hello, World!” Program\n",
245 | "\n",
246 | "\n",
247 | "\n",
248 | "```python\n",
249 | ">>> class Networkerror(RuntimeError):\n",
250 | ">>> def _init_(self, arg):\n",
251 | ">>> self.args = arg\n",
252 | "```\n",
253 | " \n",
254 | "So once you have defined the above class, you can raise the exception as follows −\n",
255 | "\n",
256 | "```python\n",
257 | ">>> try:\n",
258 | ">>> raise Networkerror(\"Bad hostname\")\n",
259 | ">>> except Networkerror,e:\n",
260 | ">>> print(e.args)\n",
261 | "```"
262 | ]
263 | },
264 | {
265 | "cell_type": "markdown",
266 | "metadata": {},
267 | "source": [
268 | "## Enumerate\n",
269 | "\n"
270 | ]
271 | },
272 | {
273 | "cell_type": "code",
274 | "execution_count": 7,
275 | "metadata": {
276 | "ExecuteTime": {
277 | "end_time": "2021-10-18T18:24:20.685720Z",
278 | "start_time": "2021-10-18T18:24:20.674978Z"
279 | }
280 | },
281 | "outputs": [
282 | {
283 | "name": "stdout",
284 | "output_type": "stream",
285 | "text": [
286 | "0 20\n",
287 | "1 30\n",
288 | "2 40\n"
289 | ]
290 | }
291 | ],
292 | "source": [
293 | "for index, item in enumerate([20, 30, 40]):\n",
294 | " print(index, item)"
295 | ]
296 | },
297 | {
298 | "cell_type": "code",
299 | "execution_count": 9,
300 | "metadata": {
301 | "ExecuteTime": {
302 | "end_time": "2021-10-18T18:24:21.672534Z",
303 | "start_time": "2021-10-18T18:24:21.651053Z"
304 | }
305 | },
306 | "outputs": [
307 | {
308 | "name": "stdout",
309 | "output_type": "stream",
310 | "text": [
311 | "hi\n",
312 | "The country {i} has been found at index {index}\n",
313 | "hi\n",
314 | "hi\n"
315 | ]
316 | }
317 | ],
318 | "source": [
319 | "countries =[\"Finland\", \"Swedan\", \"india\"]\n",
320 | "for index, i in enumerate(countries):\n",
321 | " print('hi')\n",
322 | " if i == 'Finland':\n",
323 | " print('The country {i} has been found at index {index}')"
324 | ]
325 | },
326 | {
327 | "cell_type": "markdown",
328 | "metadata": {},
329 | "source": [
330 | "## Zip\n",
331 | "\n"
332 | ]
333 | },
334 | {
335 | "cell_type": "code",
336 | "execution_count": 10,
337 | "metadata": {
338 | "ExecuteTime": {
339 | "end_time": "2021-10-18T18:24:23.259935Z",
340 | "start_time": "2021-10-18T18:24:23.243339Z"
341 | }
342 | },
343 | "outputs": [
344 | {
345 | "name": "stdout",
346 | "output_type": "stream",
347 | "text": [
348 | "[{'fruit': 'banana', 'veg': 'Tomato'}, {'fruit': 'orange', 'veg': 'Potato'}, {'fruit': 'mango', 'veg': 'Cabbage'}, {'fruit': 'lemon', 'veg': 'Onion'}, {'fruit': 'lime', 'veg': 'Carrot'}]\n"
349 | ]
350 | }
351 | ],
352 | "source": [
353 | "fruits = ['banana', 'orange', 'mango', 'lemon', 'lime'] \n",
354 | "vegetables = ['Tomato', 'Potato', 'Cabbage','Onion', 'Carrot']\n",
355 | "fruits_and_veges = []\n",
356 | "for f, v in zip(fruits, vegetables):\n",
357 | " fruits_and_veges.append({'fruit':f, 'veg':v})\n",
358 | "\n",
359 | "print(fruits_and_veges)"
360 | ]
361 | },
362 | {
363 | "cell_type": "markdown",
364 | "metadata": {},
365 | "source": [
366 | "## 💻 Exercises ➞ Exceptions Handling\n",
367 | "\n",
368 | "### Exercises ➞ Level 1\n",
369 | "\n",
370 | "1. names = ['Finland', 'Sweden', 'Norway','Denmark','Iceland', 'Estonia','Russia']. Unpack the first five countries and store them in a variable **`nordic_countries`**, store Estonia and Russia in es, and ru respectively."
371 | ]
372 | },
373 | {
374 | "cell_type": "code",
375 | "execution_count": null,
376 | "metadata": {},
377 | "outputs": [],
378 | "source": []
379 | }
380 | ],
381 | "metadata": {
382 | "hide_input": false,
383 | "kernelspec": {
384 | "display_name": "Python 3 (ipykernel)",
385 | "language": "python",
386 | "name": "python3"
387 | },
388 | "language_info": {
389 | "codemirror_mode": {
390 | "name": "ipython",
391 | "version": 3
392 | },
393 | "file_extension": ".py",
394 | "mimetype": "text/x-python",
395 | "name": "python",
396 | "nbconvert_exporter": "python",
397 | "pygments_lexer": "ipython3",
398 | "version": "3.9.13"
399 | },
400 | "toc": {
401 | "base_numbering": 1,
402 | "nav_menu": {},
403 | "number_sections": true,
404 | "sideBar": true,
405 | "skip_h1_title": false,
406 | "title_cell": "Table of Contents",
407 | "title_sidebar": "Contents",
408 | "toc_cell": false,
409 | "toc_position": {},
410 | "toc_section_display": true,
411 | "toc_window_display": false
412 | },
413 | "varInspector": {
414 | "cols": {
415 | "lenName": 16,
416 | "lenType": 16,
417 | "lenVar": 40
418 | },
419 | "kernels_config": {
420 | "python": {
421 | "delete_cmd_postfix": "",
422 | "delete_cmd_prefix": "del ",
423 | "library": "var_list.py",
424 | "varRefreshCmd": "print(var_dic_list())"
425 | },
426 | "r": {
427 | "delete_cmd_postfix": ") ",
428 | "delete_cmd_prefix": "rm(",
429 | "library": "var_list.r",
430 | "varRefreshCmd": "cat(var_dic_list()) "
431 | }
432 | },
433 | "types_to_exclude": [
434 | "module",
435 | "function",
436 | "builtin_function_or_method",
437 | "instance",
438 | "_Feature"
439 | ],
440 | "window_display": false
441 | }
442 | },
443 | "nbformat": 4,
444 | "nbformat_minor": 2
445 | }
446 |
--------------------------------------------------------------------------------
/Day 47_Python_User_defined_Exceptions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Custom Exceptions\n",
8 | "\n",
9 | "In this session, you will learn how to define custom exceptions depending upon your requirements with the help of examples.\n"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## Creating Custom Exceptions\n",
17 | "\n"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": 1,
23 | "metadata": {
24 | "ExecuteTime": {
25 | "end_time": "2021-06-18T17:19:27.045590Z",
26 | "start_time": "2021-06-18T17:19:27.028011Z"
27 | }
28 | },
29 | "outputs": [],
30 | "source": [
31 | "class CustomError(Exception):\n",
32 | " pass"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": 2,
38 | "metadata": {
39 | "ExecuteTime": {
40 | "end_time": "2021-06-18T17:19:28.105637Z",
41 | "start_time": "2021-06-18T17:19:27.828299Z"
42 | }
43 | },
44 | "outputs": [
45 | {
46 | "ename": "CustomError",
47 | "evalue": "",
48 | "output_type": "error",
49 | "traceback": [
50 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
51 | "\u001b[1;31mCustomError\u001b[0m Traceback (most recent call last)",
52 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_1508/571119069.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mCustomError\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
53 | "\u001b[1;31mCustomError\u001b[0m: "
54 | ]
55 | }
56 | ],
57 | "source": [
58 | "raise CustomError"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": 3,
64 | "metadata": {
65 | "ExecuteTime": {
66 | "end_time": "2021-06-18T17:19:28.901530Z",
67 | "start_time": "2021-06-18T17:19:28.883955Z"
68 | }
69 | },
70 | "outputs": [
71 | {
72 | "ename": "CustomError",
73 | "evalue": "An error occurred",
74 | "output_type": "error",
75 | "traceback": [
76 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
77 | "\u001b[1;31mCustomError\u001b[0m Traceback (most recent call last)",
78 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_1508/4012702173.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mCustomError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"An error occurred\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
79 | "\u001b[1;31mCustomError\u001b[0m: An error occurred"
80 | ]
81 | }
82 | ],
83 | "source": [
84 | "raise CustomError(\"An error occurred\")"
85 | ]
86 | },
87 | {
88 | "cell_type": "markdown",
89 | "metadata": {},
90 | "source": [
91 | "## Example: User-Defined Exception in Python\n",
92 | "\n"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": 4,
98 | "metadata": {
99 | "ExecuteTime": {
100 | "end_time": "2021-06-18T17:19:35.140743Z",
101 | "start_time": "2021-06-18T17:19:31.339989Z"
102 | }
103 | },
104 | "outputs": [
105 | {
106 | "name": "stdout",
107 | "output_type": "stream",
108 | "text": [
109 | "Enter a number: 5\n",
110 | "This value is too small, try again!\n",
111 | "\n",
112 | "Enter a number: 15\n",
113 | "This value is too large, try again!\n",
114 | "\n",
115 | "Enter a number: 10\n",
116 | "Congratulations! You guessed it correctly.\n"
117 | ]
118 | },
119 | {
120 | "data": {
121 | "text/plain": [
122 | "'\\n**Output**:\\n\\nEnter a number: 12\\nThis value is too large, try again!\\n\\nEnter a number: 0\\nThis value is too small, try again!\\n\\nEnter a number: 8\\nThis value is too small, try again!\\n\\nEnter a number: 10\\nCongratulations! You guessed it correctly.\\n'"
123 | ]
124 | },
125 | "execution_count": 4,
126 | "metadata": {},
127 | "output_type": "execute_result"
128 | }
129 | ],
130 | "source": [
131 | "# define Python user-defined exceptions\n",
132 | "class Error(Exception):\n",
133 | " \"\"\"Base class for other exceptions\"\"\"\n",
134 | " pass\n",
135 | "\n",
136 | "\n",
137 | "class ValueTooSmallError(Error):\n",
138 | " \"\"\"Raised when the input value is too small\"\"\"\n",
139 | " pass\n",
140 | "\n",
141 | "\n",
142 | "class ValueTooLargeError(Error):\n",
143 | " \"\"\"Raised when the input value is too large\"\"\"\n",
144 | " pass\n",
145 | "\n",
146 | "\n",
147 | "# you need to guess this number\n",
148 | "number = 10\n",
149 | "\n",
150 | "# user guesses a number until he/she gets it right\n",
151 | "while True:\n",
152 | " try:\n",
153 | " i_num = int(input(\"Enter a number: \"))\n",
154 | " if i_num < number:\n",
155 | " raise ValueTooSmallError\n",
156 | " elif i_num > number:\n",
157 | " raise ValueTooLargeError\n",
158 | " break\n",
159 | " except ValueTooSmallError:\n",
160 | " print(\"This value is too small, try again!\")\n",
161 | " print()\n",
162 | " except ValueTooLargeError:\n",
163 | " print(\"This value is too large, try again!\")\n",
164 | " print()\n",
165 | "\n",
166 | "print(\"Congratulations! You guessed it correctly.\")\n",
167 | "\n",
168 | "\n",
169 | "'''\n",
170 | "**Output**:\n",
171 | "\n",
172 | "Enter a number: 12\n",
173 | "This value is too large, try again!\n",
174 | "\n",
175 | "Enter a number: 0\n",
176 | "This value is too small, try again!\n",
177 | "\n",
178 | "Enter a number: 8\n",
179 | "This value is too small, try again!\n",
180 | "\n",
181 | "Enter a number: 10\n",
182 | "Congratulations! You guessed it correctly.\n",
183 | "'''"
184 | ]
185 | },
186 | {
187 | "cell_type": "markdown",
188 | "metadata": {},
189 | "source": [
190 | "## Customizing Exception Classes\n",
191 | "\n"
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "execution_count": 6,
197 | "metadata": {
198 | "ExecuteTime": {
199 | "end_time": "2021-06-18T17:19:44.198773Z",
200 | "start_time": "2021-06-18T17:19:39.516192Z"
201 | }
202 | },
203 | "outputs": [
204 | {
205 | "name": "stdout",
206 | "output_type": "stream",
207 | "text": [
208 | "Enter salary amount: 5001\n"
209 | ]
210 | },
211 | {
212 | "data": {
213 | "text/plain": [
214 | "'\\n**Output**:\\n\\nEnter salary amount: 3000\\n\\nSalaryNotInRangeError Traceback (most recent call last)\\n in \\n 15 salary = int(input(\"Enter salary amount: \"))\\n 16 if not 5000 < salary < 15000:\\n---> 17 raise SalaryNotInRangeError(salary)\\n\\nSalaryNotInRangeError: Salary is not in (5000, 15000) range\\n'"
215 | ]
216 | },
217 | "execution_count": 6,
218 | "metadata": {},
219 | "output_type": "execute_result"
220 | }
221 | ],
222 | "source": [
223 | "class SalaryNotInRangeError(Exception):\n",
224 | " \"\"\"Exception raised for errors in the input salary.\n",
225 | "\n",
226 | " Attributes:\n",
227 | " salary -- input salary which caused the error\n",
228 | " message -- explanation of the error\n",
229 | " \"\"\"\n",
230 | "\n",
231 | " def __init__(self, salary, message=\"Salary is not in (5000, 15000) range\"):\n",
232 | " self.salary = salary\n",
233 | " self.message = message\n",
234 | " super().__init__(self.message)\n",
235 | "\n",
236 | "\n",
237 | "salary = int(input(\"Enter salary amount: \"))\n",
238 | "if not 5000 < salary < 15000:\n",
239 | " raise SalaryNotInRangeError(salary)\n",
240 | " \n",
241 | "'''\n",
242 | "**Output**:\n",
243 | "\n",
244 | "Enter salary amount: 3000\n",
245 | "\n",
246 | "SalaryNotInRangeError Traceback (most recent call last)\n",
247 | " in \n",
248 | " 15 salary = int(input(\"Enter salary amount: \"))\n",
249 | " 16 if not 5000 < salary < 15000:\n",
250 | "---> 17 raise SalaryNotInRangeError(salary)\n",
251 | "\n",
252 | "SalaryNotInRangeError: Salary is not in (5000, 15000) range\n",
253 | "'''"
254 | ]
255 | },
256 | {
257 | "cell_type": "code",
258 | "execution_count": 7,
259 | "metadata": {
260 | "ExecuteTime": {
261 | "end_time": "2021-06-18T17:20:11.265939Z",
262 | "start_time": "2021-06-18T17:20:08.927580Z"
263 | },
264 | "scrolled": true
265 | },
266 | "outputs": [
267 | {
268 | "name": "stdout",
269 | "output_type": "stream",
270 | "text": [
271 | "Enter salary amount: 4000\n"
272 | ]
273 | },
274 | {
275 | "ename": "SalaryNotInRangeError",
276 | "evalue": "4000 -> Salary is not in (5000, 15000) range",
277 | "output_type": "error",
278 | "traceback": [
279 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
280 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m Traceback (most recent call last)",
281 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_1508/916635705.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Enter salary amount: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;36m5000\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m<\u001b[0m \u001b[1;36m15000\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mSalaryNotInRangeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msalary\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 21\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 22\u001b[0m '''\n",
282 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m: 4000 -> Salary is not in (5000, 15000) range"
283 | ]
284 | }
285 | ],
286 | "source": [
287 | "class SalaryNotInRangeError(Exception):\n",
288 | " \"\"\"Exception raised for errors in the input salary.\n",
289 | "\n",
290 | " Attributes:\n",
291 | " salary -- input salary which caused the error\n",
292 | " message -- explanation of the error\n",
293 | " \"\"\"\n",
294 | "\n",
295 | " def __init__(self, salary, message=\"Salary is not in (5000, 15000) range\"):\n",
296 | " self.salary = salary\n",
297 | " self.message = message\n",
298 | " super().__init__(self.message)\n",
299 | "\n",
300 | " def __str__(self):\n",
301 | " return f'{self.salary} -> {self.message}'\n",
302 | "\n",
303 | "\n",
304 | "salary = int(input(\"Enter salary amount: \"))\n",
305 | "if not 5000 < salary < 15000:\n",
306 | " raise SalaryNotInRangeError(salary)\n",
307 | " \n",
308 | "'''\n",
309 | "**Output**:\n",
310 | "\n",
311 | "Enter salary amount: 3000\n",
312 | "\n",
313 | "SalaryNotInRangeError Traceback (most recent call last)\n",
314 | " in \n",
315 | " 18 salary = int(input(\"Enter salary amount: \"))\n",
316 | " 19 if not 5000 < salary < 15000:\n",
317 | "---> 20 raise SalaryNotInRangeError(salary)\n",
318 | "\n",
319 | "SalaryNotInRangeError: 3000 -> Salary is not in (5000, 15000) range\n",
320 | "'''"
321 | ]
322 | },
323 | {
324 | "cell_type": "code",
325 | "execution_count": null,
326 | "metadata": {},
327 | "outputs": [],
328 | "source": []
329 | }
330 | ],
331 | "metadata": {
332 | "hide_input": false,
333 | "kernelspec": {
334 | "display_name": "Python 3 (ipykernel)",
335 | "language": "python",
336 | "name": "python3"
337 | },
338 | "language_info": {
339 | "codemirror_mode": {
340 | "name": "ipython",
341 | "version": 3
342 | },
343 | "file_extension": ".py",
344 | "mimetype": "text/x-python",
345 | "name": "python",
346 | "nbconvert_exporter": "python",
347 | "pygments_lexer": "ipython3",
348 | "version": "3.9.13"
349 | },
350 | "toc": {
351 | "base_numbering": 1,
352 | "nav_menu": {},
353 | "number_sections": true,
354 | "sideBar": true,
355 | "skip_h1_title": false,
356 | "title_cell": "Table of Contents",
357 | "title_sidebar": "Contents",
358 | "toc_cell": false,
359 | "toc_position": {},
360 | "toc_section_display": true,
361 | "toc_window_display": false
362 | },
363 | "varInspector": {
364 | "cols": {
365 | "lenName": 16,
366 | "lenType": 16,
367 | "lenVar": 40
368 | },
369 | "kernels_config": {
370 | "python": {
371 | "delete_cmd_postfix": "",
372 | "delete_cmd_prefix": "del ",
373 | "library": "var_list.py",
374 | "varRefreshCmd": "print(var_dic_list())"
375 | },
376 | "r": {
377 | "delete_cmd_postfix": ") ",
378 | "delete_cmd_prefix": "rm(",
379 | "library": "var_list.r",
380 | "varRefreshCmd": "cat(var_dic_list()) "
381 | }
382 | },
383 | "types_to_exclude": [
384 | "module",
385 | "function",
386 | "builtin_function_or_method",
387 | "instance",
388 | "_Feature"
389 | ],
390 | "window_display": false
391 | }
392 | },
393 | "nbformat": 4,
394 | "nbformat_minor": 2
395 | }
396 |
--------------------------------------------------------------------------------
/Day 55_Python_Iterators.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Iterators\n",
8 | "\n",
9 | "In this session, you will learn how iterator works and how you can build your own iterator using **`__iter__`** and **`__next__`** methods."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## Iterating Through an Iterator\n"
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": 1,
22 | "metadata": {
23 | "ExecuteTime": {
24 | "end_time": "2021-06-22T05:59:09.342273Z",
25 | "start_time": "2021-06-22T05:59:08.896940Z"
26 | }
27 | },
28 | "outputs": [
29 | {
30 | "name": "stdout",
31 | "output_type": "stream",
32 | "text": [
33 | "6\n",
34 | "9\n",
35 | "0\n",
36 | "3\n"
37 | ]
38 | },
39 | {
40 | "ename": "StopIteration",
41 | "evalue": "",
42 | "output_type": "error",
43 | "traceback": [
44 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
45 | "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)",
46 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_7936/892392434.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;31m# This will raise error, no items left\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmy_iter\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
47 | "\u001b[1;31mStopIteration\u001b[0m: "
48 | ]
49 | }
50 | ],
51 | "source": [
52 | "# Example 1:\n",
53 | "\n",
54 | "# define a list\n",
55 | "my_list = [6, 9, 0, 3] # 4 elements\n",
56 | "\n",
57 | "# get an iterator using iter()\n",
58 | "my_iter = iter(my_list)\n",
59 | "\n",
60 | "# iterate through it using next()\n",
61 | "\n",
62 | "print(next(my_iter)) # Output: 6\n",
63 | "print(next(my_iter)) # Output: 9\n",
64 | "\n",
65 | "# next(obj) is same as obj.__next__()\n",
66 | "\n",
67 | "print(my_iter.__next__()) # Output: 0\n",
68 | "print(my_iter.__next__()) # Output: 3\n",
69 | "\n",
70 | "# This will raise error, no items left\n",
71 | "next(my_iter)"
72 | ]
73 | },
74 | {
75 | "cell_type": "code",
76 | "execution_count": 2,
77 | "metadata": {
78 | "ExecuteTime": {
79 | "end_time": "2021-06-22T05:59:13.861477Z",
80 | "start_time": "2021-06-22T05:59:13.849761Z"
81 | }
82 | },
83 | "outputs": [
84 | {
85 | "name": "stdout",
86 | "output_type": "stream",
87 | "text": [
88 | "6\n",
89 | "9\n",
90 | "0\n",
91 | "3\n"
92 | ]
93 | }
94 | ],
95 | "source": [
96 | "for element in my_list: # create a function\n",
97 | " print(element)"
98 | ]
99 | },
100 | {
101 | "cell_type": "markdown",
102 | "metadata": {},
103 | "source": [
104 | "## Working of `for` loop for Iterators\n",
105 | "\n",
106 | "\n",
107 | "\n",
108 | "\n",
109 | "```python\n",
110 | "\n",
111 | ">>> # create an iterator object from that iterable\n",
112 | ">>> iter_obj = iter(iterable)\n",
113 | "\n",
114 | ">>> # infinite loop\n",
115 | ">>> while True:\n",
116 | ">>> try:\n",
117 | ">>> # get the next item\n",
118 | ">>> element = next(iter_obj)\n",
119 | ">>> # do something with element\n",
120 | ">>> except StopIteration:\n",
121 | ">>> # if StopIteration is raised, break from loop\n",
122 | ">>> break\n",
123 | "```\n"
124 | ]
125 | },
126 | {
127 | "cell_type": "markdown",
128 | "metadata": {},
129 | "source": [
130 | "## Building Custom Iterators\n",
131 | "\n",
132 | "Building an iterator from scratch is easy in Python. We just have to implement the **`__iter__()`** and the **`__next__()`** methods.\n",
133 | "\n",
134 | "\n"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": 3,
140 | "metadata": {
141 | "ExecuteTime": {
142 | "end_time": "2021-06-22T05:59:16.468016Z",
143 | "start_time": "2021-06-22T05:59:16.447511Z"
144 | }
145 | },
146 | "outputs": [
147 | {
148 | "name": "stdout",
149 | "output_type": "stream",
150 | "text": [
151 | "1\n",
152 | "2\n",
153 | "4\n",
154 | "8\n",
155 | "16\n"
156 | ]
157 | }
158 | ],
159 | "source": [
160 | "class PowTwo:\n",
161 | " \"\"\"Class to implement an iterator\n",
162 | " of powers of two\"\"\"\n",
163 | "\n",
164 | " def __init__(self, max=0):\n",
165 | " self.max = max\n",
166 | "\n",
167 | " def __iter__(self):\n",
168 | " self.n = 0\n",
169 | " return self\n",
170 | "\n",
171 | " def __next__(self):\n",
172 | " if self.n <= self.max:\n",
173 | " result = 2 ** self.n\n",
174 | " self.n += 1\n",
175 | " return result\n",
176 | " else:\n",
177 | " raise StopIteration\n",
178 | "\n",
179 | "\n",
180 | "# create an object\n",
181 | "numbers = PowTwo(4)\n",
182 | "\n",
183 | "# create an iterable from the object\n",
184 | "i = iter(numbers)\n",
185 | "\n",
186 | "# Using next to get to the next iterator element\n",
187 | "print(next(i))\n",
188 | "print(next(i))\n",
189 | "print(next(i))\n",
190 | "print(next(i))\n",
191 | "print(next(i))"
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "execution_count": 4,
197 | "metadata": {
198 | "ExecuteTime": {
199 | "end_time": "2021-06-22T05:59:18.986663Z",
200 | "start_time": "2021-06-22T05:59:18.968112Z"
201 | }
202 | },
203 | "outputs": [
204 | {
205 | "name": "stdout",
206 | "output_type": "stream",
207 | "text": [
208 | "1\n",
209 | "2\n",
210 | "4\n",
211 | "8\n",
212 | "16\n",
213 | "32\n"
214 | ]
215 | }
216 | ],
217 | "source": [
218 | "for i in PowTwo(5): # calling the class\n",
219 | " print(i)"
220 | ]
221 | },
222 | {
223 | "cell_type": "markdown",
224 | "metadata": {},
225 | "source": [
226 | "## Python Infinite Iterators\n",
227 | "\n"
228 | ]
229 | },
230 | {
231 | "cell_type": "code",
232 | "execution_count": 5,
233 | "metadata": {},
234 | "outputs": [
235 | {
236 | "data": {
237 | "text/plain": [
238 | "0"
239 | ]
240 | },
241 | "execution_count": 5,
242 | "metadata": {},
243 | "output_type": "execute_result"
244 | }
245 | ],
246 | "source": [
247 | "int()\n",
248 | "inf = iter(int,1)\n",
249 | "next(inf)\n"
250 | ]
251 | },
252 | {
253 | "cell_type": "code",
254 | "execution_count": 6,
255 | "metadata": {},
256 | "outputs": [
257 | {
258 | "data": {
259 | "text/plain": [
260 | "0"
261 | ]
262 | },
263 | "execution_count": 6,
264 | "metadata": {},
265 | "output_type": "execute_result"
266 | }
267 | ],
268 | "source": [
269 | "next(inf)"
270 | ]
271 | },
272 | {
273 | "cell_type": "code",
274 | "execution_count": 7,
275 | "metadata": {},
276 | "outputs": [
277 | {
278 | "data": {
279 | "text/plain": [
280 | "0"
281 | ]
282 | },
283 | "execution_count": 7,
284 | "metadata": {},
285 | "output_type": "execute_result"
286 | }
287 | ],
288 | "source": [
289 | "next(inf)"
290 | ]
291 | },
292 | {
293 | "cell_type": "code",
294 | "execution_count": 8,
295 | "metadata": {},
296 | "outputs": [],
297 | "source": [
298 | "class InfIter:\n",
299 | " \"\"\"Infinite iterator to return all\n",
300 | " odd numbers\"\"\"\n",
301 | "\n",
302 | " def __iter__(self):\n",
303 | " self.num = 1\n",
304 | " return self\n",
305 | " def __next__(self):\n",
306 | " num = self.num\n",
307 | " self.num += 2\n",
308 | " return num"
309 | ]
310 | },
311 | {
312 | "cell_type": "code",
313 | "execution_count": 9,
314 | "metadata": {},
315 | "outputs": [],
316 | "source": [
317 | "a = iter(InfIter())"
318 | ]
319 | },
320 | {
321 | "cell_type": "code",
322 | "execution_count": 10,
323 | "metadata": {},
324 | "outputs": [
325 | {
326 | "data": {
327 | "text/plain": [
328 | "1"
329 | ]
330 | },
331 | "execution_count": 10,
332 | "metadata": {},
333 | "output_type": "execute_result"
334 | }
335 | ],
336 | "source": [
337 | "next(a)"
338 | ]
339 | },
340 | {
341 | "cell_type": "code",
342 | "execution_count": 11,
343 | "metadata": {},
344 | "outputs": [
345 | {
346 | "data": {
347 | "text/plain": [
348 | "3"
349 | ]
350 | },
351 | "execution_count": 11,
352 | "metadata": {},
353 | "output_type": "execute_result"
354 | }
355 | ],
356 | "source": [
357 | "next(a)"
358 | ]
359 | },
360 | {
361 | "cell_type": "code",
362 | "execution_count": 12,
363 | "metadata": {},
364 | "outputs": [
365 | {
366 | "data": {
367 | "text/plain": [
368 | "5"
369 | ]
370 | },
371 | "execution_count": 12,
372 | "metadata": {},
373 | "output_type": "execute_result"
374 | }
375 | ],
376 | "source": [
377 | "next(a)"
378 | ]
379 | },
380 | {
381 | "cell_type": "code",
382 | "execution_count": 13,
383 | "metadata": {},
384 | "outputs": [
385 | {
386 | "data": {
387 | "text/plain": [
388 | "7"
389 | ]
390 | },
391 | "execution_count": 13,
392 | "metadata": {},
393 | "output_type": "execute_result"
394 | }
395 | ],
396 | "source": [
397 | "next(a)"
398 | ]
399 | },
400 | {
401 | "cell_type": "code",
402 | "execution_count": 14,
403 | "metadata": {},
404 | "outputs": [
405 | {
406 | "data": {
407 | "text/plain": [
408 | "9"
409 | ]
410 | },
411 | "execution_count": 14,
412 | "metadata": {},
413 | "output_type": "execute_result"
414 | }
415 | ],
416 | "source": [
417 | "next(a)"
418 | ]
419 | },
420 | {
421 | "cell_type": "code",
422 | "execution_count": null,
423 | "metadata": {},
424 | "outputs": [],
425 | "source": []
426 | }
427 | ],
428 | "metadata": {
429 | "hide_input": false,
430 | "kernelspec": {
431 | "display_name": "Python 3 (ipykernel)",
432 | "language": "python",
433 | "name": "python3"
434 | },
435 | "language_info": {
436 | "codemirror_mode": {
437 | "name": "ipython",
438 | "version": 3
439 | },
440 | "file_extension": ".py",
441 | "mimetype": "text/x-python",
442 | "name": "python",
443 | "nbconvert_exporter": "python",
444 | "pygments_lexer": "ipython3",
445 | "version": "3.9.7"
446 | },
447 | "toc": {
448 | "base_numbering": 1,
449 | "nav_menu": {},
450 | "number_sections": true,
451 | "sideBar": true,
452 | "skip_h1_title": false,
453 | "title_cell": "Table of Contents",
454 | "title_sidebar": "Contents",
455 | "toc_cell": false,
456 | "toc_position": {},
457 | "toc_section_display": true,
458 | "toc_window_display": false
459 | },
460 | "varInspector": {
461 | "cols": {
462 | "lenName": 16,
463 | "lenType": 16,
464 | "lenVar": 40
465 | },
466 | "kernels_config": {
467 | "python": {
468 | "delete_cmd_postfix": "",
469 | "delete_cmd_prefix": "del ",
470 | "library": "var_list.py",
471 | "varRefreshCmd": "print(var_dic_list())"
472 | },
473 | "r": {
474 | "delete_cmd_postfix": ") ",
475 | "delete_cmd_prefix": "rm(",
476 | "library": "var_list.r",
477 | "varRefreshCmd": "cat(var_dic_list()) "
478 | }
479 | },
480 | "types_to_exclude": [
481 | "module",
482 | "function",
483 | "builtin_function_or_method",
484 | "instance",
485 | "_Feature"
486 | ],
487 | "window_display": false
488 | }
489 | },
490 | "nbformat": 4,
491 | "nbformat_minor": 4
492 | }
493 |
--------------------------------------------------------------------------------
/Day 56_Python_Generators.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Generators\n",
8 | "\n",
9 | "In this session, you'll learn how to create iterations easily using Python generators, how it is different from iterators and normal functions, and why you should use it."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## Create Generators in Python\n",
17 | "\n",
18 | "It is fairly simple to create a generator in Python. It is as easy as defining a normal function, but with a **`yield`** statement instead of a **`return`** statement."
19 | ]
20 | },
21 | {
22 | "cell_type": "markdown",
23 | "metadata": {},
24 | "source": [
25 | "## Differences between Generator function and Normal function\n"
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {},
31 | "source": [
32 | "Here is an example to illustrate all of the points stated above. We have a generator function named **`my_gen()`** with several **`yield`** statements.\n",
33 | "\n",
34 | "```python\n",
35 | ">>> # A simple generator function\n",
36 | ">>> def my_gen():\n",
37 | ">>> n = 1\n",
38 | ">>> print('This is printed first')\n",
39 | ">>> # Generator function contains yield statements\n",
40 | ">>> yield n\n",
41 | "\n",
42 | ">>> n += 1\n",
43 | ">>> print('This is printed second')\n",
44 | ">>> yield n\n",
45 | "\n",
46 | ">>> n += 1\n",
47 | ">>> print('This is printed at last')\n",
48 | ">>> yield n\n",
49 | "```\n",
50 | "\n",
51 | "An interactive run in the interpreter is given below. Run these in the Python shell to see the output.\n",
52 | "\n",
53 | "```python\n",
54 | ">>> # It returns an object but does not start execution immediately.\n",
55 | ">>> a = my_gen()\n",
56 | "\n",
57 | ">>> # We can iterate through the items using next().\n",
58 | ">>> next(a)\n",
59 | "This is printed first\n",
60 | "1\n",
61 | ">>> # Once the function yields, the function is paused and the control is transferred to the caller.\n",
62 | "\n",
63 | ">>> # Local variables and theirs states are remembered between successive calls.\n",
64 | ">>> next(a)\n",
65 | "This is printed second\n",
66 | "2\n",
67 | "\n",
68 | ">>> next(a)\n",
69 | "This is printed at last\n",
70 | "3\n",
71 | "\n",
72 | ">>> # Finally, when the function terminates, StopIteration is raised automatically on further calls.\n",
73 | ">>> next(a)\n",
74 | "Traceback (most recent call last):\n",
75 | "...\n",
76 | "StopIteration\n",
77 | ">>> next(a)\n",
78 | "Traceback (most recent call last):\n",
79 | "...\n",
80 | "StopIteration\n",
81 | "```"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": 1,
87 | "metadata": {
88 | "ExecuteTime": {
89 | "end_time": "2021-06-22T06:08:41.287154Z",
90 | "start_time": "2021-06-22T06:08:41.275439Z"
91 | }
92 | },
93 | "outputs": [
94 | {
95 | "name": "stdout",
96 | "output_type": "stream",
97 | "text": [
98 | "This is printed first\n",
99 | "1\n",
100 | "This is printed second\n",
101 | "2\n",
102 | "This is printed at last\n",
103 | "3\n"
104 | ]
105 | }
106 | ],
107 | "source": [
108 | "# A simple generator function\n",
109 | "\n",
110 | "def my_gen():\n",
111 | " n = 1\n",
112 | " print('This is printed first')\n",
113 | " # Generator function contains yield statements\n",
114 | " yield n\n",
115 | "\n",
116 | " n += 1\n",
117 | " print('This is printed second')\n",
118 | " yield n\n",
119 | "\n",
120 | " n += 1\n",
121 | " print('This is printed at last')\n",
122 | " yield n\n",
123 | "\n",
124 | "\n",
125 | "# Using for loop\n",
126 | "for item in my_gen():\n",
127 | " print(item)"
128 | ]
129 | },
130 | {
131 | "cell_type": "markdown",
132 | "metadata": {},
133 | "source": [
134 | "## Python Generators with a Loop\n",
135 | "\n"
136 | ]
137 | },
138 | {
139 | "cell_type": "code",
140 | "execution_count": 2,
141 | "metadata": {
142 | "ExecuteTime": {
143 | "end_time": "2021-06-22T06:08:44.728642Z",
144 | "start_time": "2021-06-22T06:08:44.710090Z"
145 | }
146 | },
147 | "outputs": [
148 | {
149 | "name": "stdout",
150 | "output_type": "stream",
151 | "text": [
152 | "o\n",
153 | "l\n",
154 | "l\n",
155 | "e\n",
156 | "h\n"
157 | ]
158 | }
159 | ],
160 | "source": [
161 | "def rev_str(my_str):\n",
162 | " length = len(my_str)\n",
163 | " for i in range(length - 1, -1, -1):\n",
164 | " yield my_str[i]\n",
165 | "\n",
166 | "\n",
167 | "# For loop to reverse the string\n",
168 | "for char in rev_str(\"hello\"):\n",
169 | " print(char)"
170 | ]
171 | },
172 | {
173 | "cell_type": "markdown",
174 | "metadata": {},
175 | "source": [
176 | "## Python Generator Expression\n",
177 | "\n"
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "execution_count": 3,
183 | "metadata": {
184 | "ExecuteTime": {
185 | "end_time": "2021-06-22T06:08:51.544724Z",
186 | "start_time": "2021-06-22T06:08:51.532032Z"
187 | }
188 | },
189 | "outputs": [
190 | {
191 | "name": "stdout",
192 | "output_type": "stream",
193 | "text": [
194 | "[1, 9, 36, 100]\n",
195 | " at 0x0000021F93EA54A0>\n"
196 | ]
197 | }
198 | ],
199 | "source": [
200 | "# Initialize the list\n",
201 | "my_list = [1, 3, 6, 10]\n",
202 | "\n",
203 | "# square each term using list comprehension\n",
204 | "list_ = [x**2 for x in my_list]\n",
205 | "\n",
206 | "# same thing can be done using a generator expression\n",
207 | "# generator expressions are surrounded by parenthesis ()\n",
208 | "generator = (x**2 for x in my_list)\n",
209 | "\n",
210 | "print(list_)\n",
211 | "print(generator)"
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": 12,
217 | "metadata": {
218 | "ExecuteTime": {
219 | "end_time": "2021-06-22T06:08:54.397338Z",
220 | "start_time": "2021-06-22T06:08:54.076039Z"
221 | }
222 | },
223 | "outputs": [
224 | {
225 | "name": "stdout",
226 | "output_type": "stream",
227 | "text": [
228 | "1\n",
229 | "9\n",
230 | "36\n",
231 | "100\n"
232 | ]
233 | }
234 | ],
235 | "source": [
236 | "# Initialize the list\n",
237 | "my_list = [1, 3, 6, 10]\n",
238 | "\n",
239 | "a = (x**2 for x in my_list)\n",
240 | "print(next(a))\n",
241 | "\n",
242 | "print(next(a))\n",
243 | "\n",
244 | "print(next(a))\n",
245 | "\n",
246 | "print(next(a))\n",
247 | "\n",
248 | "#print(next(a))"
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": 13,
254 | "metadata": {
255 | "ExecuteTime": {
256 | "end_time": "2021-06-22T06:09:00.644066Z",
257 | "start_time": "2021-06-22T06:08:56.219645Z"
258 | }
259 | },
260 | "outputs": [
261 | {
262 | "data": {
263 | "text/plain": [
264 | "146"
265 | ]
266 | },
267 | "execution_count": 13,
268 | "metadata": {},
269 | "output_type": "execute_result"
270 | }
271 | ],
272 | "source": [
273 | "sum(x**2 for x in my_list)"
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": 14,
279 | "metadata": {
280 | "ExecuteTime": {
281 | "end_time": "2021-06-22T06:09:00.763212Z",
282 | "start_time": "2021-06-22T06:09:00.730988Z"
283 | }
284 | },
285 | "outputs": [
286 | {
287 | "data": {
288 | "text/plain": [
289 | "100"
290 | ]
291 | },
292 | "execution_count": 14,
293 | "metadata": {},
294 | "output_type": "execute_result"
295 | }
296 | ],
297 | "source": [
298 | "max(x**2 for x in my_list)"
299 | ]
300 | },
301 | {
302 | "cell_type": "markdown",
303 | "metadata": {},
304 | "source": [
305 | "## Use of Python Generators\n",
306 | "\n",
307 | "There are several reasons that make generators a powerful implementation."
308 | ]
309 | },
310 | {
311 | "cell_type": "markdown",
312 | "metadata": {},
313 | "source": [
314 | "## 1. Easy to Implement\n",
315 | "\n",
316 | "\n",
317 | "\n",
318 | "```python\n",
319 | ">>> class PowTwo:\n",
320 | ">>> def __init__(self, max=0):\n",
321 | ">>> self.n = 0\n",
322 | ">>> self.max = max\n",
323 | "\n",
324 | ">>> def __iter__(self):\n",
325 | ">>> return self\n",
326 | "\n",
327 | ">>> def __next__(self):\n",
328 | ">>> if self.n > self.max:\n",
329 | ">>> raise StopIteration\n",
330 | "\n",
331 | ">>> result = 2 ** self.n\n",
332 | ">>> self.n += 1\n",
333 | ">>> return result\n",
334 | "```\n",
335 | "\n",
336 | "## Generator\n",
337 | "\n",
338 | "```python\n",
339 | ">>> def PowTwoGen(max=0):\n",
340 | ">>> n = 0\n",
341 | ">>> while n < max:\n",
342 | ">>> yield 2 ** n\n",
343 | ">>> n += 1\n",
344 | "```\n",
345 | "\n"
346 | ]
347 | },
348 | {
349 | "cell_type": "markdown",
350 | "metadata": {},
351 | "source": [
352 | "## 2. Memory Efficient\n",
353 | "\n",
354 | "A normal function to return a sequence will create the entire sequence in memory before returning the result."
355 | ]
356 | },
357 | {
358 | "cell_type": "markdown",
359 | "metadata": {},
360 | "source": [
361 | "## 3. Represent Infinite Stream\n",
362 | "\n",
363 | "\n",
364 | "The following generator function can generate all the even numbers (at least in theory).\n",
365 | "\n",
366 | "```python\n",
367 | ">>> def all_even():\n",
368 | ">>> n = 0\n",
369 | ">>> while True:\n",
370 | ">>> yield n\n",
371 | ">>> n += 2\n",
372 | "```"
373 | ]
374 | },
375 | {
376 | "cell_type": "markdown",
377 | "metadata": {},
378 | "source": [
379 | "## 4. Pipelining Generators\n",
380 | "\n"
381 | ]
382 | },
383 | {
384 | "cell_type": "code",
385 | "execution_count": 15,
386 | "metadata": {
387 | "ExecuteTime": {
388 | "end_time": "2021-06-22T06:09:09.356685Z",
389 | "start_time": "2021-06-22T06:09:09.348874Z"
390 | }
391 | },
392 | "outputs": [
393 | {
394 | "name": "stdout",
395 | "output_type": "stream",
396 | "text": [
397 | "4895\n"
398 | ]
399 | }
400 | ],
401 | "source": [
402 | "def fibonacci_numbers(nums):\n",
403 | " x, y = 0, 1\n",
404 | " for _ in range(nums):\n",
405 | " x, y = y, x+y\n",
406 | " yield x\n",
407 | "\n",
408 | "def square(nums):\n",
409 | " for num in nums:\n",
410 | " yield num**2\n",
411 | "\n",
412 | "print(sum(square(fibonacci_numbers(10))))"
413 | ]
414 | },
415 | {
416 | "cell_type": "code",
417 | "execution_count": null,
418 | "metadata": {},
419 | "outputs": [],
420 | "source": []
421 | }
422 | ],
423 | "metadata": {
424 | "hide_input": false,
425 | "kernelspec": {
426 | "display_name": "Python 3 (ipykernel)",
427 | "language": "python",
428 | "name": "python3"
429 | },
430 | "language_info": {
431 | "codemirror_mode": {
432 | "name": "ipython",
433 | "version": 3
434 | },
435 | "file_extension": ".py",
436 | "mimetype": "text/x-python",
437 | "name": "python",
438 | "nbconvert_exporter": "python",
439 | "pygments_lexer": "ipython3",
440 | "version": "3.9.13"
441 | },
442 | "toc": {
443 | "base_numbering": 1,
444 | "nav_menu": {},
445 | "number_sections": true,
446 | "sideBar": true,
447 | "skip_h1_title": false,
448 | "title_cell": "Table of Contents",
449 | "title_sidebar": "Contents",
450 | "toc_cell": false,
451 | "toc_position": {},
452 | "toc_section_display": true,
453 | "toc_window_display": false
454 | },
455 | "varInspector": {
456 | "cols": {
457 | "lenName": 16,
458 | "lenType": 16,
459 | "lenVar": 40
460 | },
461 | "kernels_config": {
462 | "python": {
463 | "delete_cmd_postfix": "",
464 | "delete_cmd_prefix": "del ",
465 | "library": "var_list.py",
466 | "varRefreshCmd": "print(var_dic_list())"
467 | },
468 | "r": {
469 | "delete_cmd_postfix": ") ",
470 | "delete_cmd_prefix": "rm(",
471 | "library": "var_list.r",
472 | "varRefreshCmd": "cat(var_dic_list()) "
473 | }
474 | },
475 | "types_to_exclude": [
476 | "module",
477 | "function",
478 | "builtin_function_or_method",
479 | "instance",
480 | "_Feature"
481 | ],
482 | "window_display": false
483 | }
484 | },
485 | "nbformat": 4,
486 | "nbformat_minor": 4
487 | }
488 |
--------------------------------------------------------------------------------
/Day 57_Python_Closure.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Closures\n",
8 | "\n",
9 | "In this Session, you'll learn about Python closure, how to define a closure, and the reasons you should use it.\n",
10 | "\n"
11 | ]
12 | },
13 | {
14 | "cell_type": "markdown",
15 | "metadata": {},
16 | "source": [
17 | "## Higher Order Functions\n",
18 | "\n"
19 | ]
20 | },
21 | {
22 | "cell_type": "markdown",
23 | "metadata": {},
24 | "source": [
25 | "### Function as a Parameter"
26 | ]
27 | },
28 | {
29 | "cell_type": "code",
30 | "execution_count": 1,
31 | "metadata": {
32 | "ExecuteTime": {
33 | "end_time": "2021-10-18T07:33:06.599037Z",
34 | "start_time": "2021-10-18T07:33:06.586340Z"
35 | }
36 | },
37 | "outputs": [
38 | {
39 | "name": "stdout",
40 | "output_type": "stream",
41 | "text": [
42 | "15\n"
43 | ]
44 | }
45 | ],
46 | "source": [
47 | "def sum_numbers(nums): # normal function\n",
48 | " return sum(nums) # a sad function abusing the built-in sum function :<\n",
49 | "\n",
50 | "def higher_order_function(f, lst): # function as a parameter\n",
51 | " summation = f(lst)\n",
52 | " return summation\n",
53 | "result = higher_order_function(sum_numbers, [1, 2, 3, 4, 5])\n",
54 | "print(result) # 15"
55 | ]
56 | },
57 | {
58 | "cell_type": "markdown",
59 | "metadata": {},
60 | "source": [
61 | "### Function as a Return Value"
62 | ]
63 | },
64 | {
65 | "cell_type": "code",
66 | "execution_count": 2,
67 | "metadata": {
68 | "ExecuteTime": {
69 | "end_time": "2021-10-18T07:33:06.752357Z",
70 | "start_time": "2021-10-18T07:33:06.610758Z"
71 | }
72 | },
73 | "outputs": [
74 | {
75 | "name": "stdout",
76 | "output_type": "stream",
77 | "text": [
78 | "9\n",
79 | "27\n",
80 | "3\n"
81 | ]
82 | }
83 | ],
84 | "source": [
85 | "def square(x): # a square function\n",
86 | " return x ** 2\n",
87 | "\n",
88 | "def cube(x): # a cube function\n",
89 | " return x ** 3\n",
90 | "\n",
91 | "def absolute(x): # an absolute value function\n",
92 | " if x >= 0:\n",
93 | " return x\n",
94 | " else:\n",
95 | " return -(x)\n",
96 | "\n",
97 | "def higher_order_function(type): # a higher order function returning a function\n",
98 | " if type == 'square':\n",
99 | " return square\n",
100 | " elif type == 'cube':\n",
101 | " return cube\n",
102 | " elif type == 'absolute':\n",
103 | " return absolute\n",
104 | "\n",
105 | "result = higher_order_function('square')\n",
106 | "print(result(3)) # 9\n",
107 | "result = higher_order_function('cube')\n",
108 | "print(result(3)) # 27\n",
109 | "result = higher_order_function('absolute')\n",
110 | "print(result(-3)) # 3"
111 | ]
112 | },
113 | {
114 | "cell_type": "markdown",
115 | "metadata": {},
116 | "source": [
117 | "You can see from the above example that the higher order function is returning different functions depending on the passed parameter"
118 | ]
119 | },
120 | {
121 | "cell_type": "markdown",
122 | "metadata": {},
123 | "source": [
124 | "## Nonlocal variable in a nested function\n"
125 | ]
126 | },
127 | {
128 | "cell_type": "code",
129 | "execution_count": 3,
130 | "metadata": {
131 | "ExecuteTime": {
132 | "end_time": "2021-10-18T07:33:06.892983Z",
133 | "start_time": "2021-10-18T07:33:06.758218Z"
134 | },
135 | "scrolled": true
136 | },
137 | "outputs": [
138 | {
139 | "name": "stdout",
140 | "output_type": "stream",
141 | "text": [
142 | "Hello\n"
143 | ]
144 | }
145 | ],
146 | "source": [
147 | "def print_msg(msg):\n",
148 | " # This is the outer enclosing function\n",
149 | "\n",
150 | " def printer():\n",
151 | " # This is the nested function\n",
152 | " print(msg)\n",
153 | "\n",
154 | " printer()\n",
155 | "\n",
156 | "# We execute the function\n",
157 | "# Output: Hello\n",
158 | "print_msg(\"Hello\")"
159 | ]
160 | },
161 | {
162 | "cell_type": "markdown",
163 | "metadata": {},
164 | "source": [
165 | "## Defining a Closure Function\n",
166 | "\n"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": 4,
172 | "metadata": {
173 | "ExecuteTime": {
174 | "end_time": "2021-10-18T07:33:06.998455Z",
175 | "start_time": "2021-10-18T07:33:06.901774Z"
176 | }
177 | },
178 | "outputs": [
179 | {
180 | "name": "stdout",
181 | "output_type": "stream",
182 | "text": [
183 | "Hello\n"
184 | ]
185 | }
186 | ],
187 | "source": [
188 | "def print_msg(msg):\n",
189 | " # This is the outer enclosing function\n",
190 | "\n",
191 | " def printer():\n",
192 | " # This is the nested function\n",
193 | " print(msg)\n",
194 | "\n",
195 | " return printer # returns the nested function\n",
196 | "\n",
197 | "\n",
198 | "# Now let's try calling this function.\n",
199 | "# Output: Hello\n",
200 | "another = print_msg(\"Hello\")\n",
201 | "another()"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "execution_count": 5,
207 | "metadata": {
208 | "ExecuteTime": {
209 | "end_time": "2021-10-18T07:33:07.150794Z",
210 | "start_time": "2021-10-18T07:33:07.017006Z"
211 | }
212 | },
213 | "outputs": [
214 | {
215 | "name": "stdout",
216 | "output_type": "stream",
217 | "text": [
218 | "Hello\n"
219 | ]
220 | }
221 | ],
222 | "source": [
223 | "del print_msg\n",
224 | "another()"
225 | ]
226 | },
227 | {
228 | "cell_type": "code",
229 | "execution_count": 6,
230 | "metadata": {
231 | "ExecuteTime": {
232 | "end_time": "2021-10-18T07:33:07.862712Z",
233 | "start_time": "2021-10-18T07:33:07.153726Z"
234 | },
235 | "scrolled": true
236 | },
237 | "outputs": [
238 | {
239 | "ename": "NameError",
240 | "evalue": "name 'print_msg' is not defined",
241 | "output_type": "error",
242 | "traceback": [
243 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
244 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
245 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_24660/2744694622.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint_msg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Hello\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
246 | "\u001b[1;31mNameError\u001b[0m: name 'print_msg' is not defined"
247 | ]
248 | }
249 | ],
250 | "source": [
251 | "print_msg(\"Hello\")"
252 | ]
253 | },
254 | {
255 | "cell_type": "markdown",
256 | "metadata": {},
257 | "source": [
258 | "## When do we have closures?\n",
259 | "\n"
260 | ]
261 | },
262 | {
263 | "cell_type": "markdown",
264 | "metadata": {},
265 | "source": [
266 | "## When to use closures?\n",
267 | "\n"
268 | ]
269 | },
270 | {
271 | "cell_type": "code",
272 | "execution_count": 7,
273 | "metadata": {
274 | "ExecuteTime": {
275 | "end_time": "2021-10-18T07:33:13.293885Z",
276 | "start_time": "2021-10-18T07:33:13.281190Z"
277 | }
278 | },
279 | "outputs": [
280 | {
281 | "name": "stdout",
282 | "output_type": "stream",
283 | "text": [
284 | "27\n",
285 | "15\n",
286 | "30\n"
287 | ]
288 | }
289 | ],
290 | "source": [
291 | "# Example:\n",
292 | "\n",
293 | "def make_multiplier_of(n):\n",
294 | " def multiplier(x):\n",
295 | " return x * n\n",
296 | " return multiplier\n",
297 | "\n",
298 | "\n",
299 | "# Multiplier of 3\n",
300 | "times3 = make_multiplier_of(3)\n",
301 | "\n",
302 | "# Multiplier of 5\n",
303 | "times5 = make_multiplier_of(5)\n",
304 | "\n",
305 | "\n",
306 | "print(times3(9)) # Output: 27\n",
307 | "\n",
308 | "print(times5(3)) # Output: 15\n",
309 | "\n",
310 | "print(times5(times3(2))) # Output: 30 \n",
311 | "\n",
312 | "# 5 *(3 * 2)"
313 | ]
314 | },
315 | {
316 | "cell_type": "code",
317 | "execution_count": 8,
318 | "metadata": {
319 | "ExecuteTime": {
320 | "end_time": "2021-10-18T07:33:14.004822Z",
321 | "start_time": "2021-10-18T07:33:13.984320Z"
322 | }
323 | },
324 | "outputs": [
325 | {
326 | "name": "stdout",
327 | "output_type": "stream",
328 | "text": [
329 | "15\n",
330 | "20\n"
331 | ]
332 | }
333 | ],
334 | "source": [
335 | "# Example:\n",
336 | "\n",
337 | "def add_ten():\n",
338 | " ten = 10\n",
339 | " def add(num):\n",
340 | " return num + ten\n",
341 | " return add\n",
342 | "\n",
343 | "closure_result = add_ten()\n",
344 | "print(closure_result(5)) # 15\n",
345 | "print(closure_result(10)) # 20"
346 | ]
347 | },
348 | {
349 | "cell_type": "markdown",
350 | "metadata": {},
351 | "source": [
352 | "# Python Decorators"
353 | ]
354 | },
355 | {
356 | "cell_type": "code",
357 | "execution_count": 9,
358 | "metadata": {
359 | "ExecuteTime": {
360 | "end_time": "2021-10-18T07:33:15.157170Z",
361 | "start_time": "2021-10-18T07:33:15.128852Z"
362 | }
363 | },
364 | "outputs": [
365 | {
366 | "data": {
367 | "text/plain": [
368 | "(,)"
369 | ]
370 | },
371 | "execution_count": 9,
372 | "metadata": {},
373 | "output_type": "execute_result"
374 | }
375 | ],
376 | "source": [
377 | "make_multiplier_of.__closure__\n",
378 | "times3.__closure__"
379 | ]
380 | },
381 | {
382 | "cell_type": "code",
383 | "execution_count": 10,
384 | "metadata": {
385 | "ExecuteTime": {
386 | "end_time": "2021-10-18T07:33:16.187448Z",
387 | "start_time": "2021-10-18T07:33:16.167921Z"
388 | }
389 | },
390 | "outputs": [
391 | {
392 | "data": {
393 | "text/plain": [
394 | "3"
395 | ]
396 | },
397 | "execution_count": 10,
398 | "metadata": {},
399 | "output_type": "execute_result"
400 | }
401 | ],
402 | "source": [
403 | "times3.__closure__[0].cell_contents"
404 | ]
405 | },
406 | {
407 | "cell_type": "code",
408 | "execution_count": 11,
409 | "metadata": {
410 | "ExecuteTime": {
411 | "end_time": "2021-10-18T07:33:16.789013Z",
412 | "start_time": "2021-10-18T07:33:16.767533Z"
413 | }
414 | },
415 | "outputs": [
416 | {
417 | "data": {
418 | "text/plain": [
419 | "5"
420 | ]
421 | },
422 | "execution_count": 11,
423 | "metadata": {},
424 | "output_type": "execute_result"
425 | }
426 | ],
427 | "source": [
428 | "times5.__closure__[0].cell_contents"
429 | ]
430 | },
431 | {
432 | "cell_type": "code",
433 | "execution_count": null,
434 | "metadata": {},
435 | "outputs": [],
436 | "source": []
437 | },
438 | {
439 | "cell_type": "code",
440 | "execution_count": null,
441 | "metadata": {},
442 | "outputs": [],
443 | "source": []
444 | }
445 | ],
446 | "metadata": {
447 | "hide_input": false,
448 | "kernelspec": {
449 | "display_name": "Python 3 (ipykernel)",
450 | "language": "python",
451 | "name": "python3"
452 | },
453 | "language_info": {
454 | "codemirror_mode": {
455 | "name": "ipython",
456 | "version": 3
457 | },
458 | "file_extension": ".py",
459 | "mimetype": "text/x-python",
460 | "name": "python",
461 | "nbconvert_exporter": "python",
462 | "pygments_lexer": "ipython3",
463 | "version": "3.9.13"
464 | },
465 | "toc": {
466 | "base_numbering": 1,
467 | "nav_menu": {},
468 | "number_sections": true,
469 | "sideBar": true,
470 | "skip_h1_title": false,
471 | "title_cell": "Table of Contents",
472 | "title_sidebar": "Contents",
473 | "toc_cell": false,
474 | "toc_position": {},
475 | "toc_section_display": true,
476 | "toc_window_display": false
477 | },
478 | "varInspector": {
479 | "cols": {
480 | "lenName": 16,
481 | "lenType": 16,
482 | "lenVar": 40
483 | },
484 | "kernels_config": {
485 | "python": {
486 | "delete_cmd_postfix": "",
487 | "delete_cmd_prefix": "del ",
488 | "library": "var_list.py",
489 | "varRefreshCmd": "print(var_dic_list())"
490 | },
491 | "r": {
492 | "delete_cmd_postfix": ") ",
493 | "delete_cmd_prefix": "rm(",
494 | "library": "var_list.r",
495 | "varRefreshCmd": "cat(var_dic_list()) "
496 | }
497 | },
498 | "types_to_exclude": [
499 | "module",
500 | "function",
501 | "builtin_function_or_method",
502 | "instance",
503 | "_Feature"
504 | ],
505 | "window_display": false
506 | }
507 | },
508 | "nbformat": 4,
509 | "nbformat_minor": 4
510 | }
511 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 60-60-60-Python-Challenge
2 |
3 | Dear Friends the channel "Demystify with Ajay"
4 | Here's the full description in video of this repository
5 |
6 | https://www.youtube.com/channel/UCxXr3E2YkqLXri2sDJ3v-5Q
7 |
--------------------------------------------------------------------------------
/countries_data.py:
--------------------------------------------------------------------------------
1 | countries = [
2 | 'Afghanistan',
3 | 'Albania',
4 | 'Algeria',
5 | 'Andorra',
6 | 'Angola',
7 | 'Antigua and Barbuda',
8 | 'Argentina',
9 | 'Armenia',
10 | 'Australia',
11 | 'Austria',
12 | 'Azerbaijan',
13 | 'Bahamas',
14 | 'Bahrain',
15 | 'Bangladesh',
16 | 'Barbados',
17 | 'Belarus',
18 | 'Belgium',
19 | 'Belize',
20 | 'Benin',
21 | 'Bhutan',
22 | 'Bolivia',
23 | 'Bosnia and Herzegovina',
24 | 'Botswana',
25 | 'Brazil',
26 | 'Brunei',
27 | 'Bulgaria',
28 | 'Burkina Faso',
29 | 'Burundi',
30 | 'Cambodia',
31 | 'Cameroon',
32 | 'Canada',
33 | 'Cape Verde',
34 | 'Central African Republic',
35 | 'Chad',
36 | 'Chile',
37 | 'China',
38 | 'Colombi',
39 | 'Comoros',
40 | 'Congo (Brazzaville)',
41 | 'Congo',
42 | 'Costa Rica',
43 | "Cote d'Ivoire",
44 | 'Croatia',
45 | 'Cuba',
46 | 'Cyprus',
47 | 'Czech Republic',
48 | 'Denmark',
49 | 'Djibouti',
50 | 'Dominica',
51 | 'Dominican Republic',
52 | 'East Timor (Timor Timur)',
53 | 'Ecuador',
54 | 'Egypt',
55 | 'El Salvador',
56 | 'Equatorial Guinea',
57 | 'Eritrea',
58 | 'Estonia',
59 | 'Ethiopia',
60 | 'Fiji',
61 | 'Finland',
62 | 'France',
63 | 'Gabon',
64 | 'Gambia, The',
65 | 'Georgia',
66 | 'Germany',
67 | 'Ghana',
68 | 'Greece',
69 | 'Grenada',
70 | 'Guatemala',
71 | 'Guinea',
72 | 'Guinea-Bissau',
73 | 'Guyana',
74 | 'Haiti',
75 | 'Honduras',
76 | 'Hungary',
77 | 'Iceland',
78 | 'India',
79 | 'Indonesia',
80 | 'Iran',
81 | 'Iraq',
82 | 'Ireland',
83 | 'Israel',
84 | 'Italy',
85 | 'Jamaica',
86 | 'Japan',
87 | 'Jordan',
88 | 'Kazakhstan',
89 | 'Kenya',
90 | 'Kiribati',
91 | 'Korea, North',
92 | 'Korea, South',
93 | 'Kuwait',
94 | 'Kyrgyzstan',
95 | 'Laos',
96 | 'Latvia',
97 | 'Lebanon',
98 | 'Lesotho',
99 | 'Liberia',
100 | 'Libya',
101 | 'Liechtenstein',
102 | 'Lithuania',
103 | 'Luxembourg',
104 | 'Macedonia',
105 | 'Madagascar',
106 | 'Malawi',
107 | 'Malaysia',
108 | 'Maldives',
109 | 'Mali',
110 | 'Malta',
111 | 'Marshall Islands',
112 | 'Mauritania',
113 | 'Mauritius',
114 | 'Mexico',
115 | 'Micronesia',
116 | 'Moldova',
117 | 'Monaco',
118 | 'Mongolia',
119 | 'Morocco',
120 | 'Mozambique',
121 | 'Myanmar',
122 | 'Namibia',
123 | 'Nauru',
124 | 'Nepal',
125 | 'Netherlands',
126 | 'New Zealand',
127 | 'Nicaragua',
128 | 'Niger',
129 | 'Nigeria',
130 | 'Norway',
131 | 'Oman',
132 | 'Pakistan',
133 | 'Palau',
134 | 'Panama',
135 | 'Papua New Guinea',
136 | 'Paraguay',
137 | 'Peru',
138 | 'Philippines',
139 | 'Poland',
140 | 'Portugal',
141 | 'Qatar',
142 | 'Romania',
143 | 'Russia',
144 | 'Rwanda',
145 | 'Saint Kitts and Nevis',
146 | 'Saint Lucia',
147 | 'Saint Vincent',
148 | 'Samoa',
149 | 'San Marino',
150 | 'Sao Tome and Principe',
151 | 'Saudi Arabia',
152 | 'Senegal',
153 | 'Serbia and Montenegro',
154 | 'Seychelles',
155 | 'Sierra Leone',
156 | 'Singapore',
157 | 'Slovakia',
158 | 'Slovenia',
159 | 'Solomon Islands',
160 | 'Somalia',
161 | 'South Africa',
162 | 'Spain',
163 | 'Sri Lanka',
164 | 'Sudan',
165 | 'Suriname',
166 | 'Swaziland',
167 | 'Sweden',
168 | 'Switzerland',
169 | 'Syria',
170 | 'Taiwan',
171 | 'Tajikistan',
172 | 'Tanzania',
173 | 'Thailand',
174 | 'Togo',
175 | 'Tonga',
176 | 'Trinidad and Tobago',
177 | 'Tunisia',
178 | 'Turkey',
179 | 'Turkmenistan',
180 | 'Tuvalu',
181 | 'Uganda',
182 | 'Ukraine',
183 | 'United Arab Emirates',
184 | 'United Kingdom',
185 | 'United States',
186 | 'Uruguay',
187 | 'Uzbekistan',
188 | 'Vanuatu',
189 | 'Vatican City',
190 | 'Venezuela',
191 | 'Vietnam',
192 | 'Yemen',
193 | 'Zambia',
194 | 'Zimbabwe',
195 | ]
--------------------------------------------------------------------------------
/img/b1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/b1.png
--------------------------------------------------------------------------------
/img/b2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/b2.png
--------------------------------------------------------------------------------
/img/b4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/b4.png
--------------------------------------------------------------------------------
/img/b5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/b5.png
--------------------------------------------------------------------------------
/img/con1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/con1.png
--------------------------------------------------------------------------------
/img/con2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/con2.png
--------------------------------------------------------------------------------
/img/con3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/con3.png
--------------------------------------------------------------------------------
/img/con4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/con4.png
--------------------------------------------------------------------------------
/img/ctrl1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/ctrl1.png
--------------------------------------------------------------------------------
/img/d0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/d0.png
--------------------------------------------------------------------------------
/img/dnld_rep.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/dnld_rep.png
--------------------------------------------------------------------------------
/img/eif0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/eif0.png
--------------------------------------------------------------------------------
/img/eif1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/eif1.png
--------------------------------------------------------------------------------
/img/eif2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/eif2.png
--------------------------------------------------------------------------------
/img/exlc.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/exlc.png
--------------------------------------------------------------------------------
/img/f1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/f1.png
--------------------------------------------------------------------------------
/img/f2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/f2.png
--------------------------------------------------------------------------------
/img/fcs.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/fcs.png
--------------------------------------------------------------------------------
/img/for0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/for0.png
--------------------------------------------------------------------------------
/img/for1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/for1.png
--------------------------------------------------------------------------------
/img/for10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/for10.png
--------------------------------------------------------------------------------
/img/for2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/for2.png
--------------------------------------------------------------------------------
/img/for3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/for3.png
--------------------------------------------------------------------------------
/img/for4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/for4.png
--------------------------------------------------------------------------------
/img/for5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/for5.png
--------------------------------------------------------------------------------
/img/for6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/for6.png
--------------------------------------------------------------------------------
/img/for7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/for7.png
--------------------------------------------------------------------------------
/img/for8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/for8.png
--------------------------------------------------------------------------------
/img/for9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/for9.png
--------------------------------------------------------------------------------
/img/forrange.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/forrange.png
--------------------------------------------------------------------------------
/img/if1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/if1.png
--------------------------------------------------------------------------------
/img/if2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/if2.png
--------------------------------------------------------------------------------
/img/if2_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/if2_1.png
--------------------------------------------------------------------------------
/img/if2_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/if2_2.png
--------------------------------------------------------------------------------
/img/if3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/if3.png
--------------------------------------------------------------------------------
/img/if4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/if4.png
--------------------------------------------------------------------------------
/img/ife0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/ife0.png
--------------------------------------------------------------------------------
/img/ife1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/ife1.png
--------------------------------------------------------------------------------
/img/ife2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/ife2.png
--------------------------------------------------------------------------------
/img/ife3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/ife3.png
--------------------------------------------------------------------------------
/img/l0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/l0.png
--------------------------------------------------------------------------------
/img/l1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/l1.png
--------------------------------------------------------------------------------
/img/l14_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/l14_1.png
--------------------------------------------------------------------------------
/img/l6_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/l6_1.png
--------------------------------------------------------------------------------
/img/l6_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/l6_2.png
--------------------------------------------------------------------------------
/img/loop1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/loop1.png
--------------------------------------------------------------------------------
/img/nd0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/nd0.png
--------------------------------------------------------------------------------
/img/nforloop1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/nforloop1.png
--------------------------------------------------------------------------------
/img/nforloop2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/nforloop2.png
--------------------------------------------------------------------------------
/img/nif0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/nif0.png
--------------------------------------------------------------------------------
/img/nif1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/nif1.png
--------------------------------------------------------------------------------
/img/nif2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/nif2.png
--------------------------------------------------------------------------------
/img/nif3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/nif3.png
--------------------------------------------------------------------------------
/img/pa1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/pa1.png
--------------------------------------------------------------------------------
/img/pa2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/pa2.png
--------------------------------------------------------------------------------
/img/s0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/s0.png
--------------------------------------------------------------------------------
/img/s16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/s16.png
--------------------------------------------------------------------------------
/img/s3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/s3.png
--------------------------------------------------------------------------------
/img/set1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set1.png
--------------------------------------------------------------------------------
/img/set10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set10.png
--------------------------------------------------------------------------------
/img/set11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set11.png
--------------------------------------------------------------------------------
/img/set12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set12.png
--------------------------------------------------------------------------------
/img/set2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set2.png
--------------------------------------------------------------------------------
/img/set3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set3.png
--------------------------------------------------------------------------------
/img/set4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set4.png
--------------------------------------------------------------------------------
/img/set5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set5.png
--------------------------------------------------------------------------------
/img/set6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set6.png
--------------------------------------------------------------------------------
/img/set7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set7.png
--------------------------------------------------------------------------------
/img/set8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set8.png
--------------------------------------------------------------------------------
/img/set9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/set9.png
--------------------------------------------------------------------------------
/img/t0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/t0.png
--------------------------------------------------------------------------------
/img/t5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/t5.png
--------------------------------------------------------------------------------
/img/t6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/t6.png
--------------------------------------------------------------------------------
/img/wh0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/wh0.png
--------------------------------------------------------------------------------
/img/wh1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/wh1.png
--------------------------------------------------------------------------------
/img/wh2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/wh2.png
--------------------------------------------------------------------------------
/img/wh3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/wh3.png
--------------------------------------------------------------------------------
/img/wh4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/wh4.png
--------------------------------------------------------------------------------
/img/wh5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ajanthadevi2012/60-60-60-Python-Challenge/f4929c07575cdc97c777a3ad579b7335e596949a/img/wh5.png
--------------------------------------------------------------------------------
|