├── .gitattributes
├── KVDB.zip
├── LICENSE
├── coding-challange-01-solution.ipynb
├── coding-challange-01.ipynb
├── coding-challange-02-solution.ipynb
├── coding-challange-02.ipynb
├── coding-challange-03-solution.ipynb
├── coding-challange-03.ipynb
├── coding-challange-04-solution.ipynb
├── coding-challange-04.ipynb
├── coding-challange-05-solution.ipynb
├── coding-challange-05.ipynb
├── coding-challange-06-solution.ipynb
├── coding-challange-06.ipynb
├── coding-challenge-04-question-06.zip
├── coding-challenge-05-question-08.zip
├── practice-exercise-01-solution.ipynb
├── practice-exercise-01.ipynb
├── practice-exercise-02-solution.ipynb
├── practice-exercise-02.ipynb
├── practice-exercise-03-solution.ipynb
├── practice-exercise-03.ipynb
├── practice-exercise-04-solution.ipynb
├── practice-exercise-04.ipynb
├── practice-exercise-05-solution.ipynb
├── practice-exercise-05.ipynb
├── practice-exercise-06-solution.ipynb
├── practice-exercise-06.ipynb
├── practice-exercise-07-solution.ipynb
├── practice-exercise-07.ipynb
├── practice-exercise-08-question-02.zip
├── practice-exercise-08-question-05.zip
├── practice-exercise-08-solution.ipynb
└── practice-exercise-08.ipynb
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/KVDB.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LearningJournal/Python-Foundation-Course/9a99503550ae62ace918c80ea9aa52b9ba51ae17/KVDB.zip
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Prashant Kumar Pandey
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/coding-challange-01-solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Coding Challange - 01 - Solution"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Print the following tuple in reverse order.\n",
17 | "\n",
18 | "tuple_1 = (22, 65, 12, 54)\n",
19 | "\n",
20 | "#### Expected Output:\n",
21 | "\n",
22 | "(54, 12, 65, 22)"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": 1,
28 | "metadata": {},
29 | "outputs": [
30 | {
31 | "name": "stdout",
32 | "output_type": "stream",
33 | "text": [
34 | "(54, 12, 65, 22)\n"
35 | ]
36 | }
37 | ],
38 | "source": [
39 | "tuple_1 = (22, 65, 12, 54)\n",
40 | "\n",
41 | "print(tuple_1[::-1])"
42 | ]
43 | },
44 | {
45 | "cell_type": "markdown",
46 | "metadata": {},
47 | "source": [
48 | "### Question 2:\n",
49 | "\n",
50 | "Access the name 'David' from the list given below.\n",
51 | "\n",
52 | "myList = [(2, 14, 'David'), [2, 7], 'Shaun']\n",
53 | "\n",
54 | "##### Expected Output:\n",
55 | "\n",
56 | "'David'"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": 2,
62 | "metadata": {},
63 | "outputs": [
64 | {
65 | "data": {
66 | "text/plain": [
67 | "'David'"
68 | ]
69 | },
70 | "execution_count": 2,
71 | "metadata": {},
72 | "output_type": "execute_result"
73 | }
74 | ],
75 | "source": [
76 | "myList = [(2, 14, 'David'), [2, 7], 'Shaun']\n",
77 | "\n",
78 | "myList[0][2]"
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "### Question 3:\n",
86 | "\n",
87 | "We have a set and a dictionary given below. Print the set once again after adding the keys of the dictionary to the set. The order of the expected output may vary as it is a set.\n",
88 | "\n",
89 | "mySet = {2, 4, 6}\n",
90 | "\n",
91 | "myDict = {'A':'John', 'B':'Emma', 'C':'Sam'}\n",
92 | "\n",
93 | "##### Expected Output:\n",
94 | "\n",
95 | "{2, 4, 6, 'A', 'C', 'B'}"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": 3,
101 | "metadata": {},
102 | "outputs": [
103 | {
104 | "name": "stdout",
105 | "output_type": "stream",
106 | "text": [
107 | "[(2, 14, 'David'), [2, 7], 'Shaun']\n"
108 | ]
109 | }
110 | ],
111 | "source": [
112 | "mySet = {2, 4, 6}\n",
113 | "myDict = {'A':'John', 'B':'Emma', 'C':'Sam'}\n",
114 | "\n",
115 | "mySet.update(myDict.keys())\n",
116 | "print(myList)"
117 | ]
118 | },
119 | {
120 | "cell_type": "markdown",
121 | "metadata": {},
122 | "source": [
123 | "### Question 4:\n",
124 | "We have two sets given below. Print all the items from these sets after removing the duplicates.\n",
125 | "\n",
126 | "set_1 = {2, 4, 6, 8}\n",
127 | "
set_2 = {6, 8, 10, 12}\n",
128 | "\n",
129 | "#### Expected Output:\n",
130 | "\n",
131 | "{2, 4, 6, 8, 10, 12}"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": 4,
137 | "metadata": {},
138 | "outputs": [
139 | {
140 | "name": "stdout",
141 | "output_type": "stream",
142 | "text": [
143 | "{2, 4, 6, 8, 10, 12}\n"
144 | ]
145 | }
146 | ],
147 | "source": [
148 | "set_1 = {2, 4, 6, 8}\n",
149 | "set_2 = {6, 8, 10, 12}\n",
150 | "\n",
151 | "print(set_1.union(set_2))"
152 | ]
153 | },
154 | {
155 | "cell_type": "markdown",
156 | "metadata": {},
157 | "source": [
158 | "### Question 5:\n",
159 | "Write a Python program to print the first and the last second of the day.\n",
160 | "\n",
161 | "#### Expected Output:\n",
162 | "\n",
163 | "First Second: 00:00:00\n",
164 | "
Last Second: 23:59:59.999999"
165 | ]
166 | },
167 | {
168 | "cell_type": "code",
169 | "execution_count": 5,
170 | "metadata": {},
171 | "outputs": [
172 | {
173 | "name": "stdout",
174 | "output_type": "stream",
175 | "text": [
176 | "First Second: 00:00:00\n",
177 | "Last Second: 23:59:59.999999\n"
178 | ]
179 | }
180 | ],
181 | "source": [
182 | "import datetime\n",
183 | "\n",
184 | "print(\"First Second: \", datetime.time.min)\n",
185 | "print(\"Last Second: \", datetime.time.max)"
186 | ]
187 | },
188 | {
189 | "cell_type": "markdown",
190 | "metadata": {},
191 | "source": [
192 | "### Question 6:\n",
193 | "\n",
194 | "Print the total number of digits in the number given below.\n",
195 | "\n",
196 | "num = 8842\n",
197 | "\n",
198 | "#### Expected Output:\n",
199 | "\n",
200 | "Total digits are: 4"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": 6,
206 | "metadata": {},
207 | "outputs": [
208 | {
209 | "name": "stdout",
210 | "output_type": "stream",
211 | "text": [
212 | "Total digits are: 4\n"
213 | ]
214 | }
215 | ],
216 | "source": [
217 | "num = 8842\n",
218 | "\n",
219 | "count = 0\n",
220 | "while num != 0:\n",
221 | " num //= 10\n",
222 | " count += 1\n",
223 | "\n",
224 | "print(\"Total digits are: \", count)"
225 | ]
226 | },
227 | {
228 | "cell_type": "markdown",
229 | "metadata": {},
230 | "source": [
231 | "### Question 7:\n",
232 | "\n",
233 | "We have a list of lists that contains several numbers. I want you to print the list whose sum of elements is the highest and also the lowest.\n",
234 | "\n",
235 | "num_list = [[2, 8, 11], [4, 5, 7, 12], [8, 9, 10, 11], [19, 13, 7], [2, 5, 16]]\n",
236 | "\n",
237 | "#### Expected Output:\n",
238 | "\n",
239 | "The list with the maximum sum of elements: [19, 13, 7]\n",
240 | "
The list with the minimum sum of elements: [2, 8, 11]"
241 | ]
242 | },
243 | {
244 | "cell_type": "code",
245 | "execution_count": 7,
246 | "metadata": {},
247 | "outputs": [
248 | {
249 | "name": "stdout",
250 | "output_type": "stream",
251 | "text": [
252 | "The list with maximum sum of elements: [19, 13, 7]\n",
253 | "The list with minimum sum of elements: [2, 8, 11]\n"
254 | ]
255 | }
256 | ],
257 | "source": [
258 | "num_list = [[2, 8, 11], [4, 5, 7, 12], [8, 9, 10, 11], [19, 13, 7], [2, 5, 16]]\n",
259 | "\n",
260 | "print(\"The list with maximum sum of elements: \", max(num_list, key = sum))\n",
261 | "print(\"The list with minimum sum of elements: \", min(num_list, key = sum)) "
262 | ]
263 | },
264 | {
265 | "cell_type": "markdown",
266 | "metadata": {},
267 | "source": [
268 | "#### Explanation: \n",
269 | "Some functions such as max(), min() can take a python lambda, which is applied to each element before the max(), or min() operation. This is further explained in the upcoming lecture on Python Lambda."
270 | ]
271 | },
272 | {
273 | "cell_type": "markdown",
274 | "metadata": {},
275 | "source": [
276 | "### Question 8:\n",
277 | "\n",
278 | "We have a string that contains the names of different fruits. I want you to convert this string into multiple substrings where each substring includes one fruit's name.\n",
279 | "\n",
280 | "fruits_string = \" Apple, Banana, Mango, Kiwi, Guava, Grapes, Pomegranate, Orange, Watermelon\"\n",
281 | "\n",
282 | "#### Expected Output:\n",
283 | "\n",
284 | " Apple\n",
285 | "
Banana\n",
286 | "
Mango\n",
287 | "
Kiwi\n",
288 | "
Guava\n",
289 | "
Grapes\n",
290 | "
Pomegranate\n",
291 | "
Orange\n",
292 | "
Watermelon"
293 | ]
294 | },
295 | {
296 | "cell_type": "code",
297 | "execution_count": 8,
298 | "metadata": {},
299 | "outputs": [
300 | {
301 | "name": "stdout",
302 | "output_type": "stream",
303 | "text": [
304 | " Apple\n",
305 | " Banana\n",
306 | " Mango\n",
307 | " Kiwi\n",
308 | " Guava\n",
309 | " Grapes\n",
310 | " Pomegranate\n",
311 | " Orange\n",
312 | " Watermelon\n"
313 | ]
314 | }
315 | ],
316 | "source": [
317 | "fruits_string = \" Apple, Banana, Mango, Kiwi, Guava, Grapes, Pomegranate, Orange, Watermelon\"\n",
318 | "\n",
319 | "substring = fruits_string.split(',')\n",
320 | "\n",
321 | "for fruits in substring:\n",
322 | " print(fruits)"
323 | ]
324 | },
325 | {
326 | "cell_type": "markdown",
327 | "metadata": {},
328 | "source": [
329 | "### Question 9:\n",
330 | "\n",
331 | "Write a program to reverse words in a string.\n",
332 | "\n",
333 | "sample_text = \"Python is a high-level and general-purpose programming language\"\n",
334 | "\n",
335 | "#### Expected Output:\n",
336 | "\n",
337 | "language programming general-purpose and high-level a is Python"
338 | ]
339 | },
340 | {
341 | "cell_type": "code",
342 | "execution_count": 9,
343 | "metadata": {},
344 | "outputs": [
345 | {
346 | "name": "stdout",
347 | "output_type": "stream",
348 | "text": [
349 | "language programming general-purpose and high-level a is Python\n"
350 | ]
351 | }
352 | ],
353 | "source": [
354 | "sample_text = \"Python is a high-level and general-purpose programming language\"\n",
355 | "\n",
356 | "print(' '.join(sample_text.split()[::-1]))"
357 | ]
358 | },
359 | {
360 | "cell_type": "markdown",
361 | "metadata": {},
362 | "source": [
363 | "### Question 10:\n",
364 | "\n",
365 | "Given below is the height (in cm) of the top 10 students in a class. Print the heights of the top 3 students from the given list.\n",
366 | "\n",
367 | "heights = [177, 160, 171, 163, 168, 175, 176, 183, 162, 170]\n",
368 | "\n",
369 | "#### Expected Output:\n",
370 | "\n",
371 | "Top Three Heights: [183, 177, 176]"
372 | ]
373 | },
374 | {
375 | "cell_type": "code",
376 | "execution_count": 10,
377 | "metadata": {},
378 | "outputs": [
379 | {
380 | "name": "stdout",
381 | "output_type": "stream",
382 | "text": [
383 | "Top Three Heights: [183, 177, 176]\n"
384 | ]
385 | }
386 | ],
387 | "source": [
388 | "heights = [177, 160, 171, 163, 168, 175, 176, 183, 162, 170]\n",
389 | "\n",
390 | "top_three = sorted(heights)\n",
391 | "\n",
392 | "print('Top Three Heights: ', top_three[:-4:-1])"
393 | ]
394 | }
395 | ],
396 | "metadata": {
397 | "kernelspec": {
398 | "display_name": "Python 3",
399 | "language": "python",
400 | "name": "python3"
401 | },
402 | "language_info": {
403 | "codemirror_mode": {
404 | "name": "ipython",
405 | "version": 3
406 | },
407 | "file_extension": ".py",
408 | "mimetype": "text/x-python",
409 | "name": "python",
410 | "nbconvert_exporter": "python",
411 | "pygments_lexer": "ipython3",
412 | "version": "3.8.3"
413 | }
414 | },
415 | "nbformat": 4,
416 | "nbformat_minor": 4
417 | }
418 |
--------------------------------------------------------------------------------
/coding-challange-01.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Coding Challange - 01"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Print the following tuple in reverse order.\n",
17 | "\n",
18 | "tuple_1 = (22, 65, 12, 54)\n",
19 | "\n",
20 | "#### Expected Output:\n",
21 | "\n",
22 | "(54, 12, 65, 22)"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": null,
28 | "metadata": {},
29 | "outputs": [],
30 | "source": []
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "### Question 2:\n",
37 | "\n",
38 | "Access the name 'David' from the list given below.\n",
39 | "\n",
40 | "myList = [(2, 14, 'David'), [2, 7], 'Shaun']\n",
41 | "\n",
42 | "##### Expected Output:\n",
43 | "\n",
44 | "'David'"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": null,
50 | "metadata": {},
51 | "outputs": [],
52 | "source": []
53 | },
54 | {
55 | "cell_type": "markdown",
56 | "metadata": {},
57 | "source": [
58 | "### Question 3:\n",
59 | "\n",
60 | "We have a set and a dictionary given below. Print the set once again after adding the keys of the dictionary to the set. The order of the expected output may vary as it is a set.\n",
61 | "\n",
62 | "mySet = {2, 4, 6}\n",
63 | "\n",
64 | "myDict = {'A':'John', 'B':'Emma', 'C':'Sam'}\n",
65 | "\n",
66 | "##### Expected Output:\n",
67 | "\n",
68 | "{2, 4, 6, 'A', 'C', 'B'}"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": null,
74 | "metadata": {},
75 | "outputs": [],
76 | "source": []
77 | },
78 | {
79 | "cell_type": "markdown",
80 | "metadata": {},
81 | "source": [
82 | "### Question 4:\n",
83 | "We have two sets given below. Print all the items from these sets after removing the duplicates.\n",
84 | "\n",
85 | "set_1 = {2, 4, 6, 8}\n",
86 | "
set_2 = {6, 8, 10, 12}\n",
87 | "\n",
88 | "#### Expected Output:\n",
89 | "\n",
90 | "{2, 4, 6, 8, 10, 12}"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": null,
96 | "metadata": {},
97 | "outputs": [],
98 | "source": []
99 | },
100 | {
101 | "cell_type": "markdown",
102 | "metadata": {},
103 | "source": [
104 | "### Question 5:\n",
105 | "Write a Python program to print the first and the last second of the day.\n",
106 | "\n",
107 | "#### Expected Output:\n",
108 | "\n",
109 | "First Second: 00:00:00\n",
110 | "
Last Second: 23:59:59.999999"
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": null,
116 | "metadata": {},
117 | "outputs": [],
118 | "source": []
119 | },
120 | {
121 | "cell_type": "markdown",
122 | "metadata": {},
123 | "source": [
124 | "### Question 6:\n",
125 | "\n",
126 | "Print the total number of digits in the number given below.\n",
127 | "\n",
128 | "num = 8842\n",
129 | "\n",
130 | "#### Expected Output:\n",
131 | "\n",
132 | "Total digits are: 4"
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "execution_count": null,
138 | "metadata": {},
139 | "outputs": [],
140 | "source": []
141 | },
142 | {
143 | "cell_type": "markdown",
144 | "metadata": {},
145 | "source": [
146 | "### Question 7:\n",
147 | "\n",
148 | "We have a list of lists that contains several numbers. I want you to print the list whose sum of elements is the highest and also the lowest.\n",
149 | "\n",
150 | "num_list = [[2, 8, 11], [4, 5, 7, 12], [8, 9, 10, 11], [19, 13, 7], [2, 5, 16]]\n",
151 | "\n",
152 | "#### Expected Output:\n",
153 | "\n",
154 | "The list with the maximum sum of elements: [19, 13, 7]\n",
155 | "
The list with the minimum sum of elements: [2, 8, 11]"
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": null,
161 | "metadata": {},
162 | "outputs": [],
163 | "source": []
164 | },
165 | {
166 | "cell_type": "markdown",
167 | "metadata": {},
168 | "source": [
169 | "### Question 8:\n",
170 | "\n",
171 | "We have a string that contains the names of different fruits. I want you to convert this string into multiple substrings where each substring includes one fruit's name.\n",
172 | "\n",
173 | "fruits_string = \" Apple, Banana, Mango, Kiwi, Guava, Grapes, Pomegranate, Orange, Watermelon\"\n",
174 | "\n",
175 | "#### Expected Output:\n",
176 | "\n",
177 | " Apple\n",
178 | "
Banana\n",
179 | "
Mango\n",
180 | "
Kiwi\n",
181 | "
Guava\n",
182 | "
Grapes\n",
183 | "
Pomegranate\n",
184 | "
Orange\n",
185 | "
Watermelon"
186 | ]
187 | },
188 | {
189 | "cell_type": "code",
190 | "execution_count": null,
191 | "metadata": {},
192 | "outputs": [],
193 | "source": []
194 | },
195 | {
196 | "cell_type": "markdown",
197 | "metadata": {},
198 | "source": [
199 | "### Question 9:\n",
200 | "\n",
201 | "Write a program to reverse words in a string.\n",
202 | "\n",
203 | "sample_text = \"Python is a high-level and general-purpose programming language\"\n",
204 | "\n",
205 | "#### Expected Output:\n",
206 | "\n",
207 | "language programming general-purpose and high-level a is Python"
208 | ]
209 | },
210 | {
211 | "cell_type": "code",
212 | "execution_count": null,
213 | "metadata": {},
214 | "outputs": [],
215 | "source": []
216 | },
217 | {
218 | "cell_type": "markdown",
219 | "metadata": {},
220 | "source": [
221 | "### Question 10:\n",
222 | "\n",
223 | "Given below is the height (in cm) of the top 10 students in a class. Print the heights of the top 3 students from the given list.\n",
224 | "\n",
225 | "heights = [177, 160, 171, 163, 168, 175, 176, 183, 162, 170]\n",
226 | "\n",
227 | "#### Expected Output:\n",
228 | "\n",
229 | "Top Three Heights: [183, 177, 176]"
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "execution_count": null,
235 | "metadata": {},
236 | "outputs": [],
237 | "source": []
238 | }
239 | ],
240 | "metadata": {
241 | "kernelspec": {
242 | "display_name": "Python 3",
243 | "language": "python",
244 | "name": "python3"
245 | },
246 | "language_info": {
247 | "codemirror_mode": {
248 | "name": "ipython",
249 | "version": 3
250 | },
251 | "file_extension": ".py",
252 | "mimetype": "text/x-python",
253 | "name": "python",
254 | "nbconvert_exporter": "python",
255 | "pygments_lexer": "ipython3",
256 | "version": "3.8.3"
257 | }
258 | },
259 | "nbformat": 4,
260 | "nbformat_minor": 4
261 | }
262 |
--------------------------------------------------------------------------------
/coding-challange-02-solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Coding Challange - 02 - Solution"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "We have two lists of numbers given below. I want you to create a third list by picking an odd-indexed element from the first list and even-indexed elements from the second list.\n",
17 | "\n",
18 | "list_1 = [5, 10, 15, 20, 25, 30, 35]\n",
19 | "
list_2 = [7, 14, 21, 28, 35, 42, 49]\n",
20 | "\n",
21 | "#### Expected Output:\n",
22 | "\n",
23 | "[10, 20, 30, 7, 21, 35, 49]"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": 1,
29 | "metadata": {},
30 | "outputs": [
31 | {
32 | "name": "stdout",
33 | "output_type": "stream",
34 | "text": [
35 | "[10, 20, 30, 7, 21, 35, 49]\n"
36 | ]
37 | }
38 | ],
39 | "source": [
40 | "list_1 = [5, 10, 15, 20, 25, 30, 35]\n",
41 | "list_2 = [7, 14, 21, 28, 35, 42, 49]\n",
42 | "\n",
43 | "list_3 = []\n",
44 | "\n",
45 | "odd_index = list_1[1::2]\n",
46 | "even_index = list_2[0::2]\n",
47 | "\n",
48 | "list_3.extend(odd_index)\n",
49 | "list_3.extend(even_index)\n",
50 | "\n",
51 | "print(list_3)"
52 | ]
53 | },
54 | {
55 | "cell_type": "markdown",
56 | "metadata": {},
57 | "source": [
58 | "### Question 2:\n",
59 | "\n",
60 | "You have two dictionaries, with each of them containing several letters associated with certain values. \n",
61 | "\n",
62 | "num_1 = {'a' : 5,\n",
63 | " 's' : 7,\n",
64 | " 'x' : 11,\n",
65 | " 'm' : 12,\n",
66 | " 'o' : 8}\n",
67 | "\n",
68 | "num_2 = {'r' : 12,\n",
69 | " 'x' : 9,\n",
70 | " 'n' : 8,\n",
71 | " 'm' : 12,\n",
72 | " 'q' : 10}\n",
73 | "\n",
74 | "I want you to perform the below-mentioned operations on these dictionaries:\n",
75 | "\n",
76 | "\t1. Print out the letters which are common to both these dictionaries.\n",
77 | "\t2. Print out the (key, value) pair, which is common to both these dictionaries.\n",
78 | "\t3. Print out all the letters which have occurred only once in these dictionaries.\n",
79 | "\t4. Print out the letters in num_1 that are not present in num_2.\n",
80 | "\t5. Print out a new dictionary num_3, which contains unique letters of num_1 from the previous output with their \n",
81 | " associated values.\n",
82 | "\n",
83 | "#### Expected Output:\n",
84 | "\n",
85 | "\t1. {'x', 'm'}\n",
86 | " 2. {('m', 12)}\n",
87 | " 3. {'o', 'a', 'n', 'q', 'r', 's'}\n",
88 | " 4. {'o', 'a', 's'}\n",
89 | " 5. {'o': 8, 'a': 5, 's': 7}"
90 | ]
91 | },
92 | {
93 | "cell_type": "code",
94 | "execution_count": 2,
95 | "metadata": {},
96 | "outputs": [
97 | {
98 | "name": "stdout",
99 | "output_type": "stream",
100 | "text": [
101 | "{'x', 'm'}\n",
102 | "{('m', 12)}\n",
103 | "{'o', 'a', 'n', 'q', 'r', 's'}\n",
104 | "{'o', 'a', 's'}\n",
105 | "{'o': 8, 'a': 5, 's': 7}\n"
106 | ]
107 | }
108 | ],
109 | "source": [
110 | "num_1 = {'a' : 5,\n",
111 | " 's' : 7,\n",
112 | " 'x' : 11,\n",
113 | " 'm' : 12,\n",
114 | " 'o' : 8}\n",
115 | "\n",
116 | "num_2 = {'r' : 12,\n",
117 | " 'x' : 9,\n",
118 | " 'n' : 8,\n",
119 | " 'm' : 12,\n",
120 | " 'q' : 10}\n",
121 | "\n",
122 | "print(num_1.keys() & num_2.keys())\n",
123 | "\n",
124 | "print(num_1.items() & num_2.items())\n",
125 | "\n",
126 | "print(num_1.keys() ^ num_2.keys())\n",
127 | "\n",
128 | "print(num_1.keys() - num_2.keys())\n",
129 | "\n",
130 | "num_3 = {key:num_1[key] for key in num_1.keys() - (num_1.keys() & num_2.keys())}\n",
131 | "print(num_3)"
132 | ]
133 | },
134 | {
135 | "cell_type": "markdown",
136 | "metadata": {},
137 | "source": [
138 | "### Question 3:\n",
139 | "\n",
140 | "Find out the number of letters and digits from the given alpha-numeric text.\n",
141 | "\n",
142 | "sample_text = \"Learning Journal 2020\"\n",
143 | "\n",
144 | "#### Expected Output:\n",
145 | "\n",
146 | "Number of Letters: 15\n",
147 | "
Number of Digits: 4"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": 3,
153 | "metadata": {},
154 | "outputs": [
155 | {
156 | "name": "stdout",
157 | "output_type": "stream",
158 | "text": [
159 | "Number of Letters: 15\n",
160 | "Number of Digits: 4\n"
161 | ]
162 | }
163 | ],
164 | "source": [
165 | "sample_text = \"Learning Journal 2020\"\n",
166 | "\n",
167 | "digits = letters = 0\n",
168 | "\n",
169 | "for i in sample_text:\n",
170 | " if i.isdigit():\n",
171 | " digits = digits + 1\n",
172 | " elif i.isalpha():\n",
173 | " letters = letters + 1\n",
174 | " else:\n",
175 | " pass\n",
176 | " \n",
177 | "print(\"Number of Letters: \", letters)\n",
178 | "print(\"Number of Digits: \", digits)"
179 | ]
180 | },
181 | {
182 | "cell_type": "markdown",
183 | "metadata": {},
184 | "source": [
185 | "### Question 4:\n",
186 | "\n",
187 | "We have a list given below. Add an item 200 to the list, after the item 100.\n",
188 | "\n",
189 | "myList = [2, 8, [10, 20, [40, 60, [90, 100], 30, 70], 80], 50]\n",
190 | "\n",
191 | "#### Expected Output:\n",
192 | "\n",
193 | "[2, 8, [10, 20, [40, 60, [90, 100, 200], 30, 70], 80], 50]"
194 | ]
195 | },
196 | {
197 | "cell_type": "code",
198 | "execution_count": 4,
199 | "metadata": {},
200 | "outputs": [
201 | {
202 | "name": "stdout",
203 | "output_type": "stream",
204 | "text": [
205 | "[2, 8, [10, 20, [40, 60, [90, 100, 200], 30, 70], 80], 50]\n"
206 | ]
207 | }
208 | ],
209 | "source": [
210 | "myList = [2, 8, [10, 20, [40, 60, [90, 100], 30, 70], 80], 50]\n",
211 | "\n",
212 | "myList[2][2][2].append(200)\n",
213 | "print(myList)"
214 | ]
215 | },
216 | {
217 | "cell_type": "markdown",
218 | "metadata": {},
219 | "source": [
220 | "### Question 5:\n",
221 | "\n",
222 | "We have two sets given below. Print the first set again after removing the elements common to both of these sets.\n",
223 | "\n",
224 | "set_1 = {2, 8, 19, 13, 24, 55, 48, 93}\n",
225 | "
set_2 = {7, 11, 55, 84, 8, 65, 73, 13}\n",
226 | "\n",
227 | "#### Expected Output:\n",
228 | "\n",
229 | "{2, 48, 19, 24, 93}"
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "execution_count": 5,
235 | "metadata": {},
236 | "outputs": [
237 | {
238 | "name": "stdout",
239 | "output_type": "stream",
240 | "text": [
241 | "{2, 48, 19, 24, 93}\n"
242 | ]
243 | }
244 | ],
245 | "source": [
246 | "set_1 = {2, 8, 19, 13, 24, 55, 48, 93}\n",
247 | "set_2 = {7, 11, 55, 84, 8, 65, 73, 13}\n",
248 | "\n",
249 | "common_elements = set_1 & set_2\n",
250 | "\n",
251 | "for item in common_elements:\n",
252 | " set_1.remove(item)\n",
253 | "\n",
254 | "print(set_1)"
255 | ]
256 | },
257 | {
258 | "cell_type": "markdown",
259 | "metadata": {},
260 | "source": [
261 | "### Question 6:\n",
262 | "\n",
263 | "Print all the prime numbers between the given range.\n",
264 | "\n",
265 | "start = 20\n",
266 | "
end = 60\n",
267 | "\n",
268 | "#### Expected Output:\n",
269 | "\n",
270 | "Prime numbers between 20 and 60 are: \n",
271 | "23\n",
272 | "
29\n",
273 | "
31\n",
274 | "
37\n",
275 | "
41\n",
276 | "
43\n",
277 | "
47\n",
278 | "
53\n",
279 | "
59"
280 | ]
281 | },
282 | {
283 | "cell_type": "code",
284 | "execution_count": 6,
285 | "metadata": {},
286 | "outputs": [
287 | {
288 | "name": "stdout",
289 | "output_type": "stream",
290 | "text": [
291 | "Prime numbers between 20 and 60 are: \n",
292 | "23\n",
293 | "29\n",
294 | "31\n",
295 | "37\n",
296 | "41\n",
297 | "43\n",
298 | "47\n",
299 | "53\n",
300 | "59\n"
301 | ]
302 | }
303 | ],
304 | "source": [
305 | "start = 20\n",
306 | "end = 60\n",
307 | "\n",
308 | "print(f\"Prime numbers between {start} and {end} are: \")\n",
309 | "\n",
310 | "for num in range(start, end + 1):\n",
311 | " \n",
312 | " for i in range(2, num):\n",
313 | " \n",
314 | " if (num % i) == 0: \n",
315 | " break\n",
316 | " else:\n",
317 | " print(num)"
318 | ]
319 | },
320 | {
321 | "cell_type": "markdown",
322 | "metadata": {},
323 | "source": [
324 | "### Question 7:\n",
325 | "\n",
326 | "Here we have a list of 3 letters. I want you to concatenate this list with another list of numbers whose range varies from 1 to 3 (3 is included).\n",
327 | "\n",
328 | "letters_list = ['H', 'R', 'S']\n",
329 | "\n",
330 | "#### Expected Output:\n",
331 | "\n",
332 | "['H1', 'R1', 'S1', 'H2', 'R2', 'S2', 'H3', 'R3', 'S3']"
333 | ]
334 | },
335 | {
336 | "cell_type": "code",
337 | "execution_count": 7,
338 | "metadata": {},
339 | "outputs": [
340 | {
341 | "name": "stdout",
342 | "output_type": "stream",
343 | "text": [
344 | "['H1', 'R1', 'S1', 'H2', 'R2', 'S2', 'H3', 'R3', 'S3']\n"
345 | ]
346 | }
347 | ],
348 | "source": [
349 | "letters_list = ['H', 'R', 'S']\n",
350 | "\n",
351 | "new_list = [f'{x}{y}' for y in range(1, 4) for x in letters_list]\n",
352 | "print(new_list)"
353 | ]
354 | },
355 | {
356 | "cell_type": "markdown",
357 | "metadata": {},
358 | "source": [
359 | "### Question 8:\n",
360 | "\n",
361 | "Print all the numbers between 1 and 100 (both being included) that are multiples of 3 and 5 both.\n",
362 | "\n",
363 | "#### Expected Output:\n",
364 | "\n",
365 | "Multiples of 3 and 5: [15, 30, 45, 60, 75, 90]"
366 | ]
367 | },
368 | {
369 | "cell_type": "code",
370 | "execution_count": 8,
371 | "metadata": {},
372 | "outputs": [
373 | {
374 | "name": "stdout",
375 | "output_type": "stream",
376 | "text": [
377 | "Multiples of 3 and 5: [15, 30, 45, 60, 75, 90]\n"
378 | ]
379 | }
380 | ],
381 | "source": [
382 | "multiple_of_3_and_5 = []\n",
383 | "\n",
384 | "for i in range(1, 101):\n",
385 | " if (i % 5 == 0) & (i % 3 == 0):\n",
386 | " multiple_of_3_and_5.append(i) \n",
387 | " else:\n",
388 | " continue\n",
389 | "\n",
390 | "print('Multiples of 3 and 5:', multiple_of_3_and_5)"
391 | ]
392 | },
393 | {
394 | "cell_type": "markdown",
395 | "metadata": {},
396 | "source": [
397 | "### Question 9:\n",
398 | "\n",
399 | "Here we have a poem by Walt Whitman. I want you to print this poem once again after removing all the vowels from them.\n",
400 | "\n",
401 | "poem = \"Centre of equal daughters, equal sons,\\\n",
402 | "All, all alike endeared, grown, ungrown, young or old,\\\n",
403 | "Strong, ample, fair, enduring, capable, rich,\\\n",
404 | "Perennial with the Earth, with Freedom, Law and Love,\\\n",
405 | "A grand, sane, towering, seated Mother,\\\n",
406 | "Chaired in the adamant of Time.\"\n",
407 | "\n",
408 | "#### Expected Output:\n",
409 | "\n",
410 | "'Cntr f ql dghtrs, ql sns,All, ll lk ndr’d, grwn, ngrwn, yng r ld,Strng, mpl, fr, ndrng, cpbl, rch,Prnnl wth th Erth, wth Frdm, Lw nd Lv,A grnd, sn, twrng, std Mthr,Chr’d n th dmnt f Tm.'"
411 | ]
412 | },
413 | {
414 | "cell_type": "code",
415 | "execution_count": 9,
416 | "metadata": {},
417 | "outputs": [
418 | {
419 | "data": {
420 | "text/plain": [
421 | "'Cntr f ql dghtrs, ql sns,All, ll lk ndrd, grwn, ngrwn, yng r ld,Strng, mpl, fr, ndrng, cpbl, rch,Prnnl wth th Erth, wth Frdm, Lw nd Lv,A grnd, sn, twrng, std Mthr,Chrd n th dmnt f Tm.'"
422 | ]
423 | },
424 | "execution_count": 9,
425 | "metadata": {},
426 | "output_type": "execute_result"
427 | }
428 | ],
429 | "source": [
430 | "poem = \"Centre of equal daughters, equal sons,\\\n",
431 | "All, all alike endeared, grown, ungrown, young or old,\\\n",
432 | "Strong, ample, fair, enduring, capable, rich,\\\n",
433 | "Perennial with the Earth, with Freedom, Law and Love,\\\n",
434 | "A grand, sane, towering, seated Mother,\\\n",
435 | "Chaired in the adamant of Time.\"\n",
436 | "\n",
437 | "vowels = ('a', 'e', 'i', 'o', 'u')\n",
438 | "''.join([remove_vowels for remove_vowels in poem if remove_vowels not in vowels])"
439 | ]
440 | },
441 | {
442 | "cell_type": "markdown",
443 | "metadata": {},
444 | "source": [
445 | "### Question 10:\n",
446 | "\n",
447 | "Here we have some information about Python programming extracted from Wikipedia. Find all the occurrences of the word 'python' in the given python_info, ignoring the case.\n",
448 | "\n",
449 | "python_info = \"Python is an interpreted, high-level, and general-purpose programming language.\\\n",
450 | "Python is dynamically typed and garbage-collected.\\\n",
451 | "Python was created in the late 1980s as a successor to the ABC language, Python 2.0.\\\n",
452 | "Python 3.0 was released in 2008.\\\n",
453 | "A non-profit organization, the Python Software Foundation, manages and directs resources for Python development.\"\n",
454 | "\n",
455 | "#### Expected Output:\n",
456 | "\n",
457 | "The 'Python' count in python_info is: 7"
458 | ]
459 | },
460 | {
461 | "cell_type": "code",
462 | "execution_count": 10,
463 | "metadata": {},
464 | "outputs": [
465 | {
466 | "name": "stdout",
467 | "output_type": "stream",
468 | "text": [
469 | "The Python count in python_info is: 7\n"
470 | ]
471 | }
472 | ],
473 | "source": [
474 | "python_info = \"Python is an interpreted, high-level, and general-purpose programming language.\\\n",
475 | "Python is dynamically typed and garbage-collected.\\\n",
476 | "Python was created in the late 1980s as a successor to the ABC language, Python 2.0.\\\n",
477 | "Python 3.0 was released in 2008.\\\n",
478 | "A non-profit organization, the Python Software Foundation, manages and directs resources for Python development.\"\n",
479 | "\n",
480 | "substring = \"Python\"\n",
481 | "tempString = python_info.lower()\n",
482 | "count = tempString.count(substring.lower())\n",
483 | "print(\"The Python count in python_info is:\", count)"
484 | ]
485 | }
486 | ],
487 | "metadata": {
488 | "kernelspec": {
489 | "display_name": "Python 3",
490 | "language": "python",
491 | "name": "python3"
492 | },
493 | "language_info": {
494 | "codemirror_mode": {
495 | "name": "ipython",
496 | "version": 3
497 | },
498 | "file_extension": ".py",
499 | "mimetype": "text/x-python",
500 | "name": "python",
501 | "nbconvert_exporter": "python",
502 | "pygments_lexer": "ipython3",
503 | "version": "3.8.3"
504 | }
505 | },
506 | "nbformat": 4,
507 | "nbformat_minor": 4
508 | }
509 |
--------------------------------------------------------------------------------
/coding-challange-02.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Coding Challange - 02"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "We have two lists of numbers given below. I want you to create a third list by picking an odd-indexed element from the first list and even-indexed elements from the second list.\n",
17 | "\n",
18 | "list_1 = [5, 10, 15, 20, 25, 30, 35]\n",
19 | "
list_2 = [7, 14, 21, 28, 35, 42, 49]\n",
20 | "\n",
21 | "#### Expected Output:\n",
22 | "\n",
23 | "[10, 20, 30, 7, 21, 35, 49]"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": []
32 | },
33 | {
34 | "cell_type": "markdown",
35 | "metadata": {},
36 | "source": [
37 | "### Question 2:\n",
38 | "\n",
39 | "You have two dictionaries, with each of them containing several letters associated with certain values. \n",
40 | "\n",
41 | "num_1 = {'a' : 5,\n",
42 | " 's' : 7,\n",
43 | " 'x' : 11,\n",
44 | " 'm' : 12,\n",
45 | " 'o' : 8}\n",
46 | "\n",
47 | "num_2 = {'r' : 12,\n",
48 | " 'x' : 9,\n",
49 | " 'n' : 8,\n",
50 | " 'm' : 12,\n",
51 | " 'q' : 10}\n",
52 | "\n",
53 | "I want you to perform the below-mentioned operations on these dictionaries:\n",
54 | "\n",
55 | "\t1. Print out the letters which are common to both these dictionaries.\n",
56 | "\t2. Print out the (key, value) pair, which is common to both these dictionaries.\n",
57 | "\t3. Print out all the letters which have occurred only once in these dictionaries.\n",
58 | "\t4. Print out the letters in num_1 that are not present in num_2.\n",
59 | "\t5. Print out a new dictionary num_3, which contains unique letters of num_1 from the previous output with their \n",
60 | " associated values.\n",
61 | "\n",
62 | "#### Expected Output:\n",
63 | "\n",
64 | "\t1. {'x', 'm'}\n",
65 | " 2. {('m', 12)}\n",
66 | " 3. {'o', 'a', 'n', 'q', 'r', 's'}\n",
67 | " 4. {'o', 'a', 's'}\n",
68 | " 5. {'o': 8, 'a': 5, 's': 7}"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": null,
74 | "metadata": {},
75 | "outputs": [],
76 | "source": []
77 | },
78 | {
79 | "cell_type": "markdown",
80 | "metadata": {},
81 | "source": [
82 | "### Question 3:\n",
83 | "\n",
84 | "Find out the number of letters and digits from the given alpha-numeric text.\n",
85 | "\n",
86 | "sample_text = \"Learning Journal 2020\"\n",
87 | "\n",
88 | "#### Expected Output:\n",
89 | "\n",
90 | "Number of Letters: 15\n",
91 | "
Number of Digits: 4"
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "execution_count": null,
97 | "metadata": {},
98 | "outputs": [],
99 | "source": []
100 | },
101 | {
102 | "cell_type": "markdown",
103 | "metadata": {},
104 | "source": [
105 | "### Question 4:\n",
106 | "\n",
107 | "We have a list given below. Add an item 200 to the list, after the item 100.\n",
108 | "\n",
109 | "myList = [2, 8, [10, 20, [40, 60, [90, 100], 30, 70], 80], 50]\n",
110 | "\n",
111 | "#### Expected Output:\n",
112 | "\n",
113 | "[2, 8, [10, 20, [40, 60, [90, 100, 200], 30, 70], 80], 50]"
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "execution_count": null,
119 | "metadata": {},
120 | "outputs": [],
121 | "source": []
122 | },
123 | {
124 | "cell_type": "markdown",
125 | "metadata": {},
126 | "source": [
127 | "### Question 5:\n",
128 | "\n",
129 | "We have two sets given below. Print the first set again after removing the elements common to both of these sets.\n",
130 | "\n",
131 | "set_1 = {2, 8, 19, 13, 24, 55, 48, 93}\n",
132 | "
set_2 = {7, 11, 55, 84, 8, 65, 73, 13}\n",
133 | "\n",
134 | "#### Expected Output:\n",
135 | "\n",
136 | "{2, 48, 19, 24, 93}"
137 | ]
138 | },
139 | {
140 | "cell_type": "code",
141 | "execution_count": null,
142 | "metadata": {},
143 | "outputs": [],
144 | "source": []
145 | },
146 | {
147 | "cell_type": "markdown",
148 | "metadata": {},
149 | "source": [
150 | "### Question 6:\n",
151 | "\n",
152 | "Print all the prime numbers between the given range.\n",
153 | "\n",
154 | "start = 20\n",
155 | "
end = 60\n",
156 | "\n",
157 | "#### Expected Output:\n",
158 | "\n",
159 | "Prime numbers between 20 and 60 are: \n",
160 | "23\n",
161 | "
29\n",
162 | "
31\n",
163 | "
37\n",
164 | "
41\n",
165 | "
43\n",
166 | "
47\n",
167 | "
53\n",
168 | "
59"
169 | ]
170 | },
171 | {
172 | "cell_type": "code",
173 | "execution_count": null,
174 | "metadata": {},
175 | "outputs": [],
176 | "source": []
177 | },
178 | {
179 | "cell_type": "markdown",
180 | "metadata": {},
181 | "source": [
182 | "### Question 7:\n",
183 | "\n",
184 | "Here we have a list of 3 letters. I want you to concatenate this list with another list of numbers whose range varies from 1 to 3 (3 is included).\n",
185 | "\n",
186 | "letters_list = ['H', 'R', 'S']\n",
187 | "\n",
188 | "#### Expected Output:\n",
189 | "\n",
190 | "['H1', 'R1', 'S1', 'H2', 'R2', 'S2', 'H3', 'R3', 'S3']"
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "execution_count": null,
196 | "metadata": {},
197 | "outputs": [],
198 | "source": []
199 | },
200 | {
201 | "cell_type": "markdown",
202 | "metadata": {},
203 | "source": [
204 | "### Question 8:\n",
205 | "\n",
206 | "Print all the numbers between 1 and 100 (both being included) that are multiples of 3 and 5 both.\n",
207 | "\n",
208 | "#### Expected Output:\n",
209 | "\n",
210 | "Multiples of 3 and 5: [15, 30, 45, 60, 75, 90]"
211 | ]
212 | },
213 | {
214 | "cell_type": "code",
215 | "execution_count": null,
216 | "metadata": {},
217 | "outputs": [],
218 | "source": []
219 | },
220 | {
221 | "cell_type": "markdown",
222 | "metadata": {},
223 | "source": [
224 | "### Question 9:\n",
225 | "\n",
226 | "Here we have a poem by Walt Whitman. I want you to print this poem once again after removing all the vowels from them.\n",
227 | "\n",
228 | "poem = \"Centre of equal daughters, equal sons,\\\n",
229 | "All, all alike endeared, grown, ungrown, young or old,\\\n",
230 | "Strong, ample, fair, enduring, capable, rich,\\\n",
231 | "Perennial with the Earth, with Freedom, Law and Love,\\\n",
232 | "A grand, sane, towering, seated Mother,\\\n",
233 | "Chaired in the adamant of Time.\"\n",
234 | "\n",
235 | "#### Expected Output:\n",
236 | "\n",
237 | "'Cntr f ql dghtrs, ql sns,All, ll lk ndr’d, grwn, ngrwn, yng r ld,Strng, mpl, fr, ndrng, cpbl, rch,Prnnl wth th Erth, wth Frdm, Lw nd Lv,A grnd, sn, twrng, std Mthr,Chr’d n th dmnt f Tm.'"
238 | ]
239 | },
240 | {
241 | "cell_type": "code",
242 | "execution_count": null,
243 | "metadata": {},
244 | "outputs": [],
245 | "source": []
246 | },
247 | {
248 | "cell_type": "markdown",
249 | "metadata": {},
250 | "source": [
251 | "### Question 10:\n",
252 | "\n",
253 | "Here we have some information about Python programming extracted from Wikipedia. Find all the occurrences of the word 'python' in the given python_info, ignoring the case.\n",
254 | "\n",
255 | "python_info = \"Python is an interpreted, high-level, and general-purpose programming language.\\\n",
256 | "Python is dynamically typed and garbage-collected.\\\n",
257 | "Python was created in the late 1980s as a successor to the ABC language, Python 2.0.\\\n",
258 | "Python 3.0 was released in 2008.\\\n",
259 | "A non-profit organization, the Python Software Foundation, manages and directs resources for Python development.\"\n",
260 | "\n",
261 | "#### Expected Output:\n",
262 | "\n",
263 | "The 'Python' count in python_info is: 7"
264 | ]
265 | },
266 | {
267 | "cell_type": "code",
268 | "execution_count": null,
269 | "metadata": {},
270 | "outputs": [],
271 | "source": []
272 | }
273 | ],
274 | "metadata": {
275 | "kernelspec": {
276 | "display_name": "Python 3",
277 | "language": "python",
278 | "name": "python3"
279 | },
280 | "language_info": {
281 | "codemirror_mode": {
282 | "name": "ipython",
283 | "version": 3
284 | },
285 | "file_extension": ".py",
286 | "mimetype": "text/x-python",
287 | "name": "python",
288 | "nbconvert_exporter": "python",
289 | "pygments_lexer": "ipython3",
290 | "version": "3.8.3"
291 | }
292 | },
293 | "nbformat": 4,
294 | "nbformat_minor": 4
295 | }
296 |
--------------------------------------------------------------------------------
/coding-challange-03.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Coding Challange - 03"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "We have two lists given below. I want you to print the first list in the original order and the second list in reverse order simultaneously.\n",
17 | "\n",
18 | "list_1 = [12, 25, 31, 20, 18]\n",
19 | "
list_2 = [11, 9, 43, 22, 55]\n",
20 | "\n",
21 | "#### Expected Output:\n",
22 | "\n",
23 | "12 55\n",
24 | "
25 22\n",
25 | "
31 43\n",
26 | "
20 9\n",
27 | "
18 11"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": []
36 | },
37 | {
38 | "cell_type": "markdown",
39 | "metadata": {},
40 | "source": [
41 | "### Question 2:\n",
42 | "\n",
43 | "Print today's date in the format given below.\n",
44 | "\n",
45 | "Day_name Day_number Month_name Year\n",
46 | "\n",
47 | "#### Expected Output:\n",
48 | "\n",
49 | "Today's date is: Tuesday 13 October 2020"
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": null,
55 | "metadata": {},
56 | "outputs": [],
57 | "source": []
58 | },
59 | {
60 | "cell_type": "markdown",
61 | "metadata": {},
62 | "source": [
63 | "### Question 3:\n",
64 | "\n",
65 | "We have a string which contains grocery items. Print these items in a comma-separated sequence after sorting them alphabetically.\n",
66 | "\n",
67 | "grocery_items = ' Grated Cheese, Coffee Powder, Pickles\\\n",
68 | " White Chocolate, Dark Chocolate, Eggs, Breads, Milk,\\\n",
69 | " Sugar, Salt, Cat Food, Fries'\n",
70 | "\n",
71 | "#### Expected Output:\n",
72 | "\n",
73 | "Breads, Cat Food, Coffee Powder, Dark Chocolate, Eggs, Fries, Grated Cheese, Milk, Pickles White Chocolate, Salt, Sugar"
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "execution_count": null,
79 | "metadata": {},
80 | "outputs": [],
81 | "source": []
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "metadata": {},
86 | "source": [
87 | "### Question 4:\n",
88 | "\n",
89 | "Write a Python program to print the dates of yesterday, today, and tomorrow.\n",
90 | "\n",
91 | "#### Expected Output:\n",
92 | "\n",
93 | "Yesterday : 2020-10-12\n",
94 | "
Today : 2020-10-13\n",
95 | "
Tomorrow : 2020-10-14"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": null,
101 | "metadata": {},
102 | "outputs": [],
103 | "source": []
104 | },
105 | {
106 | "cell_type": "markdown",
107 | "metadata": {},
108 | "source": [
109 | "### Question 5:\n",
110 | "\n",
111 | "We have a set that contains roll numbers of candidates who have applied for an event. Out of the applied candidates, few have submitted their application forms, and we have registered their details in a dictionary given below. Print the roll numbers of the candidates who have submitted their application forms and also the ones who are yet to submit it.\n",
112 | "\n",
113 | "roll_numbers = {12, 7, 15, 23, 32, 30}\n",
114 | "
student_details = {12:'Judy', 30:'Shane', 23:'Aaron'}\n",
115 | "\n",
116 | "#### Expected Output:\n",
117 | "\n",
118 | "Completed Applications: {12, 30, 23}\n",
119 | "
Pending Applications: {32, 15, 7}"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": null,
125 | "metadata": {},
126 | "outputs": [],
127 | "source": []
128 | },
129 | {
130 | "cell_type": "markdown",
131 | "metadata": {},
132 | "source": [
133 | "### Question 6:\n",
134 | "\n",
135 | "We have the time in seconds given below. Convert this time into days, hours, minutes, and seconds.\n",
136 | "\n",
137 | "time = 996452\n",
138 | "\n",
139 | "#### Expected Output:\n",
140 | "\n",
141 | "Days : Hours : Minutes : Seconds -> 11 : 12 : 47 : 32"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": null,
147 | "metadata": {},
148 | "outputs": [],
149 | "source": []
150 | },
151 | {
152 | "cell_type": "markdown",
153 | "metadata": {},
154 | "source": [
155 | "### Question 7:\n",
156 | "\n",
157 | "We have a list of companies with their face values in the stock market. Now, I want to get the company's name along with its face value for the company, which has the maximum and the minimum face values. I also want you all to sort this list based on face values in increasing order. In this example, you have the flexibility to go ahead and google for some hints.\n",
158 | "\n",
159 | "stock_market = {'AXIS BANK' : 7,\n",
160 | "
'BHARTI AIRTEL' : 5,\n",
161 | "
'COAL INDIA' : 10,\n",
162 | "
'ITC' : 1,\n",
163 | "
'TCS' : 3,\n",
164 | "
'L&T' : 2,\n",
165 | "
'RELIANCE' : 9,\n",
166 | "
'KOTAK BANK' : 8,\n",
167 | "
'AMERICAN EXPRESS' : 11}\n",
168 | "\n",
169 | "#### Expected Output:\n",
170 | "\n",
171 | "(1, 'ITC')\n",
172 | "
(11, 'AMERICAN EXPRESS')\n",
173 | "
[(1, 'ITC'), (2, 'L&T'), (3, 'TCS'), (5, 'BHARTI AIRTEL'), (7, 'AXIS BANK'), (8, 'KOTAK BANK'), (9, 'RELIANCE'), (10, 'COAL INDIA'), (11, 'AMERICAN EXPRESS')]"
174 | ]
175 | },
176 | {
177 | "cell_type": "code",
178 | "execution_count": null,
179 | "metadata": {},
180 | "outputs": [],
181 | "source": []
182 | },
183 | {
184 | "cell_type": "markdown",
185 | "metadata": {},
186 | "source": [
187 | "### Question 8:\n",
188 | "\n",
189 | "Write a program to print all the numbers between 100 to 300 (both included) such that each digit of the given number is an even number.\n",
190 | "\n",
191 | "#### Expected Output:\n",
192 | "\n",
193 | "200, 202, 204, 206, 208, 220, 222, 224, 226, 228, 240, 242, 244, 246, 248, 260, 262, 264, 266, 268, 280, 282, 284, 286, 288"
194 | ]
195 | },
196 | {
197 | "cell_type": "code",
198 | "execution_count": null,
199 | "metadata": {},
200 | "outputs": [],
201 | "source": []
202 | },
203 | {
204 | "cell_type": "markdown",
205 | "metadata": {},
206 | "source": [
207 | "### Question 9:\n",
208 | "\n",
209 | "We have a list of several names along with their age. Now, there is a meeting where seats are reserved for senior citizens (age - 45 above). I want to filter out the senior citizens from this list and get a resultant list where we the names of all those who satisfy the criteria.\n",
210 | "\n",
211 | "passenger_list = {'Ross' : 35,\n",
212 | "
'Thomas': 42,\n",
213 | "
'Rick' : 55,\n",
214 | "
'Ericson' : 51,\n",
215 | "
'Josh' : 45,\n",
216 | "
'Lara' : 50,\n",
217 | "
'Emma' : 38,\n",
218 | "
'Lily' : 46,\n",
219 | "
'Ron' : 41,\n",
220 | "
'Michael' : 47,\n",
221 | "
'Joanna' : 56,\n",
222 | "
'Arthur' : 42}\n",
223 | "\n",
224 | "#### Expected Output:\n",
225 | "\n",
226 | "{'Rick', 'Lily', 'Ericson', 'Joanna', 'Michael', 'Lara'}"
227 | ]
228 | },
229 | {
230 | "cell_type": "code",
231 | "execution_count": null,
232 | "metadata": {},
233 | "outputs": [],
234 | "source": []
235 | },
236 | {
237 | "cell_type": "markdown",
238 | "metadata": {},
239 | "source": [
240 | "### Question 10:\n",
241 | "\n",
242 | "We have a list given below, which contains several numbers. I want to slice this list based on its indexes, into 3 equal sub-lists.\n",
243 | "\n",
244 | "num_list = [31, 24, 2, 16, 19, 45, 75, 63, 79]\n",
245 | "\n",
246 | "#### Expected Output:\n",
247 | "\n",
248 | "Sub-List 1 [31, 24, 2]\n",
249 | "
Sub-List 2 [16, 19, 45]\n",
250 | "
Sub-List 3 [75, 63, 79]"
251 | ]
252 | },
253 | {
254 | "cell_type": "code",
255 | "execution_count": null,
256 | "metadata": {},
257 | "outputs": [],
258 | "source": []
259 | }
260 | ],
261 | "metadata": {
262 | "kernelspec": {
263 | "display_name": "Python 3",
264 | "language": "python",
265 | "name": "python3"
266 | },
267 | "language_info": {
268 | "codemirror_mode": {
269 | "name": "ipython",
270 | "version": 3
271 | },
272 | "file_extension": ".py",
273 | "mimetype": "text/x-python",
274 | "name": "python",
275 | "nbconvert_exporter": "python",
276 | "pygments_lexer": "ipython3",
277 | "version": "3.8.3"
278 | }
279 | },
280 | "nbformat": 4,
281 | "nbformat_minor": 4
282 | }
283 |
--------------------------------------------------------------------------------
/coding-challange-04-solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Coding Challange - 04 - Solution"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Write a python class Pet that is initialized with the name and species of the pet. It contains an instance method to display the info on the screen. Define two other classes Dog and Cat. Call the instance method of Pet class using the Dog and Cat objects.\n",
17 | "\n",
18 | "#### Expected Output:\n",
19 | "\n",
20 | "Name: Kitty\n",
21 | "
Species: Persian Cat\n",
22 | "\n",
23 | "Name: Bruno\n",
24 | "
Species: German Shepherd"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": 1,
30 | "metadata": {},
31 | "outputs": [
32 | {
33 | "name": "stdout",
34 | "output_type": "stream",
35 | "text": [
36 | "Name: Kitty\n",
37 | "Species: Persian Cat\n",
38 | "\n",
39 | "Name: Bruno\n",
40 | "Species: German Shepherd\n",
41 | "\n"
42 | ]
43 | }
44 | ],
45 | "source": [
46 | "class Pet: \n",
47 | " def __init__(self, name, species):\n",
48 | " self.name = name\n",
49 | " self.species = species\n",
50 | "\n",
51 | " def getName(self):\n",
52 | " return self.name\n",
53 | "\n",
54 | " def getSpecies(self):\n",
55 | " return self.species\n",
56 | "\n",
57 | " def show_info(self):\n",
58 | " print(f\"Name: {self.name}\\nSpecies: {self.species}\\n\")\n",
59 | "\n",
60 | "class Dog(Pet):\n",
61 | " pass\n",
62 | "\n",
63 | "class Cat(Pet):\n",
64 | " pass\n",
65 | "\n",
66 | "dog1 = Dog('Bruno', 'German Shepherd')\n",
67 | "cat1 = Cat('Kitty', 'Persian Cat')\n",
68 | "\n",
69 | "cat1.show_info()\n",
70 | "dog1.show_info()"
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "### Question 2:\n",
78 | "\n",
79 | "Write a python function pattern to print a pattern using '0' such that its base and height contain an equal number of '0.'\n",
80 | "\n",
81 | "#### Sample Input:\n",
82 | "\n",
83 | "5\n",
84 | "\n",
85 | "#### Expected Output:\n",
86 | "\n",
87 | " 0\n",
88 | "
00\n",
89 | "
000\n",
90 | "
0000\n",
91 | "
00000"
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "execution_count": 2,
97 | "metadata": {},
98 | "outputs": [
99 | {
100 | "name": "stdout",
101 | "output_type": "stream",
102 | "text": [
103 | "0 \n",
104 | "00 \n",
105 | "000 \n",
106 | "0000 \n",
107 | "00000\n"
108 | ]
109 | }
110 | ],
111 | "source": [
112 | "def pattern(x):\n",
113 | " \n",
114 | " for i in range(1, x + 1):\n",
115 | " print('0' * i + ' ' * (x - i) )\n",
116 | " \n",
117 | "pattern(5)"
118 | ]
119 | },
120 | {
121 | "cell_type": "markdown",
122 | "metadata": {},
123 | "source": [
124 | "### Question 3:\n",
125 | "\n",
126 | "Create a class with two methods, the first one to get the input string from the user, and the second one print the odd indexed letters from the input string.\n",
127 | "\n",
128 | "#### Expected Output:\n",
129 | "\n",
130 | "Enter a string: Learning Journal\n",
131 | "
erigJunl"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": 3,
137 | "metadata": {},
138 | "outputs": [
139 | {
140 | "name": "stdout",
141 | "output_type": "stream",
142 | "text": [
143 | "Enter a string: Learning Journal\n",
144 | "erigJunl\n"
145 | ]
146 | }
147 | ],
148 | "source": [
149 | "class String:\n",
150 | " def getString(self):\n",
151 | " self.text = input('Enter a string: ')\n",
152 | "\n",
153 | " def printString(self):\n",
154 | " print(self.text[1::2])\n",
155 | "\n",
156 | "my_obj = String()\n",
157 | "my_obj.getString()\n",
158 | "my_obj.printString()"
159 | ]
160 | },
161 | {
162 | "cell_type": "markdown",
163 | "metadata": {},
164 | "source": [
165 | "### Question 4:\n",
166 | "\n",
167 | "Write a lambda function to find the palindromes from the list given below.\n",
168 | "\n",
169 | "words_list = [\"refer\", \"python\", \"level\", \"join\", \"notebook\", \"dad\"]\n",
170 | "\n",
171 | "#### Expected Output:\n",
172 | "\n",
173 | "Palindromes: ['refer', 'level', 'dad']"
174 | ]
175 | },
176 | {
177 | "cell_type": "code",
178 | "execution_count": 4,
179 | "metadata": {},
180 | "outputs": [
181 | {
182 | "name": "stdout",
183 | "output_type": "stream",
184 | "text": [
185 | "Palindromes: ['refer', 'level', 'dad']\n"
186 | ]
187 | }
188 | ],
189 | "source": [
190 | "words_list = [\"refer\", \"python\", \"level\", \"join\", \"notebook\", \"dad\"]\n",
191 | "palindrome_list = list(filter(lambda x: (x == \"\".join(reversed(x))), words_list)) \n",
192 | "print(\"Palindromes: \", palindrome_list)"
193 | ]
194 | },
195 | {
196 | "cell_type": "markdown",
197 | "metadata": {},
198 | "source": [
199 | "### Question 5:\n",
200 | "\n",
201 | "Write a python class Date with an instance method get_date to print the date. Define another class, Time, which again contains an instance method get_time to print the time given below. Call both the classes using the object of Time class.\n",
202 | "\n",
203 | "Time: 10:30:00\n",
204 | "
Date: 2020-02-25\n",
205 | "\n",
206 | "#### Expected Output:\n",
207 | "\n",
208 | "10:30:00\n",
209 | "
2020-02-25"
210 | ]
211 | },
212 | {
213 | "cell_type": "code",
214 | "execution_count": 5,
215 | "metadata": {},
216 | "outputs": [
217 | {
218 | "name": "stdout",
219 | "output_type": "stream",
220 | "text": [
221 | "10:30:00\n",
222 | "2020-02-25\n"
223 | ]
224 | }
225 | ],
226 | "source": [
227 | "class Date:\n",
228 | " def get_date(self):\n",
229 | " print('2020-02-25')\n",
230 | "\n",
231 | "\n",
232 | "class Time(Date):\n",
233 | " def get_time(self):\n",
234 | " print(\"10:30:00\")\n",
235 | " \n",
236 | "time_obj = Time()\n",
237 | "time_obj.get_time()\n",
238 | "time_obj.get_date()"
239 | ]
240 | },
241 | {
242 | "cell_type": "markdown",
243 | "metadata": {},
244 | "source": [
245 | "### Question 6:\n",
246 | "\n",
247 | "Create two modules student.py and merit.py. The student.py module should contain a Student class that is initialized with name and roll number. It should also contain an instance method getMarks, which contains an argument to get the number of subjects. The getMarks function should ask the user to enter the marks of the student. The merit.py module should contain an instance method to check if the student's average marks are above 90. If it is above 90, give him/her the merit. Otherwise, print 'not eligible.' Print the message along with the student's name and roll number.\n",
248 | "\n",
249 | "#### Expected Output:\n",
250 | "\n",
251 | "Enter the name of the Student: David\n",
252 | "
Enter the roll number of the Student: 23\n",
253 | "
Enter the number of Subjects: 3\n",
254 | "
Enter marks1: 96\n",
255 | "
Enter marks2: 85\n",
256 | "
Enter marks3: 82\n",
257 | "
David with Roll No: 23, is not eligible."
258 | ]
259 | },
260 | {
261 | "cell_type": "markdown",
262 | "metadata": {},
263 | "source": [
264 | "### Solution:\n",
265 | "\n",
266 | "You can find the solution to this question in the \"coding-challenge-04-question-06.zip\" folder. The folder consists of 2 files, namely student.py and merit.py."
267 | ]
268 | },
269 | {
270 | "cell_type": "markdown",
271 | "metadata": {},
272 | "source": [
273 | "### Question 7:\n",
274 | "\n",
275 | "We have a dictionary of OTT Platforms, and its index is given below. Take the subscription of all the even indexed OTT platforms from 1 to 10. Since our dictionary is not updated, we have only five platforms, so using a try-except block of code handle exceptions and print the message 'Key Out of Bound. Update the list.' However, print a message 'Take these subscriptions' for the platforms mentioned in the output.\n",
276 | "\n",
277 | "my_dict = {1 : 'HBO Max', 2 : 'Netflix', 3 : 'Prime Video', 4 : 'Hotstar + Disney', 5 : 'Hulu TV'}\n",
278 | "\n",
279 | "#### Expected Output:\n",
280 | "\n",
281 | "Netflix\n",
282 | "
Hotstar + Disney\n",
283 | "
Key Out of Bound. Update the list.\n",
284 | "
Take these subscriptions."
285 | ]
286 | },
287 | {
288 | "cell_type": "code",
289 | "execution_count": 6,
290 | "metadata": {},
291 | "outputs": [
292 | {
293 | "name": "stdout",
294 | "output_type": "stream",
295 | "text": [
296 | "Netflix\n",
297 | "Hotstar + Disney\n",
298 | "Key Out of Bound. Update the list.\n",
299 | "Take these subscriptions.\n"
300 | ]
301 | }
302 | ],
303 | "source": [
304 | "my_dict = {1 : 'HBO Max', 2 : 'Netflix', 3 : 'Prime Video', 4 : 'Hotstar + Disney', 5 : 'Hulu TV'}\n",
305 | "\n",
306 | "try:\n",
307 | " for i in range(1, 10):\n",
308 | " if i % 2 == 0:\n",
309 | " print(my_dict[i])\n",
310 | "except:\n",
311 | " print('Key Out of Bound. Update the list.')\n",
312 | "finally:\n",
313 | " print('Take these subscriptions.')"
314 | ]
315 | },
316 | {
317 | "cell_type": "markdown",
318 | "metadata": {},
319 | "source": [
320 | "### Question 8:\n",
321 | "\n",
322 | "Write a python function to add the digits of a positive integer repeatedly until you get the result as a single digit.\n",
323 | "\n",
324 | "#### Expected Output:\n",
325 | "\n",
326 | "Enter a number:98\n",
327 | "
8"
328 | ]
329 | },
330 | {
331 | "cell_type": "code",
332 | "execution_count": 7,
333 | "metadata": {},
334 | "outputs": [
335 | {
336 | "name": "stdout",
337 | "output_type": "stream",
338 | "text": [
339 | "Enter a number:98\n"
340 | ]
341 | },
342 | {
343 | "data": {
344 | "text/plain": [
345 | "8"
346 | ]
347 | },
348 | "execution_count": 7,
349 | "metadata": {},
350 | "output_type": "execute_result"
351 | }
352 | ],
353 | "source": [
354 | "def add_digits(x):\n",
355 | " if x > 0:\n",
356 | " return (x - 1) % 9 + 1 \n",
357 | " else:\n",
358 | " return 'Not a valid input'\n",
359 | "\n",
360 | "n = int(input('Enter a number:'))\n",
361 | "add_digits(n)"
362 | ]
363 | },
364 | {
365 | "cell_type": "markdown",
366 | "metadata": {},
367 | "source": [
368 | "### Question 9:\n",
369 | "\n",
370 | "Write a python program to print the factorial of a number.\n",
371 | "\n",
372 | "#### Expected Output:\n",
373 | "\n",
374 | "Enter a number: 5\n",
375 | "
120"
376 | ]
377 | },
378 | {
379 | "cell_type": "code",
380 | "execution_count": 8,
381 | "metadata": {},
382 | "outputs": [
383 | {
384 | "name": "stdout",
385 | "output_type": "stream",
386 | "text": [
387 | "Enter a number: 5\n"
388 | ]
389 | },
390 | {
391 | "data": {
392 | "text/plain": [
393 | "120"
394 | ]
395 | },
396 | "execution_count": 8,
397 | "metadata": {},
398 | "output_type": "execute_result"
399 | }
400 | ],
401 | "source": [
402 | "def factorial(n): \n",
403 | " \n",
404 | " if n == 1 or n == 0:\n",
405 | " return 1\n",
406 | " else:\n",
407 | " return n * factorial(n - 1)\n",
408 | "\n",
409 | "num = int(input('Enter a number: '))\n",
410 | "factorial(num)"
411 | ]
412 | },
413 | {
414 | "cell_type": "markdown",
415 | "metadata": {},
416 | "source": [
417 | "### Question 10:\n",
418 | "\n",
419 | "Write a python function to print the longest string between the two strings given below.\n",
420 | "\n",
421 | "first_str = 'Learning'\n",
422 | "
second_str = 'Journal'\n",
423 | "\n",
424 | "#### Expected Output:\n",
425 | "\n",
426 | "Learning"
427 | ]
428 | },
429 | {
430 | "cell_type": "code",
431 | "execution_count": 9,
432 | "metadata": {},
433 | "outputs": [
434 | {
435 | "name": "stdout",
436 | "output_type": "stream",
437 | "text": [
438 | "Learning\n"
439 | ]
440 | }
441 | ],
442 | "source": [
443 | "def longest_string(str1,str2):\n",
444 | " len1 = len(str1)\n",
445 | " len2 = len(str2)\n",
446 | " \n",
447 | " if len1 > len2:\n",
448 | " print(str1)\n",
449 | " elif len2 > len1:\n",
450 | " print(str2)\n",
451 | " else:\n",
452 | " print(str1)\n",
453 | " print(str2)\n",
454 | "\n",
455 | "first_str = 'Learning'\n",
456 | "second_str = 'Journal'\n",
457 | "\n",
458 | "longest_string(first_str, second_str)"
459 | ]
460 | }
461 | ],
462 | "metadata": {
463 | "kernelspec": {
464 | "display_name": "Python 3",
465 | "language": "python",
466 | "name": "python3"
467 | },
468 | "language_info": {
469 | "codemirror_mode": {
470 | "name": "ipython",
471 | "version": 3
472 | },
473 | "file_extension": ".py",
474 | "mimetype": "text/x-python",
475 | "name": "python",
476 | "nbconvert_exporter": "python",
477 | "pygments_lexer": "ipython3",
478 | "version": "3.8.3"
479 | }
480 | },
481 | "nbformat": 4,
482 | "nbformat_minor": 4
483 | }
484 |
--------------------------------------------------------------------------------
/coding-challange-04.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Coding Challange - 04"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Write a python class Pet that is initialized with the name and species of the pet. It contains an instance method to display the info on the screen. Define two other classes Dog and Cat. Call the instance method of Pet class using the Dog and Cat objects.\n",
17 | "\n",
18 | "#### Expected Output:\n",
19 | "\n",
20 | "Name: Kitty\n",
21 | "
Species: Persian Cat\n",
22 | "\n",
23 | "Name: Bruno\n",
24 | "
Species: German Shepherd"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": null,
30 | "metadata": {},
31 | "outputs": [],
32 | "source": []
33 | },
34 | {
35 | "cell_type": "markdown",
36 | "metadata": {},
37 | "source": [
38 | "### Question 2:\n",
39 | "\n",
40 | "Write a python function pattern to print a pattern using '0' such that its base and height contain an equal number of '0.'\n",
41 | "\n",
42 | "#### Sample Input:\n",
43 | "\n",
44 | "5\n",
45 | "\n",
46 | "#### Expected Output:\n",
47 | "\n",
48 | " 0\n",
49 | "
00\n",
50 | "
000\n",
51 | "
0000\n",
52 | "
00000"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": null,
58 | "metadata": {},
59 | "outputs": [],
60 | "source": []
61 | },
62 | {
63 | "cell_type": "markdown",
64 | "metadata": {},
65 | "source": [
66 | "### Question 3:\n",
67 | "\n",
68 | "Create a class with two methods, the first one to get the input string from the user, and the second one print the odd indexed letters from the input string.\n",
69 | "\n",
70 | "#### Expected Output:\n",
71 | "\n",
72 | "Enter a string: Learning Journal\n",
73 | "
erigJunl"
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "execution_count": null,
79 | "metadata": {},
80 | "outputs": [],
81 | "source": []
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "metadata": {},
86 | "source": [
87 | "### Question 4:\n",
88 | "\n",
89 | "Write a lambda function to find the palindromes from the list given below.\n",
90 | "\n",
91 | "words_list = [\"refer\", \"python\", \"level\", \"join\", \"notebook\", \"dad\"]\n",
92 | "\n",
93 | "#### Expected Output:\n",
94 | "\n",
95 | "Palindromes: ['refer', 'level', 'dad']"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": null,
101 | "metadata": {},
102 | "outputs": [],
103 | "source": []
104 | },
105 | {
106 | "cell_type": "markdown",
107 | "metadata": {},
108 | "source": [
109 | "### Question 5:\n",
110 | "\n",
111 | "Write a python class Date with an instance method get_date to print the date. Define another class, Time, which again contains an instance method get_time to print the time given below. Call both the classes using the object of Time class.\n",
112 | "\n",
113 | "Time: 10:30:00\n",
114 | "
Date: 2020-02-25\n",
115 | "\n",
116 | "#### Expected Output:\n",
117 | "\n",
118 | "10:30:00\n",
119 | "
2020-02-25"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": null,
125 | "metadata": {},
126 | "outputs": [],
127 | "source": []
128 | },
129 | {
130 | "cell_type": "markdown",
131 | "metadata": {},
132 | "source": [
133 | "### Question 6:\n",
134 | "\n",
135 | "Create two modules student.py and merit.py. The student.py module should contain a Student class that is initialized with name and roll number. It should also contain an instance method getMarks, which contains an argument to get the number of subjects. The getMarks function should ask the user to enter the marks of the student. The merit.py module should contain an instance method to check if the student's average marks are above 90. If it is above 90, give him/her the merit. Otherwise, print 'not eligible.' Print the message along with the student's name and roll number.\n",
136 | "\n",
137 | "#### Expected Output:\n",
138 | "\n",
139 | "Enter the name of the Student: David\n",
140 | "
Enter the roll number of the Student: 23\n",
141 | "
Enter the number of Subjects: 3\n",
142 | "
Enter marks1: 96\n",
143 | "
Enter marks2: 85\n",
144 | "
Enter marks3: 82\n",
145 | "
David with Roll No: 23, is not eligible."
146 | ]
147 | },
148 | {
149 | "cell_type": "code",
150 | "execution_count": null,
151 | "metadata": {},
152 | "outputs": [],
153 | "source": []
154 | },
155 | {
156 | "cell_type": "markdown",
157 | "metadata": {},
158 | "source": [
159 | "### Question 7:\n",
160 | "\n",
161 | "We have a dictionary of OTT Platforms, and its index is given below. Take the subscription of all the even indexed OTT platforms from 1 to 10. Since our dictionary is not updated, we have only five platforms, so using a try-except block of code handle exceptions and print the message 'Key Out of Bound. Update the list.' However, print a message 'Take these subscriptions' for the platforms mentioned in the output.\n",
162 | "\n",
163 | "my_dict = {1 : 'HBO Max', 2 : 'Netflix', 3 : 'Prime Video', 4 : 'Hotstar + Disney', 5 : 'Hulu TV'}\n",
164 | "\n",
165 | "#### Expected Output:\n",
166 | "\n",
167 | "Netflix\n",
168 | "
Hotstar + Disney\n",
169 | "
Key Out of Bound. Update the list.\n",
170 | "
Take these subscriptions."
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": null,
176 | "metadata": {},
177 | "outputs": [],
178 | "source": []
179 | },
180 | {
181 | "cell_type": "markdown",
182 | "metadata": {},
183 | "source": [
184 | "### Question 8:\n",
185 | "\n",
186 | "Write a python function to add the digits of a positive integer repeatedly until you get the result as a single digit.\n",
187 | "\n",
188 | "#### Expected Output:\n",
189 | "\n",
190 | "Enter a number:98\n",
191 | "
8"
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "execution_count": null,
197 | "metadata": {},
198 | "outputs": [],
199 | "source": []
200 | },
201 | {
202 | "cell_type": "markdown",
203 | "metadata": {},
204 | "source": [
205 | "### Question 9:\n",
206 | "\n",
207 | "Write a python program to print the factorial of a number.\n",
208 | "\n",
209 | "#### Expected Output:\n",
210 | "\n",
211 | "Enter a number: 5\n",
212 | "
120"
213 | ]
214 | },
215 | {
216 | "cell_type": "code",
217 | "execution_count": null,
218 | "metadata": {},
219 | "outputs": [],
220 | "source": []
221 | },
222 | {
223 | "cell_type": "markdown",
224 | "metadata": {},
225 | "source": [
226 | "### Question 10:\n",
227 | "\n",
228 | "Write a python function to print the longest string between the two strings given below.\n",
229 | "\n",
230 | "first_str = 'Learning'\n",
231 | "
second_str = 'Journal'\n",
232 | "\n",
233 | "#### Expected Output:\n",
234 | "\n",
235 | "Learning"
236 | ]
237 | },
238 | {
239 | "cell_type": "code",
240 | "execution_count": null,
241 | "metadata": {},
242 | "outputs": [],
243 | "source": []
244 | }
245 | ],
246 | "metadata": {
247 | "kernelspec": {
248 | "display_name": "Python 3",
249 | "language": "python",
250 | "name": "python3"
251 | },
252 | "language_info": {
253 | "codemirror_mode": {
254 | "name": "ipython",
255 | "version": 3
256 | },
257 | "file_extension": ".py",
258 | "mimetype": "text/x-python",
259 | "name": "python",
260 | "nbconvert_exporter": "python",
261 | "pygments_lexer": "ipython3",
262 | "version": "3.8.3"
263 | }
264 | },
265 | "nbformat": 4,
266 | "nbformat_minor": 4
267 | }
268 |
--------------------------------------------------------------------------------
/coding-challange-05.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Coding Challange - 05"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Define a python function to print the longest word in the list of technologies given below.\n",
17 | "\n",
18 | "myList = ['Java', 'Data Science', 'Python', 'Machine Learning', 'Artificial Intelligence']\n",
19 | "\n",
20 | "#### Expected Output:\n",
21 | "\n",
22 | "Artificial Intelligence"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": null,
28 | "metadata": {},
29 | "outputs": [],
30 | "source": []
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "### Question 2:\n",
37 | "\n",
38 | "Create a class Shape that is initialized with length. Then, define an instance method getArea that calculates the area of the Shape. Create another class, Square, which inherits the Shape class and overrides the getArea method to return the area of Square.\n",
39 | "\n",
40 | "#### Sample Input:\n",
41 | "\n",
42 | "Length of sqaure = 8\n",
43 | "\n",
44 | "#### Expected Output:\n",
45 | "\n",
46 | "64"
47 | ]
48 | },
49 | {
50 | "cell_type": "code",
51 | "execution_count": null,
52 | "metadata": {},
53 | "outputs": [],
54 | "source": []
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {},
59 | "source": [
60 | "### Question 3:\n",
61 | "\n",
62 | "Write a python function to print the next palindrome after the specified integer. (Hint: Use the sys module to get the largest integer for the range.)\n",
63 | "\n",
64 | "#### Sample Input:\n",
65 | "\n",
66 | "num = 90\n",
67 | "\n",
68 | "#### Expected Output:\n",
69 | "\n",
70 | "99"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": null,
76 | "metadata": {},
77 | "outputs": [],
78 | "source": []
79 | },
80 | {
81 | "cell_type": "markdown",
82 | "metadata": {},
83 | "source": [
84 | "### Question 4:\n",
85 | "\n",
86 | "Write a python function to check if the list of numbers given below is in Arithmetic Progression or not.\n",
87 | "\n",
88 | "myList = [5, 7, 9, 11]\n",
89 | "\n",
90 | "#### Expected Output:\n",
91 | "\n",
92 | "True"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "metadata": {},
99 | "outputs": [],
100 | "source": []
101 | },
102 | {
103 | "cell_type": "markdown",
104 | "metadata": {},
105 | "source": [
106 | "### Question 5:\n",
107 | "\n",
108 | "Write a python class vowelsCheck to find the number of vowels in a string. Take the string input from the user.\n",
109 | "\n",
110 | "#### Expected Output:\n",
111 | "\n",
112 | "Enter a string: Artificial Intelligence is the future\n",
113 | "
Number of vowels: 13"
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "execution_count": null,
119 | "metadata": {},
120 | "outputs": [],
121 | "source": []
122 | },
123 | {
124 | "cell_type": "markdown",
125 | "metadata": {},
126 | "source": [
127 | "### Question 6:\n",
128 | "\n",
129 | "Write a python exception handling program to check if the date of February entered is a multiple of 7 or not. Print the message 'Incorrect Date' if the date exceeds 28. For all other exceptions, print the message 'Invalid Data.' (Hint: User-defined exception)\n",
130 | "\n",
131 | "#### Sample Input - 1:\n",
132 | "\n",
133 | "Enter the date of February: 30\n",
134 | "\n",
135 | "#### Expected Output:\n",
136 | "\n",
137 | "Incorrect Date.\n",
138 | "\n",
139 | "#### Sample Input - 2:\n",
140 | "\n",
141 | "Enter the date of February: aa\n",
142 | "\n",
143 | "#### Expected Output:\n",
144 | "\n",
145 | "Invalid Data.\n",
146 | "\n",
147 | "#### Sample Input - 3:\n",
148 | "\n",
149 | "Enter the date of February: 21\n",
150 | "\n",
151 | "#### Expected Output:\n",
152 | "\n",
153 | "'Multiple of 7.'"
154 | ]
155 | },
156 | {
157 | "cell_type": "code",
158 | "execution_count": null,
159 | "metadata": {},
160 | "outputs": [],
161 | "source": []
162 | },
163 | {
164 | "cell_type": "markdown",
165 | "metadata": {},
166 | "source": [
167 | "### Question 7:\n",
168 | "\n",
169 | "Write a python function to find the maximum of three numbers from the list given below, without using the built-in function max().\n",
170 | "\n",
171 | "numList = [8, 12, 7]\n",
172 | "\n",
173 | "#### Expected Output:\n",
174 | "\n",
175 | "12"
176 | ]
177 | },
178 | {
179 | "cell_type": "code",
180 | "execution_count": null,
181 | "metadata": {},
182 | "outputs": [],
183 | "source": []
184 | },
185 | {
186 | "cell_type": "markdown",
187 | "metadata": {},
188 | "source": [
189 | "### Question 8:\n",
190 | "\n",
191 | "Create two modules volume1.py and volume2.py. The volume1.py module contains a class volume_1 that is initialized with length, breadth, and height. It is created to calculate the volumes of cube and cuboid. So, define two instance methods cube() and cuboid() in the class volume_1. The volume2.py module contains a class volume_2 that is initialized with radius and height. It is created to calculate the volumes of spheres, cones, and cylinders. So, define three instance methods sphere(), cone() and cylinder() in the class volume_2. Finally, create a python file result.py to call the methods of both these classes. Remember that the modules volume1.py and volume2.py can also call its methods separately in their respective modules.\n",
192 | "\n",
193 | "#### Sample Input:\n",
194 | "\n",
195 | "For volume1.py:\n",
196 | "\n",
197 | "Length = 3\n",
198 | "
Breadth = 4\n",
199 | "
Height = 5\n",
200 | "\n",
201 | "For volume2.py:\n",
202 | "\n",
203 | "Radius = 6\n",
204 | "
Height = 7\n",
205 | "\n",
206 | "Check for cuboid from volume1.py and sphere from volume2.py.\n",
207 | "\n",
208 | "#### Expected Output:\n",
209 | "\n",
210 | "60\n",
211 | "904.3199999999999"
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": null,
217 | "metadata": {},
218 | "outputs": [],
219 | "source": []
220 | },
221 | {
222 | "cell_type": "markdown",
223 | "metadata": {},
224 | "source": [
225 | "### Question 9:\n",
226 | "\n",
227 | "We have a bag of 9 balls, which has a discrete number from 1 to 9 marked on each of them. But when we take out all the balls from the bag, few of them are missing. Given below is the list of balls that are present.\n",
228 | "\n",
229 | "myList = [1, 2, 3, 5, 6, 7, 9]\n",
230 | "\n",
231 | "Write a python function to find the missing balls.\n",
232 | "\n",
233 | "#### Expected Output:\n",
234 | "\n",
235 | "[4, 8]"
236 | ]
237 | },
238 | {
239 | "cell_type": "code",
240 | "execution_count": null,
241 | "metadata": {},
242 | "outputs": [],
243 | "source": []
244 | },
245 | {
246 | "cell_type": "markdown",
247 | "metadata": {},
248 | "source": [
249 | "### Question 10:\n",
250 | "\n",
251 | "Create a class Fan which is initialized with its brand. The class contains two instance methods for switching on the fan with speed five and switching off the fan with speed 0. Create another method to show the current status of the fan.\n",
252 | "\n",
253 | "#### Sample Input:\n",
254 | "\n",
255 | "Brand = MONTE CARLO\n",
256 | "
Scene - 1: Switch-on the fan and show the status\n",
257 | "
Scene - 2: Switch-off the fan and show the status\n",
258 | "\n",
259 | "#### Expected Output:\n",
260 | "\n",
261 | "The fan manufactured by MONTE CARLO is currently Running with a speed of 5\n",
262 | "
The fan manufactured by MONTE CARLO is currently Idle with a speed of 0"
263 | ]
264 | },
265 | {
266 | "cell_type": "code",
267 | "execution_count": null,
268 | "metadata": {},
269 | "outputs": [],
270 | "source": []
271 | }
272 | ],
273 | "metadata": {
274 | "kernelspec": {
275 | "display_name": "Python 3",
276 | "language": "python",
277 | "name": "python3"
278 | },
279 | "language_info": {
280 | "codemirror_mode": {
281 | "name": "ipython",
282 | "version": 3
283 | },
284 | "file_extension": ".py",
285 | "mimetype": "text/x-python",
286 | "name": "python",
287 | "nbconvert_exporter": "python",
288 | "pygments_lexer": "ipython3",
289 | "version": "3.8.3"
290 | }
291 | },
292 | "nbformat": 4,
293 | "nbformat_minor": 4
294 | }
295 |
--------------------------------------------------------------------------------
/coding-challange-06.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Coding Challange - 06"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Write a python class Person that is initialized with age. Create two more classes, Male and Female, that are inherited from the Person class. The two-child classes should contain an instance method marriage(), which returns if the Person can marry or not depending upon the age.\n",
17 | "\n",
18 | "Take marriage age for Male as 21 years and Female as 18 years. \n",
19 | "\n",
20 | "#### Sample Input:\n",
21 | "\n",
22 | "Male - 16\n",
23 | "
Female - 24\n",
24 | "\n",
25 | "#### Expected Output:\n",
26 | "\n",
27 | "Cannot Marry\n",
28 | "
Can Marry"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": []
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "metadata": {},
41 | "source": [
42 | "### Question 2:\n",
43 | "\n",
44 | "You are given a task to put a number tag on the books on a bookshelf. Each of the n numbers of books should have a number from 1 to n, and different books should have different numbers. Write a python function to calculate the number of digits required for numbering all those tags.\n",
45 | "\n",
46 | "\n",
47 | "#### Expected Output:\n",
48 | "\n",
49 | "Enter the number of books:95\n",
50 | "
181"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {},
57 | "outputs": [],
58 | "source": []
59 | },
60 | {
61 | "cell_type": "markdown",
62 | "metadata": {},
63 | "source": [
64 | "### Question 3:\n",
65 | "\n",
66 | "Write a python program to get the difference between a number and the reverse of that number. Print the difference as a positive integer.\n",
67 | "\n",
68 | "#### Sample Input:\n",
69 | "\n",
70 | "num = 86\n",
71 | "\n",
72 | "#### Expected Output:\n",
73 | "\n",
74 | "18"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": null,
80 | "metadata": {},
81 | "outputs": [],
82 | "source": []
83 | },
84 | {
85 | "cell_type": "markdown",
86 | "metadata": {},
87 | "source": [
88 | "### Question 4:\n",
89 | "\n",
90 | "Write a python function to print the time given below into military time(24 Hours) format.\n",
91 | "\n",
92 | "10:20:45PM\n",
93 | "\n",
94 | "#### Expected Output:\n",
95 | "\n",
96 | "'22:20:45'"
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": null,
102 | "metadata": {},
103 | "outputs": [],
104 | "source": []
105 | },
106 | {
107 | "cell_type": "markdown",
108 | "metadata": {},
109 | "source": [
110 | "### Question 5:\n",
111 | "\n",
112 | "Using user-defined exception, write a python program to guess the number entered in the code until you get the correct answer.\n",
113 | "\n",
114 | "#### Expected Output:\n",
115 | "\n",
116 | "Enter a number greater than 0: 12\n",
117 | "
Less than the desired number, please try again!\n",
118 | "\n",
119 | "Enter a number greater than 0: 18\n",
120 | "
Greater than the desired number, try again!\n",
121 | "\n",
122 | "Enter a number greater than 0: 15\n",
123 | "
BINGO! That's the number."
124 | ]
125 | },
126 | {
127 | "cell_type": "code",
128 | "execution_count": null,
129 | "metadata": {},
130 | "outputs": [],
131 | "source": []
132 | },
133 | {
134 | "cell_type": "markdown",
135 | "metadata": {},
136 | "source": [
137 | "### Question 6:\n",
138 | "\n",
139 | "Write a python function to find the minimum and maximum values calculated by summing precisely four of the five integers from the list given below.\n",
140 | "\n",
141 | "myList = [1, 3, 5, 7, 9]\n",
142 | "\n",
143 | "\n",
144 | "#### Expected Output:\n",
145 | "\n",
146 | "Minimum: 16\n",
147 | "
Maximum: 24"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": null,
153 | "metadata": {},
154 | "outputs": [],
155 | "source": []
156 | },
157 | {
158 | "cell_type": "markdown",
159 | "metadata": {},
160 | "source": [
161 | "### Question 7:\n",
162 | "\n",
163 | "We have a list of numbers given below. Write a python function to ask the user to input a number from the list. Then, push that number towards the end of the list. If the entered number is not present in the list, print the old list.\n",
164 | "\n",
165 | "num_list = [0, 2, 4, 5, 6, 2, 3]\n",
166 | "\n",
167 | "#### Expected Output:\n",
168 | "\n",
169 | "Enter the number to be moved: 2\n",
170 | "
[0, 4, 5, 6, 3, 2, 2]"
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": null,
176 | "metadata": {},
177 | "outputs": [],
178 | "source": []
179 | },
180 | {
181 | "cell_type": "markdown",
182 | "metadata": {},
183 | "source": [
184 | "### Question 8:\n",
185 | "\n",
186 | "Write a python program to check if the number is divisible by its digits.\n",
187 | "\n",
188 | "\n",
189 | "#### Expected Output:\n",
190 | "\n",
191 | "Enter a number: 48\n",
192 | "
Yes"
193 | ]
194 | },
195 | {
196 | "cell_type": "code",
197 | "execution_count": null,
198 | "metadata": {},
199 | "outputs": [],
200 | "source": []
201 | },
202 | {
203 | "cell_type": "markdown",
204 | "metadata": {},
205 | "source": [
206 | "### Question 9:\n",
207 | "\n",
208 | "Write a python function to evaluate the grade ranging from 0 to 100 in an examination. The student fails the exam if the grade is less than 40. But we like to round off grades above 40 based on the following criteria.\n",
209 | "If the difference between the grade and the next multiple of 5 is less than 3, round grade up to the next multiple of 5.\n",
210 | "\n",
211 | "#### Expected Output:\n",
212 | "\n",
213 | "Enter the number of subjects:4\n",
214 | "
Marks of Subjects 1:36\n",
215 | "
Marks of Subjects 2:48\n",
216 | "
Marks of Subjects 3:73\n",
217 | "
Marks of Subjects 4:66\n",
218 | "
Resultant Grade: [36, 50, 75, 66]"
219 | ]
220 | },
221 | {
222 | "cell_type": "code",
223 | "execution_count": null,
224 | "metadata": {},
225 | "outputs": [],
226 | "source": []
227 | },
228 | {
229 | "cell_type": "markdown",
230 | "metadata": {},
231 | "source": [
232 | "### Question 10:\n",
233 | "\n",
234 | "Write a python function grow_more to get a sapling height, which increases by one unit in March and by two units in August in a year. The argument of the function takes the number of growth cycles the sapling goes through. Consider the sapling is planted in January.\n",
235 | "\n",
236 | "#### Sample Input:\n",
237 | "\n",
238 | "7\n",
239 | "\n",
240 | "#### Expected Output:\n",
241 | "\n",
242 | "30\n",
243 | "\n",
244 | "#### Explanation:\n",
245 | "\n",
246 | "At 0 --> 1\n",
247 | "
At 1 --> 2\n",
248 | "
At 2 --> 3\n",
249 | "
At 3 --> 6\n",
250 | "
At 4 --> 7\n",
251 | "
At 5 --> 14\n",
252 | "
At 6 --> 15\n",
253 | "
At 7 --> 30"
254 | ]
255 | },
256 | {
257 | "cell_type": "code",
258 | "execution_count": null,
259 | "metadata": {},
260 | "outputs": [],
261 | "source": []
262 | }
263 | ],
264 | "metadata": {
265 | "kernelspec": {
266 | "display_name": "Python 3",
267 | "language": "python",
268 | "name": "python3"
269 | },
270 | "language_info": {
271 | "codemirror_mode": {
272 | "name": "ipython",
273 | "version": 3
274 | },
275 | "file_extension": ".py",
276 | "mimetype": "text/x-python",
277 | "name": "python",
278 | "nbconvert_exporter": "python",
279 | "pygments_lexer": "ipython3",
280 | "version": "3.8.3"
281 | }
282 | },
283 | "nbformat": 4,
284 | "nbformat_minor": 4
285 | }
286 |
--------------------------------------------------------------------------------
/coding-challenge-04-question-06.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LearningJournal/Python-Foundation-Course/9a99503550ae62ace918c80ea9aa52b9ba51ae17/coding-challenge-04-question-06.zip
--------------------------------------------------------------------------------
/coding-challenge-05-question-08.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LearningJournal/Python-Foundation-Course/9a99503550ae62ace918c80ea9aa52b9ba51ae17/coding-challenge-05-question-08.zip
--------------------------------------------------------------------------------
/practice-exercise-01-solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 01 - Solution"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "There are two numbers given below. Print the sum of these numbers if their product is greater than 100. Otherwise, print their product.\n",
17 | "\n",
18 | "a = 15\n",
19 | "
b = 12\n",
20 | "\n",
21 | "#### Expected Output:\n",
22 | "\n",
23 | "27"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": 1,
29 | "metadata": {},
30 | "outputs": [
31 | {
32 | "name": "stdout",
33 | "output_type": "stream",
34 | "text": [
35 | "27\n"
36 | ]
37 | }
38 | ],
39 | "source": [
40 | "a = 15\n",
41 | "b = 12\n",
42 | "\n",
43 | "if (a * b) > 100:\n",
44 | " print(a + b)\n",
45 | "else:\n",
46 | " print(a * b)"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "### Question 2:\n",
54 | "\n",
55 | "Write a Python program to print the volume of a cone whose height and diameter are given below. (Take pi = 3.14)\n",
56 | "\n",
57 | "h = 10\n",
58 | "
d = 13\n",
59 | "\n",
60 | "#### Expected Output:\n",
61 | "\n",
62 | "442.21666666666664"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": 2,
68 | "metadata": {},
69 | "outputs": [
70 | {
71 | "name": "stdout",
72 | "output_type": "stream",
73 | "text": [
74 | "442.21666666666664\n"
75 | ]
76 | }
77 | ],
78 | "source": [
79 | "h = 10\n",
80 | "d = 13\n",
81 | "\n",
82 | "area = ((1/3)*(3.14)*((d/2)**2)*h)\n",
83 | "print(area)"
84 | ]
85 | },
86 | {
87 | "cell_type": "markdown",
88 | "metadata": {},
89 | "source": [
90 | "### Question 3:\n",
91 | "\n",
92 | "We have the name and seat numbers of a student given below as two tuples. With this given data, print the students' names and their assigned seat numbers in a single line using the appropriate data type.\n",
93 | "\n",
94 | "name = ('Shaun', 'Ron', 'Michael')\n",
95 | "
seat_numbers = (101, 102, 103)\n",
96 | "\n",
97 | "#### Expected Output:\n",
98 | "\n",
99 | "{'Shaun': 101, 'Ron': 102, 'Michael': 103}"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 3,
105 | "metadata": {},
106 | "outputs": [
107 | {
108 | "name": "stdout",
109 | "output_type": "stream",
110 | "text": [
111 | "{'Shaun': 101, 'Ron': 102, 'Michael': 103}\n"
112 | ]
113 | }
114 | ],
115 | "source": [
116 | "student_details = {'Shaun':101, 'Ron':102, 'Michael':103} #Dictionary\n",
117 | "print(student_details)"
118 | ]
119 | },
120 | {
121 | "cell_type": "markdown",
122 | "metadata": {},
123 | "source": [
124 | "### Question 4:\n",
125 | "\n",
126 | "We have a number given below. If the number is greater than 0, add 1 to it. Otherwise, subtract 1 from it, and print the new number obtained.\n",
127 | "\n",
128 | "num = -6\n",
129 | "\n",
130 | "#### Expected Output:\n",
131 | "\n",
132 | "-7"
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "execution_count": 4,
138 | "metadata": {},
139 | "outputs": [
140 | {
141 | "name": "stdout",
142 | "output_type": "stream",
143 | "text": [
144 | "-7\n"
145 | ]
146 | }
147 | ],
148 | "source": [
149 | "num = -6\n",
150 | "\n",
151 | "if num > 0:\n",
152 | " num += 1\n",
153 | " print(num)\n",
154 | "else:\n",
155 | " num -= 1\n",
156 | " print(num)"
157 | ]
158 | },
159 | {
160 | "cell_type": "markdown",
161 | "metadata": {},
162 | "source": [
163 | "### Question 5:\n",
164 | "\n",
165 | "I have four variables, each assigned with certain values given below. A massive expression line follows it. Re-write the expression which suits the desired lexical model.\n",
166 | "\n",
167 | "a = 5\n",
168 | "
b = 2\n",
169 | "
c = 8\n",
170 | "
d = 7\n",
171 | "\n",
172 | "x = (((a + b) * (a + c) * (a + d)) / ((b + a) * (b + c) * (b + d)) / ((c + a) * (c + b) * (c + d))) * (a * b * c * d)\n",
173 | "\n",
174 | "#### Expected Output:\n",
175 | "\n",
176 | "0.4977777777777778"
177 | ]
178 | },
179 | {
180 | "cell_type": "code",
181 | "execution_count": 5,
182 | "metadata": {},
183 | "outputs": [
184 | {
185 | "name": "stdout",
186 | "output_type": "stream",
187 | "text": [
188 | "0.4977777777777778\n"
189 | ]
190 | }
191 | ],
192 | "source": [
193 | "a = 5\n",
194 | "b = 2\n",
195 | "c = 8\n",
196 | "d = 7\n",
197 | "\n",
198 | "x = (((a + b) * (a + c) * (a + d))\\\n",
199 | " / ((b + a) * (b + c) * (b + d))\\\n",
200 | " / ((c + a) * (c + b) * (c + d)))\\\n",
201 | " * (a * b * c * d)\n",
202 | "print(x)"
203 | ]
204 | },
205 | {
206 | "cell_type": "markdown",
207 | "metadata": {},
208 | "source": [
209 | "### Question 6:\n",
210 | "\n",
211 | "There are two numbers given below. Compare them and print the result obtained.\n",
212 | "\n",
213 | "a = 5\n",
214 | "
b = 9\n",
215 | "\n",
216 | "#### Expected Output:\n",
217 | "\n",
218 | "b is greater than a"
219 | ]
220 | },
221 | {
222 | "cell_type": "code",
223 | "execution_count": 6,
224 | "metadata": {},
225 | "outputs": [
226 | {
227 | "name": "stdout",
228 | "output_type": "stream",
229 | "text": [
230 | "b is greater than a\n"
231 | ]
232 | }
233 | ],
234 | "source": [
235 | "a = 5\n",
236 | "b = 9\n",
237 | "\n",
238 | "if a > b:\n",
239 | " print('a is greater than b')\n",
240 | "elif a < b:\n",
241 | " print('b is greater than a')\n",
242 | "else:\n",
243 | " print('a is equal to b')"
244 | ]
245 | },
246 | {
247 | "cell_type": "markdown",
248 | "metadata": {},
249 | "source": [
250 | "### Question 7:\n",
251 | "\n",
252 | "We have a set given below. Find out whether 10 and 7 are present in the given set or not.\n",
253 | "\n",
254 | "mySet = {5, 7, 2, 6, 3}\n",
255 | "\n",
256 | "#### Expected Output:\n",
257 | "\n",
258 | "False\n",
259 | "
True"
260 | ]
261 | },
262 | {
263 | "cell_type": "code",
264 | "execution_count": 7,
265 | "metadata": {},
266 | "outputs": [
267 | {
268 | "name": "stdout",
269 | "output_type": "stream",
270 | "text": [
271 | "False\n",
272 | "True\n"
273 | ]
274 | }
275 | ],
276 | "source": [
277 | "mySet = {5, 7, 2, 6, 3}\n",
278 | "\n",
279 | "print(10 in mySet)\n",
280 | "print(7 in mySet)"
281 | ]
282 | },
283 | {
284 | "cell_type": "markdown",
285 | "metadata": {},
286 | "source": [
287 | "### Question 8:\n",
288 | "\n",
289 | "We have a number given below. Write a program to check for the divisibility of this number by 3 and 5, and print the result obtained.\n",
290 | "\n",
291 | "a = 12\n",
292 | "\n",
293 | "#### Expected Output:\n",
294 | "\n",
295 | "a is divisible by either 3 or 5, but not both"
296 | ]
297 | },
298 | {
299 | "cell_type": "code",
300 | "execution_count": 8,
301 | "metadata": {},
302 | "outputs": [
303 | {
304 | "name": "stdout",
305 | "output_type": "stream",
306 | "text": [
307 | "a is divisible by either 3 or 5, but not both\n"
308 | ]
309 | }
310 | ],
311 | "source": [
312 | "a = 12\n",
313 | "\n",
314 | "if a % 3 == 0 and a % 5 == 0:\n",
315 | " print('a is divisible by both 3 and 5')\n",
316 | "elif a % 3 == 0 or a % 5 == 0:\n",
317 | " print('a is divisible by either 3 or 5, but not both')\n",
318 | "else:\n",
319 | " print('a is neither divisible by 3 or 5')"
320 | ]
321 | },
322 | {
323 | "cell_type": "markdown",
324 | "metadata": {},
325 | "source": [
326 | "### Question 9:\n",
327 | "\n",
328 | "Write a Python program to check if the given year is a leap year.\n",
329 | "\n",
330 | "year = 1996\n",
331 | "\n",
332 | "#### Expected Output:\n",
333 | "\n",
334 | "Leap Year"
335 | ]
336 | },
337 | {
338 | "cell_type": "code",
339 | "execution_count": 9,
340 | "metadata": {},
341 | "outputs": [
342 | {
343 | "name": "stdout",
344 | "output_type": "stream",
345 | "text": [
346 | "Leap Year\n"
347 | ]
348 | }
349 | ],
350 | "source": [
351 | "year = 1996\n",
352 | "\n",
353 | "if year % 4 == 0:\n",
354 | " print('Leap Year')\n",
355 | "else:\n",
356 | " print('Non Leap Year')"
357 | ]
358 | },
359 | {
360 | "cell_type": "markdown",
361 | "metadata": {},
362 | "source": [
363 | "### Question 10:\n",
364 | "\n",
365 | "I have an examination form that requires the following information.\n",
366 | "\n",
367 | "Name, Age, Roll Number, and Subjects. \n",
368 | "\n",
369 | "Declare the parameters mentioned above with a suitable data type, and then assign some values to it, and finally print the result.\n",
370 | "\n",
371 | "#### Expected Output:\n",
372 | "\n",
373 | "Name: Sachin\n",
374 | "
Age: 17\n",
375 | "
Roll Number: 528841\n",
376 | "
Subjects: ['Maths', 'Physics', 'Chemistry', 'Computer Science', 'English']"
377 | ]
378 | },
379 | {
380 | "cell_type": "code",
381 | "execution_count": 10,
382 | "metadata": {},
383 | "outputs": [
384 | {
385 | "name": "stdout",
386 | "output_type": "stream",
387 | "text": [
388 | "Name: Sachin\n",
389 | "Age: 17\n",
390 | "Roll Number: 528841\n",
391 | "Subjects: ['Maths', 'Physics', 'Chemistry', 'Computer Science', 'English']\n"
392 | ]
393 | }
394 | ],
395 | "source": [
396 | "name = 'Sachin' #String Object\n",
397 | "age = 17 #Int Object\n",
398 | "roll_no = 528841 #Int Object\n",
399 | "subjects = ['Maths', 'Physics', 'Chemistry', 'Computer Science', 'English'] #Sequence Type - List \n",
400 | "\n",
401 | "print('Name: ',name)\n",
402 | "print('Age: ',age)\n",
403 | "print('Roll Number: ',roll_no)\n",
404 | "print('Subjects: ',subjects)"
405 | ]
406 | }
407 | ],
408 | "metadata": {
409 | "kernelspec": {
410 | "display_name": "Python 3",
411 | "language": "python",
412 | "name": "python3"
413 | },
414 | "language_info": {
415 | "codemirror_mode": {
416 | "name": "ipython",
417 | "version": 3
418 | },
419 | "file_extension": ".py",
420 | "mimetype": "text/x-python",
421 | "name": "python",
422 | "nbconvert_exporter": "python",
423 | "pygments_lexer": "ipython3",
424 | "version": "3.8.3"
425 | }
426 | },
427 | "nbformat": 4,
428 | "nbformat_minor": 4
429 | }
430 |
--------------------------------------------------------------------------------
/practice-exercise-01.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 01"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "There are two numbers given below. Print the sum of these numbers if their product is greater than 100. Otherwise, print their product.\n",
17 | "\n",
18 | "a = 15\n",
19 | "
b = 12\n",
20 | "\n",
21 | "#### Expected Output:\n",
22 | "\n",
23 | "27"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": []
32 | },
33 | {
34 | "cell_type": "markdown",
35 | "metadata": {},
36 | "source": [
37 | "### Question 2:\n",
38 | "\n",
39 | "Write a Python program to print the volume of a cone whose height and diameter are given below. (Take pi = 3.14)\n",
40 | "\n",
41 | "h = 10\n",
42 | "
d = 13\n",
43 | "\n",
44 | "#### Expected Output:\n",
45 | "\n",
46 | "442.21666666666664"
47 | ]
48 | },
49 | {
50 | "cell_type": "code",
51 | "execution_count": null,
52 | "metadata": {},
53 | "outputs": [],
54 | "source": []
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {},
59 | "source": [
60 | "### Question 3:\n",
61 | "\n",
62 | "We have the name and seat numbers of a student given below as two tuples. With this given data, print the students' names and their assigned seat numbers in a single line using the appropriate data type.\n",
63 | "\n",
64 | "name = ('Shaun', 'Ron', 'Michael')\n",
65 | "
seat_numbers = (101, 102, 103)\n",
66 | "\n",
67 | "#### Expected Output:\n",
68 | "\n",
69 | "{'Shaun': 101, 'Ron': 102, 'Michael': 103}"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": null,
75 | "metadata": {},
76 | "outputs": [],
77 | "source": []
78 | },
79 | {
80 | "cell_type": "markdown",
81 | "metadata": {},
82 | "source": [
83 | "### Question 4:\n",
84 | "\n",
85 | "We have a number given below. If the number is greater than 0, add 1 to it. Otherwise, subtract 1 from it, and print the new number obtained.\n",
86 | "\n",
87 | "num = -6\n",
88 | "\n",
89 | "#### Expected Output:\n",
90 | "\n",
91 | "-7"
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "execution_count": null,
97 | "metadata": {},
98 | "outputs": [],
99 | "source": []
100 | },
101 | {
102 | "cell_type": "markdown",
103 | "metadata": {},
104 | "source": [
105 | "### Question 5:\n",
106 | "\n",
107 | "I have four variables, each assigned with certain values given below. A massive expression line follows it. Re-write the expression which suits the desired lexical model.\n",
108 | "\n",
109 | "a = 5\n",
110 | "
b = 2\n",
111 | "
c = 8\n",
112 | "
d = 7\n",
113 | "\n",
114 | "x = (((a + b) * (a + c) * (a + d)) / ((b + a) * (b + c) * (b + d)) / ((c + a) * (c + b) * (c + d))) * (a * b * c * d)\n",
115 | "\n",
116 | "#### Expected Output:\n",
117 | "\n",
118 | "0.4977777777777778"
119 | ]
120 | },
121 | {
122 | "cell_type": "code",
123 | "execution_count": null,
124 | "metadata": {},
125 | "outputs": [],
126 | "source": []
127 | },
128 | {
129 | "cell_type": "markdown",
130 | "metadata": {},
131 | "source": [
132 | "### Question 6:\n",
133 | "\n",
134 | "There are two numbers given below. Compare them and print the result obtained.\n",
135 | "\n",
136 | "a = 5\n",
137 | "
b = 9\n",
138 | "\n",
139 | "#### Expected Output:\n",
140 | "\n",
141 | "b is greater than a"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": null,
147 | "metadata": {},
148 | "outputs": [],
149 | "source": []
150 | },
151 | {
152 | "cell_type": "markdown",
153 | "metadata": {},
154 | "source": [
155 | "### Question 7:\n",
156 | "\n",
157 | "We have a set given below. Find out whether 10 and 7 are present in the given set or not.\n",
158 | "\n",
159 | "mySet = {5, 7, 2, 6, 3}\n",
160 | "\n",
161 | "#### Expected Output:\n",
162 | "\n",
163 | "False\n",
164 | "
True"
165 | ]
166 | },
167 | {
168 | "cell_type": "code",
169 | "execution_count": null,
170 | "metadata": {},
171 | "outputs": [],
172 | "source": []
173 | },
174 | {
175 | "cell_type": "markdown",
176 | "metadata": {},
177 | "source": [
178 | "### Question 8:\n",
179 | "\n",
180 | "We have a number given below. Write a program to check for the divisibility of this number by 3 and 5, and print the result obtained.\n",
181 | "\n",
182 | "a = 12\n",
183 | "\n",
184 | "#### Expected Output:\n",
185 | "\n",
186 | "a is divisible by either 3 or 5, but not both"
187 | ]
188 | },
189 | {
190 | "cell_type": "code",
191 | "execution_count": null,
192 | "metadata": {},
193 | "outputs": [],
194 | "source": []
195 | },
196 | {
197 | "cell_type": "markdown",
198 | "metadata": {},
199 | "source": [
200 | "### Question 9:\n",
201 | "\n",
202 | "Write a Python program to check if the given year is a leap year.\n",
203 | "\n",
204 | "year = 1996\n",
205 | "\n",
206 | "#### Expected Output:\n",
207 | "\n",
208 | "Leap Year"
209 | ]
210 | },
211 | {
212 | "cell_type": "code",
213 | "execution_count": null,
214 | "metadata": {},
215 | "outputs": [],
216 | "source": []
217 | },
218 | {
219 | "cell_type": "markdown",
220 | "metadata": {},
221 | "source": [
222 | "### Question 10:\n",
223 | "\n",
224 | "I have an examination form that requires the following information.\n",
225 | "\n",
226 | "Name, Age, Roll Number, and Subjects. \n",
227 | "\n",
228 | "Declare the parameters mentioned above with a suitable data type, and then assign some values to it, and finally print the result.\n",
229 | "\n",
230 | "#### Expected Output:\n",
231 | "\n",
232 | "Name: Sachin\n",
233 | "
Age: 17\n",
234 | "
Roll Number: 528841\n",
235 | "
Subjects: ['Maths', 'Physics', 'Chemistry', 'Computer Science', 'English']"
236 | ]
237 | },
238 | {
239 | "cell_type": "code",
240 | "execution_count": null,
241 | "metadata": {},
242 | "outputs": [],
243 | "source": []
244 | }
245 | ],
246 | "metadata": {
247 | "kernelspec": {
248 | "display_name": "Python 3",
249 | "language": "python",
250 | "name": "python3"
251 | },
252 | "language_info": {
253 | "codemirror_mode": {
254 | "name": "ipython",
255 | "version": 3
256 | },
257 | "file_extension": ".py",
258 | "mimetype": "text/x-python",
259 | "name": "python",
260 | "nbconvert_exporter": "python",
261 | "pygments_lexer": "ipython3",
262 | "version": "3.8.3"
263 | }
264 | },
265 | "nbformat": 4,
266 | "nbformat_minor": 4
267 | }
268 |
--------------------------------------------------------------------------------
/practice-exercise-02-solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 02 - Solution"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Print the item 'Orange' from the list of fruits given below.\n",
17 | "\n",
18 | "fruits = ['Apple', 'Grapes', 'Orange', 'Pineapple', 'Watermelon']\n",
19 | "\n",
20 | "#### Expected Output:\n",
21 | "\n",
22 | "Orange"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": 1,
28 | "metadata": {},
29 | "outputs": [
30 | {
31 | "name": "stdout",
32 | "output_type": "stream",
33 | "text": [
34 | "Orange\n"
35 | ]
36 | }
37 | ],
38 | "source": [
39 | "fruits = ['Apple', 'Grapes', 'Orange', 'Pineapple', 'Watermelon']\n",
40 | "\n",
41 | "print(fruits[2])"
42 | ]
43 | },
44 | {
45 | "cell_type": "markdown",
46 | "metadata": {},
47 | "source": [
48 | "### Question 2:\n",
49 | "\n",
50 | "We have a list of fruits and a string given below. Since the string contains fruit, add that string to the given list and print that new list.\n",
51 | "\n",
52 | "fruit_list = ['Apple', 'Grapes', 'Orange', 'Pineapple', 'Watermelon']\n",
53 | "
fruit_string = 'Mango'\n",
54 | "\n",
55 | "#### Expected Output:\n",
56 | "\n",
57 | "['Apple', 'Grapes', 'Orange', 'Pineapple', 'Watermelon', 'Mango']"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 2,
63 | "metadata": {},
64 | "outputs": [
65 | {
66 | "name": "stdout",
67 | "output_type": "stream",
68 | "text": [
69 | "['Apple', 'Grapes', 'Orange', 'Pineapple', 'Watermelon', 'Mango']\n"
70 | ]
71 | }
72 | ],
73 | "source": [
74 | "fruit_list = ['Apple', 'Grapes', 'Orange', 'Pineapple', 'Watermelon']\n",
75 | "fruit_string = 'Mango'\n",
76 | "\n",
77 | "print(fruit_list + [fruit_string])"
78 | ]
79 | },
80 | {
81 | "cell_type": "markdown",
82 | "metadata": {},
83 | "source": [
84 | "### Question 3:\n",
85 | "\n",
86 | "There is a list given below which contains the name of cities. Repeat this list of cities three times, and print the old list and the new list.\n",
87 | "\n",
88 | "cityList = ['London', 'New York', 'Delhi']\n",
89 | "\n",
90 | "#### Expected Output:\n",
91 | "\n",
92 | "Old City List: ['London', 'New York', 'Delhi']\n",
93 | "
New City List: ['London', 'New York', 'Delhi', 'London', 'New York', 'Delhi', 'London', 'New York', 'Delhi']"
94 | ]
95 | },
96 | {
97 | "cell_type": "code",
98 | "execution_count": 3,
99 | "metadata": {},
100 | "outputs": [
101 | {
102 | "name": "stdout",
103 | "output_type": "stream",
104 | "text": [
105 | "Old City List: ['London', 'New York', 'Delhi']\n",
106 | "New City List: ['London', 'New York', 'Delhi', 'London', 'New York', 'Delhi', 'London', 'New York', 'Delhi']\n"
107 | ]
108 | }
109 | ],
110 | "source": [
111 | "cityList = ['London', 'New York', 'Delhi']\n",
112 | "\n",
113 | "repeatedList = cityList * 3\n",
114 | "print(\"Old City List: \" + str(cityList))\n",
115 | "print(\"New City List: \" + str(repeatedList))"
116 | ]
117 | },
118 | {
119 | "cell_type": "markdown",
120 | "metadata": {},
121 | "source": [
122 | "### Question 4:\n",
123 | "\n",
124 | "Check if the city 'Delhi' is present in the list of cities given below.\n",
125 | "\n",
126 | "cityList = ['London', 'New York', 'Delhi', 'Mumbai', 'Paris']\n",
127 | "\n",
128 | "#### Expected Output:\n",
129 | "\n",
130 | "True"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": 4,
136 | "metadata": {},
137 | "outputs": [
138 | {
139 | "name": "stdout",
140 | "output_type": "stream",
141 | "text": [
142 | "True\n"
143 | ]
144 | }
145 | ],
146 | "source": [
147 | "cityList = ['London', 'New York', 'Delhi', 'Mumbai', 'Paris']\n",
148 | "\n",
149 | "print('Delhi' in cityList)"
150 | ]
151 | },
152 | {
153 | "cell_type": "markdown",
154 | "metadata": {},
155 | "source": [
156 | "### Question 5:\n",
157 | "\n",
158 | "Reverse the string given below.\n",
159 | "\n",
160 | "name = 'Learning Journal'\n",
161 | "\n",
162 | "#### Expected Output:\n",
163 | "\n",
164 | "'lanruoJ gninraeL'"
165 | ]
166 | },
167 | {
168 | "cell_type": "code",
169 | "execution_count": 5,
170 | "metadata": {},
171 | "outputs": [
172 | {
173 | "data": {
174 | "text/plain": [
175 | "'lanruoJ gninraeL'"
176 | ]
177 | },
178 | "execution_count": 5,
179 | "metadata": {},
180 | "output_type": "execute_result"
181 | }
182 | ],
183 | "source": [
184 | "name = 'Learning Journal'\n",
185 | "\n",
186 | "name[::-1]"
187 | ]
188 | },
189 | {
190 | "cell_type": "markdown",
191 | "metadata": {},
192 | "source": [
193 | "### Question 6:\n",
194 | "\n",
195 | "There is a string object given below. Print the word 'sunny' from string 'msg.'\n",
196 | "\n",
197 | "msg = 'A bright sunny day'\n",
198 | "\n",
199 | "#### Expected Solution:\n",
200 | "\n",
201 | "'sunny'"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "execution_count": 6,
207 | "metadata": {},
208 | "outputs": [
209 | {
210 | "data": {
211 | "text/plain": [
212 | "'sunny'"
213 | ]
214 | },
215 | "execution_count": 6,
216 | "metadata": {},
217 | "output_type": "execute_result"
218 | }
219 | ],
220 | "source": [
221 | "msg = 'A bright sunny day'\n",
222 | "\n",
223 | "msg[9:14]"
224 | ]
225 | },
226 | {
227 | "cell_type": "markdown",
228 | "metadata": {},
229 | "source": [
230 | "### Question 7:\n",
231 | "\n",
232 | "Check if the cities 'New York' and 'Delhi' are present in the list of cities given below.\n",
233 | "\n",
234 | "cityList = ['London', 'New York', 'Delhi', 'Mumbai', 'Paris']\n",
235 | "\n",
236 | "#### Expected Output:\n",
237 | "\n",
238 | "True\n",
239 | "
True"
240 | ]
241 | },
242 | {
243 | "cell_type": "code",
244 | "execution_count": 7,
245 | "metadata": {},
246 | "outputs": [
247 | {
248 | "name": "stdout",
249 | "output_type": "stream",
250 | "text": [
251 | "True\n",
252 | "True\n"
253 | ]
254 | }
255 | ],
256 | "source": [
257 | "cityList = ['London', 'New York', 'Delhi', 'Mumbai', 'Paris']\n",
258 | "\n",
259 | "print('New York' in cityList)\n",
260 | "print('Delhi' in cityList)"
261 | ]
262 | },
263 | {
264 | "cell_type": "markdown",
265 | "metadata": {},
266 | "source": [
267 | "#### Explanation:\n",
268 | "\n",
269 | "If your try searching for 'New York' and 'Delhi' in a single statement like this.\n",
270 | "\n",
271 | "print(['New York', 'Delhi'] in cityList)\n",
272 | "\n",
273 | "You will get the output as false. This is because subsequence containment is only applicable to str objects."
274 | ]
275 | },
276 | {
277 | "cell_type": "markdown",
278 | "metadata": {},
279 | "source": [
280 | "### Question 8:\n",
281 | "\n",
282 | "Using the slicing operation, remove the whitespaces between the letters and print the string once again.\n",
283 | "\n",
284 | "name = 'P y t h o n'\n",
285 | "\n",
286 | "#### Expected Output:\n",
287 | "\n",
288 | "'Python'"
289 | ]
290 | },
291 | {
292 | "cell_type": "code",
293 | "execution_count": 8,
294 | "metadata": {},
295 | "outputs": [
296 | {
297 | "data": {
298 | "text/plain": [
299 | "'Python'"
300 | ]
301 | },
302 | "execution_count": 8,
303 | "metadata": {},
304 | "output_type": "execute_result"
305 | }
306 | ],
307 | "source": [
308 | "name = 'P y t h o n'\n",
309 | "\n",
310 | "name[::2]"
311 | ]
312 | },
313 | {
314 | "cell_type": "markdown",
315 | "metadata": {},
316 | "source": [
317 | "### Question 9:\n",
318 | "\n",
319 | "Print the index of the letter 'h' from the string given below.\n",
320 | "\n",
321 | "name = 'Python'\n",
322 | "\n",
323 | "#### Expected Output:\n",
324 | "\n",
325 | "3"
326 | ]
327 | },
328 | {
329 | "cell_type": "code",
330 | "execution_count": 9,
331 | "metadata": {},
332 | "outputs": [
333 | {
334 | "name": "stdout",
335 | "output_type": "stream",
336 | "text": [
337 | "3\n"
338 | ]
339 | }
340 | ],
341 | "source": [
342 | "name = 'Python'\n",
343 | "\n",
344 | "print(name.index('h'))"
345 | ]
346 | },
347 | {
348 | "cell_type": "markdown",
349 | "metadata": {},
350 | "source": [
351 | "### Question 10:\n",
352 | "\n",
353 | "Print the odd indexed elements from the list of colors given below.\n",
354 | "\n",
355 | "myList = ['Red', 'Blue', 'Orange', 'White', 'Black', 'Yellow']\n",
356 | "\n",
357 | "#### Expected Output:\n",
358 | "\n",
359 | "['Blue', 'White', 'Yellow']"
360 | ]
361 | },
362 | {
363 | "cell_type": "code",
364 | "execution_count": 10,
365 | "metadata": {},
366 | "outputs": [
367 | {
368 | "data": {
369 | "text/plain": [
370 | "['Blue', 'White', 'Yellow']"
371 | ]
372 | },
373 | "execution_count": 10,
374 | "metadata": {},
375 | "output_type": "execute_result"
376 | }
377 | ],
378 | "source": [
379 | "myList = ['Red', 'Blue', 'Orange', 'White', 'Black', 'Yellow']\n",
380 | "\n",
381 | "myList[1::2]"
382 | ]
383 | }
384 | ],
385 | "metadata": {
386 | "kernelspec": {
387 | "display_name": "Python 3",
388 | "language": "python",
389 | "name": "python3"
390 | },
391 | "language_info": {
392 | "codemirror_mode": {
393 | "name": "ipython",
394 | "version": 3
395 | },
396 | "file_extension": ".py",
397 | "mimetype": "text/x-python",
398 | "name": "python",
399 | "nbconvert_exporter": "python",
400 | "pygments_lexer": "ipython3",
401 | "version": "3.8.3"
402 | }
403 | },
404 | "nbformat": 4,
405 | "nbformat_minor": 4
406 | }
407 |
--------------------------------------------------------------------------------
/practice-exercise-02.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 02"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Print the item 'Orange' from the list of fruits given below.\n",
17 | "\n",
18 | "fruits = ['Apple', 'Grapes', 'Orange', 'Pineapple', 'Watermelon']\n",
19 | "\n",
20 | "#### Expected Output:\n",
21 | "\n",
22 | "Orange"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": null,
28 | "metadata": {},
29 | "outputs": [],
30 | "source": []
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "### Question 2:\n",
37 | "\n",
38 | "We have a list of fruits and a string given below. Since the string contains fruit, add that string to the given list and print that new list.\n",
39 | "\n",
40 | "fruit_list = ['Apple', 'Grapes', 'Orange', 'Pineapple', 'Watermelon']\n",
41 | "
fruit_string = 'Mango'\n",
42 | "\n",
43 | "#### Expected Output:\n",
44 | "\n",
45 | "['Apple', 'Grapes', 'Orange', 'Pineapple', 'Watermelon', 'Mango']"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": null,
51 | "metadata": {},
52 | "outputs": [],
53 | "source": []
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "### Question 3:\n",
60 | "\n",
61 | "There is a list given below which contains the name of cities. Repeat this list of cities three times, and print the old list and the new list.\n",
62 | "\n",
63 | "cityList = ['London', 'New York', 'Delhi']\n",
64 | "\n",
65 | "#### Expected Output:\n",
66 | "\n",
67 | "Old City List: ['London', 'New York', 'Delhi']\n",
68 | "
New City List: ['London', 'New York', 'Delhi', 'London', 'New York', 'Delhi', 'London', 'New York', 'Delhi']"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": null,
74 | "metadata": {},
75 | "outputs": [],
76 | "source": []
77 | },
78 | {
79 | "cell_type": "markdown",
80 | "metadata": {},
81 | "source": [
82 | "### Question 4:\n",
83 | "\n",
84 | "Check if the city 'Delhi' is present in the list of cities given below.\n",
85 | "\n",
86 | "cityList = ['London', 'New York', 'Delhi', 'Mumbai', 'Paris']\n",
87 | "\n",
88 | "#### Expected Output:\n",
89 | "\n",
90 | "True"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": null,
96 | "metadata": {},
97 | "outputs": [],
98 | "source": []
99 | },
100 | {
101 | "cell_type": "markdown",
102 | "metadata": {},
103 | "source": [
104 | "### Question 5:\n",
105 | "\n",
106 | "Reverse the string given below.\n",
107 | "\n",
108 | "name = 'Learning Journal'\n",
109 | "\n",
110 | "#### Expected Output:\n",
111 | "\n",
112 | "'lanruoJ gninraeL'"
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "execution_count": null,
118 | "metadata": {},
119 | "outputs": [],
120 | "source": []
121 | },
122 | {
123 | "cell_type": "markdown",
124 | "metadata": {},
125 | "source": [
126 | "### Question 6:\n",
127 | "\n",
128 | "There is a string object given below. Print the word 'sunny' from string 'msg.'\n",
129 | "\n",
130 | "msg = 'A bright sunny day'\n",
131 | "\n",
132 | "#### Expected Solution:\n",
133 | "\n",
134 | "'sunny'"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": null,
140 | "metadata": {},
141 | "outputs": [],
142 | "source": []
143 | },
144 | {
145 | "cell_type": "markdown",
146 | "metadata": {},
147 | "source": [
148 | "### Question 7:\n",
149 | "\n",
150 | "Check if the cities 'New York' and 'Delhi' are present in the list of cities given below.\n",
151 | "\n",
152 | "cityList = ['London', 'New York', 'Delhi', 'Mumbai', 'Paris']\n",
153 | "\n",
154 | "#### Expected Output:\n",
155 | "\n",
156 | "True\n",
157 | "
True"
158 | ]
159 | },
160 | {
161 | "cell_type": "code",
162 | "execution_count": null,
163 | "metadata": {},
164 | "outputs": [],
165 | "source": []
166 | },
167 | {
168 | "cell_type": "markdown",
169 | "metadata": {},
170 | "source": [
171 | "### Question 8:\n",
172 | "\n",
173 | "Using the slicing operation, remove the whitespaces between the letters and print the string once again.\n",
174 | "\n",
175 | "name = 'P y t h o n'\n",
176 | "\n",
177 | "#### Expected Output:\n",
178 | "\n",
179 | "'Python'"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": null,
185 | "metadata": {},
186 | "outputs": [],
187 | "source": []
188 | },
189 | {
190 | "cell_type": "markdown",
191 | "metadata": {},
192 | "source": [
193 | "### Question 9:\n",
194 | "\n",
195 | "Print the index of the letter 'h' from the string given below.\n",
196 | "\n",
197 | "name = 'Python'\n",
198 | "\n",
199 | "#### Expected Output:\n",
200 | "\n",
201 | "3"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "execution_count": null,
207 | "metadata": {},
208 | "outputs": [],
209 | "source": []
210 | },
211 | {
212 | "cell_type": "markdown",
213 | "metadata": {},
214 | "source": [
215 | "### Question 10:\n",
216 | "\n",
217 | "Print the odd indexed elements from the list of colors given below.\n",
218 | "\n",
219 | "myList = ['Red', 'Blue', 'Orange', 'White', 'Black', 'Yellow']\n",
220 | "\n",
221 | "#### Expected Output:\n",
222 | "\n",
223 | "['Blue', 'White', 'Yellow']"
224 | ]
225 | },
226 | {
227 | "cell_type": "code",
228 | "execution_count": null,
229 | "metadata": {},
230 | "outputs": [],
231 | "source": []
232 | }
233 | ],
234 | "metadata": {
235 | "kernelspec": {
236 | "display_name": "Python 3",
237 | "language": "python",
238 | "name": "python3"
239 | },
240 | "language_info": {
241 | "codemirror_mode": {
242 | "name": "ipython",
243 | "version": 3
244 | },
245 | "file_extension": ".py",
246 | "mimetype": "text/x-python",
247 | "name": "python",
248 | "nbconvert_exporter": "python",
249 | "pygments_lexer": "ipython3",
250 | "version": "3.8.3"
251 | }
252 | },
253 | "nbformat": 4,
254 | "nbformat_minor": 4
255 | }
256 |
--------------------------------------------------------------------------------
/practice-exercise-03-solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 03 - Solution"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Convert the string given below into a list by taking each word separated by a comma as an element.\n",
17 | "\n",
18 | "languages = \"Java, Python, C++, Scala, C\"\n",
19 | "\n",
20 | "#### Expected Output:\n",
21 | "\n",
22 | "['Java', ' Python', ' C++', ' Scala', ' C']"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": 1,
28 | "metadata": {},
29 | "outputs": [
30 | {
31 | "data": {
32 | "text/plain": [
33 | "['Java', ' Python', ' C++', ' Scala', ' C']"
34 | ]
35 | },
36 | "execution_count": 1,
37 | "metadata": {},
38 | "output_type": "execute_result"
39 | }
40 | ],
41 | "source": [
42 | "languages = \"Java, Python, C++, Scala, C\"\n",
43 | "\n",
44 | "languages.split(',')"
45 | ]
46 | },
47 | {
48 | "cell_type": "markdown",
49 | "metadata": {},
50 | "source": [
51 | "### Question 2:\n",
52 | "\n",
53 | "We have a list of numbers given below. Add the element '10' at the 3rd position of the list and finally print the list.\n",
54 | "\n",
55 | "num_list = [5, 6, 8, 7, 9]\n",
56 | "\n",
57 | "#### Expected Output:\n",
58 | "\n",
59 | "[5, 6, 8, 10, 7, 9]"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": 2,
65 | "metadata": {},
66 | "outputs": [
67 | {
68 | "name": "stdout",
69 | "output_type": "stream",
70 | "text": [
71 | "[5, 6, 8, 10, 7, 9]\n"
72 | ]
73 | }
74 | ],
75 | "source": [
76 | "num_list = [5, 6, 8, 7, 9]\n",
77 | "\n",
78 | "num_list.insert(3, 10)\n",
79 | "\n",
80 | "print(num_list)"
81 | ]
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "metadata": {},
86 | "source": [
87 | "### Question 3:\n",
88 | "\n",
89 | "Print all the multiples of 3 between 5 and 25.\n",
90 | "\n",
91 | "#### Expected Output:\n",
92 | "\n",
93 | "6\n",
94 | "
9\n",
95 | "
12\n",
96 | "
15\n",
97 | "
18\n",
98 | "
21\n",
99 | "
24"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 3,
105 | "metadata": {},
106 | "outputs": [
107 | {
108 | "name": "stdout",
109 | "output_type": "stream",
110 | "text": [
111 | "6\n",
112 | "9\n",
113 | "12\n",
114 | "15\n",
115 | "18\n",
116 | "21\n",
117 | "24\n"
118 | ]
119 | }
120 | ],
121 | "source": [
122 | "for i in range(5, 25):\n",
123 | " if i % 3 == 0:\n",
124 | " print(i)"
125 | ]
126 | },
127 | {
128 | "cell_type": "markdown",
129 | "metadata": {},
130 | "source": [
131 | "### Question 4:\n",
132 | "\n",
133 | "We have a list of numbers given below. Print the square of these numbers into another list using list comprehension.\n",
134 | "\n",
135 | "num = [2, 4, 6, 8]\n",
136 | "\n",
137 | "#### Expected Output:\n",
138 | "\n",
139 | "[4, 16, 36, 64]"
140 | ]
141 | },
142 | {
143 | "cell_type": "code",
144 | "execution_count": 4,
145 | "metadata": {},
146 | "outputs": [
147 | {
148 | "name": "stdout",
149 | "output_type": "stream",
150 | "text": [
151 | "[4, 16, 36, 64]\n"
152 | ]
153 | }
154 | ],
155 | "source": [
156 | "num = [2, 4, 6, 8]\n",
157 | "\n",
158 | "square = [i**2 for i in num]\n",
159 | "\n",
160 | "print(square)"
161 | ]
162 | },
163 | {
164 | "cell_type": "markdown",
165 | "metadata": {},
166 | "source": [
167 | "### Question 5:\n",
168 | "\n",
169 | "Print the first 10 natural numbers.\n",
170 | "\n",
171 | "#### Expected Output:\n",
172 | "\n",
173 | "1\n",
174 | "
2\n",
175 | "
3\n",
176 | "
4\n",
177 | "
5\n",
178 | "
6\n",
179 | "
7\n",
180 | "
8\n",
181 | "
9\n",
182 | "
10"
183 | ]
184 | },
185 | {
186 | "cell_type": "code",
187 | "execution_count": 5,
188 | "metadata": {},
189 | "outputs": [
190 | {
191 | "name": "stdout",
192 | "output_type": "stream",
193 | "text": [
194 | "1\n",
195 | "2\n",
196 | "3\n",
197 | "4\n",
198 | "5\n",
199 | "6\n",
200 | "7\n",
201 | "8\n",
202 | "9\n",
203 | "10\n"
204 | ]
205 | }
206 | ],
207 | "source": [
208 | "num = 1\n",
209 | "\n",
210 | "while num <= 10:\n",
211 | " print(num)\n",
212 | " num += 1"
213 | ]
214 | },
215 | {
216 | "cell_type": "markdown",
217 | "metadata": {},
218 | "source": [
219 | "### Question 6:\n",
220 | "\n",
221 | "We have a tuple of numbers given below. Remove the largest number from the tuple and print it in sorted order.\n",
222 | "\n",
223 | "num_tuple = (5, 8, 13, 2, 17, 1)\n",
224 | "\n",
225 | "#### Expected Output:\n",
226 | "\n",
227 | "[1, 2, 5, 8, 13]"
228 | ]
229 | },
230 | {
231 | "cell_type": "code",
232 | "execution_count": 6,
233 | "metadata": {},
234 | "outputs": [
235 | {
236 | "name": "stdout",
237 | "output_type": "stream",
238 | "text": [
239 | "[1, 2, 5, 8, 13]\n"
240 | ]
241 | }
242 | ],
243 | "source": [
244 | "num_tuple = (5, 8, 13, 2, 17, 1)\n",
245 | "\n",
246 | "sorted_num = sorted(num_tuple)\n",
247 | "sorted_num.pop()\n",
248 | "\n",
249 | "print(sorted_num)"
250 | ]
251 | },
252 | {
253 | "cell_type": "markdown",
254 | "metadata": {},
255 | "source": [
256 | "### Question 7:\n",
257 | "\n",
258 | "Convert the list given below into a string using a comma as a separator argument.\n",
259 | "\n",
260 | "myList = ['Lenovo', ' Dell', ' Acer', ' Asus', ' HP']\n",
261 | "\n",
262 | "#### Expected Output:\n",
263 | "\n",
264 | "'Lenovo, Dell, Acer, Asus, HP'"
265 | ]
266 | },
267 | {
268 | "cell_type": "code",
269 | "execution_count": 7,
270 | "metadata": {},
271 | "outputs": [
272 | {
273 | "data": {
274 | "text/plain": [
275 | "'Lenovo, Dell, Acer, Asus, HP'"
276 | ]
277 | },
278 | "execution_count": 7,
279 | "metadata": {},
280 | "output_type": "execute_result"
281 | }
282 | ],
283 | "source": [
284 | "myList = ['Lenovo', ' Dell', ' Acer', ' Asus', ' HP']\n",
285 | "\n",
286 | "myString = ','.join(myList)\n",
287 | "\n",
288 | "myString"
289 | ]
290 | },
291 | {
292 | "cell_type": "markdown",
293 | "metadata": {},
294 | "source": [
295 | "### Question 8:\n",
296 | "\n",
297 | "Print all the even numbers between -10 and 0.\n",
298 | "\n",
299 | "#### Expected Output:\n",
300 | "\n",
301 | "-10\n",
302 | "
-8\n",
303 | "
-6\n",
304 | "
-4\n",
305 | "
-2"
306 | ]
307 | },
308 | {
309 | "cell_type": "code",
310 | "execution_count": 8,
311 | "metadata": {},
312 | "outputs": [
313 | {
314 | "name": "stdout",
315 | "output_type": "stream",
316 | "text": [
317 | "-10\n",
318 | "-8\n",
319 | "-6\n",
320 | "-4\n",
321 | "-2\n"
322 | ]
323 | }
324 | ],
325 | "source": [
326 | "for i in range(-10, 0, 2):\n",
327 | " print(i)"
328 | ]
329 | },
330 | {
331 | "cell_type": "markdown",
332 | "metadata": {},
333 | "source": [
334 | "### Question 9:\n",
335 | "\n",
336 | "Reverse the integer given below.\n",
337 | "\n",
338 | "n = 5623\n",
339 | "\n",
340 | "#### Expected Output:\n",
341 | "\n",
342 | "3265"
343 | ]
344 | },
345 | {
346 | "cell_type": "code",
347 | "execution_count": 9,
348 | "metadata": {},
349 | "outputs": [
350 | {
351 | "name": "stdout",
352 | "output_type": "stream",
353 | "text": [
354 | "3265\n"
355 | ]
356 | }
357 | ],
358 | "source": [
359 | "n = 5623\n",
360 | "rev = 0\n",
361 | "\n",
362 | "while n > 0:\n",
363 | " rem = n % 10\n",
364 | " rev = (rev * 10) + rem\n",
365 | " n = n // 10\n",
366 | " \n",
367 | "print(rev)"
368 | ]
369 | },
370 | {
371 | "cell_type": "markdown",
372 | "metadata": {},
373 | "source": [
374 | "### Question 10:\n",
375 | "\n",
376 | "We have a list of numbers given below. Print the number, square of the number, and the cube of the number, all in a single list.\n",
377 | "\n",
378 | "num = [2, 4, 6, 8]\n",
379 | "\n",
380 | "#### Expected Output:\n",
381 | "\n",
382 | "[(2, 4, 8), (4, 16, 64), (6, 36, 216), (8, 64, 512)]"
383 | ]
384 | },
385 | {
386 | "cell_type": "code",
387 | "execution_count": 10,
388 | "metadata": {},
389 | "outputs": [
390 | {
391 | "name": "stdout",
392 | "output_type": "stream",
393 | "text": [
394 | "[(2, 4, 8), (4, 16, 64), (6, 36, 216), (8, 64, 512)]\n"
395 | ]
396 | }
397 | ],
398 | "source": [
399 | "num = [2, 4, 6, 8]\n",
400 | "\n",
401 | "result = [(x, x**2, x**3) for x in num]\n",
402 | "\n",
403 | "print(result)"
404 | ]
405 | }
406 | ],
407 | "metadata": {
408 | "kernelspec": {
409 | "display_name": "Python 3",
410 | "language": "python",
411 | "name": "python3"
412 | },
413 | "language_info": {
414 | "codemirror_mode": {
415 | "name": "ipython",
416 | "version": 3
417 | },
418 | "file_extension": ".py",
419 | "mimetype": "text/x-python",
420 | "name": "python",
421 | "nbconvert_exporter": "python",
422 | "pygments_lexer": "ipython3",
423 | "version": "3.8.3"
424 | }
425 | },
426 | "nbformat": 4,
427 | "nbformat_minor": 4
428 | }
429 |
--------------------------------------------------------------------------------
/practice-exercise-03.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 03"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Convert the string given below into a list by taking each word separated by a comma as an element.\n",
17 | "\n",
18 | "languages = \"Java, Python, C++, Scala, C\"\n",
19 | "\n",
20 | "#### Expected Output:\n",
21 | "\n",
22 | "['Java', ' Python', ' C++', ' Scala', ' C']"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": null,
28 | "metadata": {},
29 | "outputs": [],
30 | "source": []
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "### Question 2:\n",
37 | "\n",
38 | "We have a list of numbers given below. Add the element '10' at the 3rd position of the list and finally print the list.\n",
39 | "\n",
40 | "num_list = [5, 6, 8, 7, 9]\n",
41 | "\n",
42 | "#### Expected Output:\n",
43 | "\n",
44 | "[5, 6, 8, 10, 7, 9]"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": null,
50 | "metadata": {},
51 | "outputs": [],
52 | "source": []
53 | },
54 | {
55 | "cell_type": "markdown",
56 | "metadata": {},
57 | "source": [
58 | "### Question 3:\n",
59 | "\n",
60 | "Print all the multiples of 3 between 5 and 25.\n",
61 | "\n",
62 | "#### Expected Output:\n",
63 | "\n",
64 | "6\n",
65 | "
9\n",
66 | "
12\n",
67 | "
15\n",
68 | "
18\n",
69 | "
21\n",
70 | "
24"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": null,
76 | "metadata": {},
77 | "outputs": [],
78 | "source": []
79 | },
80 | {
81 | "cell_type": "markdown",
82 | "metadata": {},
83 | "source": [
84 | "### Question 4:\n",
85 | "\n",
86 | "We have a list of numbers given below. Print the square of these numbers into another list using list comprehension.\n",
87 | "\n",
88 | "num = [2, 4, 6, 8]\n",
89 | "\n",
90 | "#### Expected Output:\n",
91 | "\n",
92 | "[4, 16, 36, 64]"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "metadata": {},
99 | "outputs": [],
100 | "source": []
101 | },
102 | {
103 | "cell_type": "markdown",
104 | "metadata": {},
105 | "source": [
106 | "### Question 5:\n",
107 | "\n",
108 | "Print the first 10 natural numbers.\n",
109 | "\n",
110 | "#### Expected Output:\n",
111 | "\n",
112 | "1\n",
113 | "
2\n",
114 | "
3\n",
115 | "
4\n",
116 | "
5\n",
117 | "
6\n",
118 | "
7\n",
119 | "
8\n",
120 | "
9\n",
121 | "
10"
122 | ]
123 | },
124 | {
125 | "cell_type": "code",
126 | "execution_count": null,
127 | "metadata": {},
128 | "outputs": [],
129 | "source": []
130 | },
131 | {
132 | "cell_type": "markdown",
133 | "metadata": {},
134 | "source": [
135 | "### Question 6:\n",
136 | "\n",
137 | "We have a tuple of numbers given below. Remove the largest number from the tuple and print it in sorted order.\n",
138 | "\n",
139 | "num_tuple = (5, 8, 13, 2, 17, 1)\n",
140 | "\n",
141 | "#### Expected Output:\n",
142 | "\n",
143 | "[1, 2, 5, 8, 13]"
144 | ]
145 | },
146 | {
147 | "cell_type": "code",
148 | "execution_count": null,
149 | "metadata": {},
150 | "outputs": [],
151 | "source": []
152 | },
153 | {
154 | "cell_type": "markdown",
155 | "metadata": {},
156 | "source": [
157 | "### Question 7:\n",
158 | "\n",
159 | "Convert the list given below into a string using a comma as a separator argument.\n",
160 | "\n",
161 | "myList = ['Lenovo', ' Dell', ' Acer', ' Asus', ' HP']\n",
162 | "\n",
163 | "#### Expected Output:\n",
164 | "\n",
165 | "'Lenovo, Dell, Acer, Asus, HP'"
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": null,
171 | "metadata": {},
172 | "outputs": [],
173 | "source": []
174 | },
175 | {
176 | "cell_type": "markdown",
177 | "metadata": {},
178 | "source": [
179 | "### Question 8:\n",
180 | "\n",
181 | "Print all the even numbers between -10 and 0.\n",
182 | "\n",
183 | "#### Expected Output:\n",
184 | "\n",
185 | "-10\n",
186 | "
-8\n",
187 | "
-6\n",
188 | "
-4\n",
189 | "
-2"
190 | ]
191 | },
192 | {
193 | "cell_type": "code",
194 | "execution_count": null,
195 | "metadata": {},
196 | "outputs": [],
197 | "source": []
198 | },
199 | {
200 | "cell_type": "markdown",
201 | "metadata": {},
202 | "source": [
203 | "### Question 9:\n",
204 | "\n",
205 | "Reverse the integer given below.\n",
206 | "\n",
207 | "n = 5623\n",
208 | "\n",
209 | "#### Expected Output:\n",
210 | "\n",
211 | "3265"
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": null,
217 | "metadata": {},
218 | "outputs": [],
219 | "source": []
220 | },
221 | {
222 | "cell_type": "markdown",
223 | "metadata": {},
224 | "source": [
225 | "### Question 10:\n",
226 | "\n",
227 | "We have a list of numbers given below. Print the number, square of the number, and the cube of the number, all in a single list.\n",
228 | "\n",
229 | "num = [2, 4, 6, 8]\n",
230 | "\n",
231 | "#### Expected Output:\n",
232 | "\n",
233 | "[(2, 4, 8), (4, 16, 64), (6, 36, 216), (8, 64, 512)]"
234 | ]
235 | },
236 | {
237 | "cell_type": "code",
238 | "execution_count": null,
239 | "metadata": {},
240 | "outputs": [],
241 | "source": []
242 | }
243 | ],
244 | "metadata": {
245 | "kernelspec": {
246 | "display_name": "Python 3",
247 | "language": "python",
248 | "name": "python3"
249 | },
250 | "language_info": {
251 | "codemirror_mode": {
252 | "name": "ipython",
253 | "version": 3
254 | },
255 | "file_extension": ".py",
256 | "mimetype": "text/x-python",
257 | "name": "python",
258 | "nbconvert_exporter": "python",
259 | "pygments_lexer": "ipython3",
260 | "version": "3.8.3"
261 | }
262 | },
263 | "nbformat": 4,
264 | "nbformat_minor": 4
265 | }
266 |
--------------------------------------------------------------------------------
/practice-exercise-04-solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 04 - Solution"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "We have two sets given below. Print the set of elements that are present in either set1 or set2 but not both.\n",
17 | "\n",
18 | "set1 = {1, 2, 3, 4, 5}\n",
19 | "
set2 = {4, 5, 6, 7}\n",
20 | "\n",
21 | "#### Expected Output:\n",
22 | "\n",
23 | "{1, 2, 3, 6, 7}"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": 1,
29 | "metadata": {},
30 | "outputs": [
31 | {
32 | "data": {
33 | "text/plain": [
34 | "{1, 2, 3, 6, 7}"
35 | ]
36 | },
37 | "execution_count": 1,
38 | "metadata": {},
39 | "output_type": "execute_result"
40 | }
41 | ],
42 | "source": [
43 | "set1 = {1, 2, 3, 4, 5}\n",
44 | "set2 = {4, 5, 6, 7}\n",
45 | "\n",
46 | "set1 ^ set2"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "### Question 2:\n",
54 | "\n",
55 | "Print all the keys of the dictionary given below.\n",
56 | "\n",
57 | "myDict = {1:'One', 2:'Two', 3:'Three'}\n",
58 | "\n",
59 | "#### Expected Output:\n",
60 | "\n",
61 | "dict_keys([1, 2, 3])"
62 | ]
63 | },
64 | {
65 | "cell_type": "code",
66 | "execution_count": 2,
67 | "metadata": {},
68 | "outputs": [
69 | {
70 | "data": {
71 | "text/plain": [
72 | "dict_keys([1, 2, 3])"
73 | ]
74 | },
75 | "execution_count": 2,
76 | "metadata": {},
77 | "output_type": "execute_result"
78 | }
79 | ],
80 | "source": [
81 | "myDict = {1:'One', 2:'Two', 3:'Three'}\n",
82 | "\n",
83 | "myDict.keys()"
84 | ]
85 | },
86 | {
87 | "cell_type": "markdown",
88 | "metadata": {},
89 | "source": [
90 | "### Question 3:\n",
91 | "\n",
92 | "We have two sets given below. Print the elements common to both the sets.\n",
93 | "\n",
94 | "set1 = {1, 2, 3, 4, 5}\n",
95 | "
set2 = {4, 5, 6, 7}\n",
96 | "\n",
97 | "#### Expected Output:\n",
98 | "\n",
99 | "{4, 5}"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 3,
105 | "metadata": {},
106 | "outputs": [
107 | {
108 | "data": {
109 | "text/plain": [
110 | "{4, 5}"
111 | ]
112 | },
113 | "execution_count": 3,
114 | "metadata": {},
115 | "output_type": "execute_result"
116 | }
117 | ],
118 | "source": [
119 | "set1 = {1, 2, 3, 4, 5}\n",
120 | "set2 = {4, 5, 6, 7}\n",
121 | "\n",
122 | "set1 & set2"
123 | ]
124 | },
125 | {
126 | "cell_type": "markdown",
127 | "metadata": {},
128 | "source": [
129 | "### Question 4:\n",
130 | "\n",
131 | "We have a dictionary given below. Delete the item with key '3,' and add an item with key '7' and value 'Black.'\n",
132 | "\n",
133 | "color = {1:'Red', 2:'Orange', 3:'White', 4:'Brown', 5:'Yellow'}\n",
134 | "\n",
135 | "#### Expected Output:\n",
136 | "\n",
137 | "{1: 'Red', 2: 'Orange', 4: 'Brown', 5: 'Yellow', 7: 'Black'}"
138 | ]
139 | },
140 | {
141 | "cell_type": "code",
142 | "execution_count": 4,
143 | "metadata": {},
144 | "outputs": [
145 | {
146 | "data": {
147 | "text/plain": [
148 | "{1: 'Red', 2: 'Orange', 4: 'Brown', 5: 'Yellow', 7: 'Black'}"
149 | ]
150 | },
151 | "execution_count": 4,
152 | "metadata": {},
153 | "output_type": "execute_result"
154 | }
155 | ],
156 | "source": [
157 | "color = {1:'Red', 2:'Orange', 3:'White', 4:'Brown', 5:'Yellow'}\n",
158 | "\n",
159 | "del color[3]\n",
160 | "\n",
161 | "color[7] = 'Black'\n",
162 | "\n",
163 | "color"
164 | ]
165 | },
166 | {
167 | "cell_type": "markdown",
168 | "metadata": {},
169 | "source": [
170 | "### Question 5:\n",
171 | "\n",
172 | "We have two sets given below. Check if set1 is a subset of set2.\n",
173 | "\n",
174 | "set1 = {2, 4, 6}\n",
175 | "
set2 = {2, 4, 6, 8, 10}\n",
176 | "\n",
177 | "#### Expected Output:\n",
178 | "\n",
179 | "True"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": 5,
185 | "metadata": {},
186 | "outputs": [
187 | {
188 | "data": {
189 | "text/plain": [
190 | "True"
191 | ]
192 | },
193 | "execution_count": 5,
194 | "metadata": {},
195 | "output_type": "execute_result"
196 | }
197 | ],
198 | "source": [
199 | "set1 = {2, 4, 6}\n",
200 | "set2 = {2, 4, 6, 8, 10}\n",
201 | "\n",
202 | "set1 <= set2"
203 | ]
204 | },
205 | {
206 | "cell_type": "markdown",
207 | "metadata": {},
208 | "source": [
209 | "### Question 6:\n",
210 | "\n",
211 | "Merge the two dictionaries given below.\n",
212 | "\n",
213 | "myDict1 = {1:'One', 2:'Two', 3:'Three'}\n",
214 | "
myDict2 = {4:'Four', 5:'Five', 6:'Six'}\n",
215 | "\n",
216 | "#### Expected Output:\n",
217 | "\n",
218 | "{1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six'}"
219 | ]
220 | },
221 | {
222 | "cell_type": "code",
223 | "execution_count": 6,
224 | "metadata": {},
225 | "outputs": [
226 | {
227 | "data": {
228 | "text/plain": [
229 | "{1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six'}"
230 | ]
231 | },
232 | "execution_count": 6,
233 | "metadata": {},
234 | "output_type": "execute_result"
235 | }
236 | ],
237 | "source": [
238 | "myDict1 = {1:'One', 2:'Two', 3:'Three'}\n",
239 | "myDict2 = {4:'Four', 5:'Five', 6:'Six'}\n",
240 | "\n",
241 | "myDict1.update(myDict2)\n",
242 | "\n",
243 | "myDict1"
244 | ]
245 | },
246 | {
247 | "cell_type": "markdown",
248 | "metadata": {},
249 | "source": [
250 | "### Question 7:\n",
251 | "\n",
252 | "We have two sets given below. Print the elements that are present in set1 but not in set2.\n",
253 | "\n",
254 | "set1 = {2, 3, 4, 5}\n",
255 | "
set2 = {2, 4, 6, 8}\n",
256 | "\n",
257 | "#### Expected Solution:\n",
258 | "\n",
259 | "{3, 5}"
260 | ]
261 | },
262 | {
263 | "cell_type": "code",
264 | "execution_count": 7,
265 | "metadata": {},
266 | "outputs": [
267 | {
268 | "data": {
269 | "text/plain": [
270 | "{3, 5}"
271 | ]
272 | },
273 | "execution_count": 7,
274 | "metadata": {},
275 | "output_type": "execute_result"
276 | }
277 | ],
278 | "source": [
279 | "set1 = {2, 3, 4, 5}\n",
280 | "set2 = {2, 4, 6, 8}\n",
281 | "\n",
282 | "set1 - set2"
283 | ]
284 | },
285 | {
286 | "cell_type": "markdown",
287 | "metadata": {},
288 | "source": [
289 | "### Question 8:\n",
290 | "\n",
291 | "We have a dictionary given below. Copy this dictionary into another dictionary 'replica,' and change the value of the key 103 to 'Sally' in the replica dictionary only. Finally, print both the dictionaries.\n",
292 | "\n",
293 | "student_details = {101:'Judy', 102:'Victor', 103:'Sam'}\n",
294 | "\n",
295 | "#### Expected Output:\n",
296 | "\n",
297 | "{101: 'Judy', 102: 'Victor', 103: 'Sam'}\n",
298 | "
{101: 'Judy', 102: 'Victor', 103: 'Sally'}"
299 | ]
300 | },
301 | {
302 | "cell_type": "code",
303 | "execution_count": 8,
304 | "metadata": {},
305 | "outputs": [
306 | {
307 | "name": "stdout",
308 | "output_type": "stream",
309 | "text": [
310 | "{101: 'Judy', 102: 'Victor', 103: 'Sam'}\n",
311 | "{101: 'Judy', 102: 'Victor', 103: 'Sally'}\n"
312 | ]
313 | }
314 | ],
315 | "source": [
316 | "import copy\n",
317 | "student_details = {101:'Judy', 102:'Victor', 103:'Sam'}\n",
318 | "\n",
319 | "replica = copy.deepcopy(student_details)\n",
320 | "replica[103] = 'Sally'\n",
321 | "\n",
322 | "print(student_details)\n",
323 | "print(replica)"
324 | ]
325 | },
326 | {
327 | "cell_type": "markdown",
328 | "metadata": {},
329 | "source": [
330 | "### Question 9:\n",
331 | "\n",
332 | "Remove all the duplicate items from the tuple given below.\n",
333 | "\n",
334 | "myTuple = ('Red', 'Blue', 'Green', 'Red', 'Orange', 'Green')\n",
335 | "\n",
336 | "#### Expected Output:\n",
337 | "\n",
338 | "{'Blue', 'Green', 'Orange', 'Red'}"
339 | ]
340 | },
341 | {
342 | "cell_type": "code",
343 | "execution_count": 9,
344 | "metadata": {},
345 | "outputs": [
346 | {
347 | "data": {
348 | "text/plain": [
349 | "{'Blue', 'Green', 'Orange', 'Red'}"
350 | ]
351 | },
352 | "execution_count": 9,
353 | "metadata": {},
354 | "output_type": "execute_result"
355 | }
356 | ],
357 | "source": [
358 | "myTuple = ('Red', 'Blue', 'Green', 'Red', 'Orange', 'Green')\n",
359 | "\n",
360 | "mySet = set(myTuple)\n",
361 | "\n",
362 | "mySet"
363 | ]
364 | },
365 | {
366 | "cell_type": "markdown",
367 | "metadata": {},
368 | "source": [
369 | "### Question 10:\n",
370 | "\n",
371 | "Print the number and the cube of that number in a dictionary from 0 to 5.\n",
372 | "\n",
373 | "#### Expected Output:\n",
374 | "\n",
375 | "{0: 0, 1: 1, 2: 8, 3: 27, 4: 64, 5: 125}"
376 | ]
377 | },
378 | {
379 | "cell_type": "code",
380 | "execution_count": 10,
381 | "metadata": {},
382 | "outputs": [
383 | {
384 | "name": "stdout",
385 | "output_type": "stream",
386 | "text": [
387 | "{0: 0, 1: 1, 2: 8, 3: 27, 4: 64, 5: 125}\n"
388 | ]
389 | }
390 | ],
391 | "source": [
392 | "myDict = {x: x**3 for x in range(6)}\n",
393 | "\n",
394 | "print(myDict)"
395 | ]
396 | }
397 | ],
398 | "metadata": {
399 | "kernelspec": {
400 | "display_name": "Python 3",
401 | "language": "python",
402 | "name": "python3"
403 | },
404 | "language_info": {
405 | "codemirror_mode": {
406 | "name": "ipython",
407 | "version": 3
408 | },
409 | "file_extension": ".py",
410 | "mimetype": "text/x-python",
411 | "name": "python",
412 | "nbconvert_exporter": "python",
413 | "pygments_lexer": "ipython3",
414 | "version": "3.8.3"
415 | }
416 | },
417 | "nbformat": 4,
418 | "nbformat_minor": 4
419 | }
420 |
--------------------------------------------------------------------------------
/practice-exercise-04.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 04"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "We have two sets given below. Print the set of elements that are present in either set1 or set2 but not both.\n",
17 | "\n",
18 | "set1 = {1, 2, 3, 4, 5}\n",
19 | "
set2 = {4, 5, 6, 7}\n",
20 | "\n",
21 | "#### Expected Output:\n",
22 | "\n",
23 | "{1, 2, 3, 6, 7}"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": []
32 | },
33 | {
34 | "cell_type": "markdown",
35 | "metadata": {},
36 | "source": [
37 | "### Question 2:\n",
38 | "\n",
39 | "Print all the keys of the dictionary given below.\n",
40 | "\n",
41 | "myDict = {1:'One', 2:'Two', 3:'Three'}\n",
42 | "\n",
43 | "#### Expected Output:\n",
44 | "\n",
45 | "dict_keys([1, 2, 3])"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": null,
51 | "metadata": {},
52 | "outputs": [],
53 | "source": []
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "### Question 3:\n",
60 | "\n",
61 | "We have two sets given below. Print the elements common to both the sets.\n",
62 | "\n",
63 | "set1 = {1, 2, 3, 4, 5}\n",
64 | "
set2 = {4, 5, 6, 7}\n",
65 | "\n",
66 | "#### Expected Output:\n",
67 | "\n",
68 | "{4, 5}"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": null,
74 | "metadata": {},
75 | "outputs": [],
76 | "source": []
77 | },
78 | {
79 | "cell_type": "markdown",
80 | "metadata": {},
81 | "source": [
82 | "### Question 4:\n",
83 | "\n",
84 | "We have a dictionary given below. Delete the item with key '3,' and add an item with key '7' and value 'Black.'\n",
85 | "\n",
86 | "color = {1:'Red', 2:'Orange', 3:'White', 4:'Brown', 5:'Yellow'}\n",
87 | "\n",
88 | "#### Expected Output:\n",
89 | "\n",
90 | "{1: 'Red', 2: 'Orange', 4: 'Brown', 5: 'Yellow', 7: 'Black'}"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": null,
96 | "metadata": {},
97 | "outputs": [],
98 | "source": []
99 | },
100 | {
101 | "cell_type": "markdown",
102 | "metadata": {},
103 | "source": [
104 | "### Question 5:\n",
105 | "\n",
106 | "We have two sets given below. Check if set1 is a subset of set2.\n",
107 | "\n",
108 | "set1 = {2, 4, 6}\n",
109 | "
set2 = {2, 4, 6, 8, 10}\n",
110 | "\n",
111 | "#### Expected Output:\n",
112 | "\n",
113 | "True"
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "execution_count": null,
119 | "metadata": {},
120 | "outputs": [],
121 | "source": []
122 | },
123 | {
124 | "cell_type": "markdown",
125 | "metadata": {},
126 | "source": [
127 | "### Question 6:\n",
128 | "\n",
129 | "Merge the two dictionaries given below.\n",
130 | "\n",
131 | "myDict1 = {1:'One', 2:'Two', 3:'Three'}\n",
132 | "
myDict2 = {4:'Four', 5:'Five', 6:'Six'}\n",
133 | "\n",
134 | "#### Expected Output:\n",
135 | "\n",
136 | "{1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six'}"
137 | ]
138 | },
139 | {
140 | "cell_type": "code",
141 | "execution_count": null,
142 | "metadata": {},
143 | "outputs": [],
144 | "source": []
145 | },
146 | {
147 | "cell_type": "markdown",
148 | "metadata": {},
149 | "source": [
150 | "### Question 7:\n",
151 | "\n",
152 | "We have two sets given below. Print the elements that are present in set1 but not in set2.\n",
153 | "\n",
154 | "set1 = {2, 3, 4, 5}\n",
155 | "
set2 = {2, 4, 6, 8}\n",
156 | "\n",
157 | "#### Expected Solution:\n",
158 | "\n",
159 | "{3, 5}"
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": null,
165 | "metadata": {},
166 | "outputs": [],
167 | "source": []
168 | },
169 | {
170 | "cell_type": "markdown",
171 | "metadata": {},
172 | "source": [
173 | "### Question 8:\n",
174 | "\n",
175 | "We have a dictionary given below. Copy this dictionary into another dictionary 'replica,' and change the value of the key 103 to 'Sally' in the replica dictionary only. Finally, print both the dictionaries.\n",
176 | "\n",
177 | "student_details = {101:'Judy', 102:'Victor', 103:'Sam'}\n",
178 | "\n",
179 | "#### Expected Output:\n",
180 | "\n",
181 | "{101: 'Judy', 102: 'Victor', 103: 'Sam'}\n",
182 | "
{101: 'Judy', 102: 'Victor', 103: 'Sally'}"
183 | ]
184 | },
185 | {
186 | "cell_type": "code",
187 | "execution_count": null,
188 | "metadata": {},
189 | "outputs": [],
190 | "source": []
191 | },
192 | {
193 | "cell_type": "markdown",
194 | "metadata": {},
195 | "source": [
196 | "### Question 9:\n",
197 | "\n",
198 | "Remove all the duplicate items from the tuple given below.\n",
199 | "\n",
200 | "myTuple = ('Red', 'Blue', 'Green', 'Red', 'Orange', 'Green')\n",
201 | "\n",
202 | "#### Expected Output:\n",
203 | "\n",
204 | "{'Blue', 'Green', 'Orange', 'Red'}"
205 | ]
206 | },
207 | {
208 | "cell_type": "code",
209 | "execution_count": null,
210 | "metadata": {},
211 | "outputs": [],
212 | "source": []
213 | },
214 | {
215 | "cell_type": "markdown",
216 | "metadata": {},
217 | "source": [
218 | "### Question 10:\n",
219 | "\n",
220 | "Print the number and the cube of that number in a dictionary from 0 to 5.\n",
221 | "\n",
222 | "#### Expected Output:\n",
223 | "\n",
224 | "{0: 0, 1: 1, 2: 8, 3: 27, 4: 64, 5: 125}"
225 | ]
226 | },
227 | {
228 | "cell_type": "code",
229 | "execution_count": null,
230 | "metadata": {},
231 | "outputs": [],
232 | "source": []
233 | }
234 | ],
235 | "metadata": {
236 | "kernelspec": {
237 | "display_name": "Python 3",
238 | "language": "python",
239 | "name": "python3"
240 | },
241 | "language_info": {
242 | "codemirror_mode": {
243 | "name": "ipython",
244 | "version": 3
245 | },
246 | "file_extension": ".py",
247 | "mimetype": "text/x-python",
248 | "name": "python",
249 | "nbconvert_exporter": "python",
250 | "pygments_lexer": "ipython3",
251 | "version": "3.8.3"
252 | }
253 | },
254 | "nbformat": 4,
255 | "nbformat_minor": 4
256 | }
257 |
--------------------------------------------------------------------------------
/practice-exercise-05-solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 05 - Solution"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Write a Python program to display the current date and time.\n",
17 | "\n",
18 | "#### Expected Output:\n",
19 | "\n",
20 | "Current date and time: 2020-10-17 16:02:50.583157"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": 1,
26 | "metadata": {},
27 | "outputs": [
28 | {
29 | "name": "stdout",
30 | "output_type": "stream",
31 | "text": [
32 | "Current date and time: 2020-10-17 16:02:50.583157\n"
33 | ]
34 | }
35 | ],
36 | "source": [
37 | "from datetime import datetime\n",
38 | "\n",
39 | "print(\"Current date and time: \" , datetime.now())"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {},
45 | "source": [
46 | "### Question 2:\n",
47 | "\n",
48 | "Print the month and minutes from the datetime object given below.\n",
49 | "\n",
50 | "dob_time = datetime(1995, 6, 14, 10, 37, 50, 0) \n",
51 | "\n",
52 | "#### Expected Output:\n",
53 | "\n",
54 | "Month: 6\n",
55 | "
Minutes: 37"
56 | ]
57 | },
58 | {
59 | "cell_type": "code",
60 | "execution_count": 2,
61 | "metadata": {},
62 | "outputs": [
63 | {
64 | "name": "stdout",
65 | "output_type": "stream",
66 | "text": [
67 | "Month: 6\n",
68 | "Minutes: 37\n"
69 | ]
70 | }
71 | ],
72 | "source": [
73 | "from datetime import datetime\n",
74 | "\n",
75 | "dob_time = datetime(1995, 6, 14, 10, 37, 50, 0) \n",
76 | "\n",
77 | "print('Month:', dob_time.month)\n",
78 | "print('Minutes:', dob_time.minute)"
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "### Question 3:\n",
86 | "\n",
87 | "Print the date in the format given below.\n",
88 | "\n",
89 | "Day_number Month_name(abbr.) Year - Day_name(abbr.)\n",
90 | "\n",
91 | "given_date = datetime(2011, 10, 12)\n",
92 | "\n",
93 | "#### Expected Output:\n",
94 | "\n",
95 | "12 Oct 2011 - Wed"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": 3,
101 | "metadata": {},
102 | "outputs": [
103 | {
104 | "name": "stdout",
105 | "output_type": "stream",
106 | "text": [
107 | "12 Oct 2011 - Wed\n"
108 | ]
109 | }
110 | ],
111 | "source": [
112 | "from datetime import datetime\n",
113 | "\n",
114 | "given_date = datetime(2011, 10, 12)\n",
115 | "\n",
116 | "print(given_date.strftime('%d %b %Y - %a'))"
117 | ]
118 | },
119 | {
120 | "cell_type": "markdown",
121 | "metadata": {},
122 | "source": [
123 | "### Question 4:\n",
124 | "\n",
125 | "Write a Python program to find the day of the week of a given date?\n",
126 | "\n",
127 | "given_date = datetime(2008, 5, 15)\n",
128 | "\n",
129 | "#### Expected Output:\n",
130 | "\n",
131 | "Thursday"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": 4,
137 | "metadata": {},
138 | "outputs": [
139 | {
140 | "name": "stdout",
141 | "output_type": "stream",
142 | "text": [
143 | "Thursday\n"
144 | ]
145 | }
146 | ],
147 | "source": [
148 | "from datetime import datetime\n",
149 | "\n",
150 | "given_date = datetime(2008, 5, 15)\n",
151 | "\n",
152 | "print(given_date.strftime('%A'))"
153 | ]
154 | },
155 | {
156 | "cell_type": "markdown",
157 | "metadata": {},
158 | "source": [
159 | "### Question 5:\n",
160 | "\n",
161 | "Write a Python program to add 2 days and 10 hours to a given date.\n",
162 | "\n",
163 | "given_date = datetime(2016, 2, 27, 11, 30, 0)\n",
164 | "\n",
165 | "#### Expected Output:\n",
166 | "\n",
167 | "2016-02-29 21:30:00"
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "execution_count": 5,
173 | "metadata": {},
174 | "outputs": [
175 | {
176 | "name": "stdout",
177 | "output_type": "stream",
178 | "text": [
179 | "2016-02-29 21:30:00\n"
180 | ]
181 | }
182 | ],
183 | "source": [
184 | "from datetime import datetime, timedelta\n",
185 | "\n",
186 | "given_date = datetime(2016, 2, 27, 11, 30, 0)\n",
187 | "\n",
188 | "result = given_date + timedelta(days = 2, hours = 10)\n",
189 | "\n",
190 | "print(result)"
191 | ]
192 | },
193 | {
194 | "cell_type": "markdown",
195 | "metadata": {},
196 | "source": [
197 | "### Question 6:\n",
198 | "\n",
199 | "Write a Python program to calculate the number of days between two given dates.\n",
200 | "\n",
201 | "date_1 = datetime(2020, 7, 21).date()\n",
202 | "
date_2 = datetime(2020, 3, 10).date()\n",
203 | "\n",
204 | "#### Expected Output:\n",
205 | "\n",
206 | "133 days, 0:00:00"
207 | ]
208 | },
209 | {
210 | "cell_type": "code",
211 | "execution_count": 6,
212 | "metadata": {},
213 | "outputs": [
214 | {
215 | "name": "stdout",
216 | "output_type": "stream",
217 | "text": [
218 | "133 days, 0:00:00\n"
219 | ]
220 | }
221 | ],
222 | "source": [
223 | "from datetime import datetime\n",
224 | "\n",
225 | "date_1 = datetime(2020, 7, 21).date()\n",
226 | "date_2 = datetime(2020, 3, 10).date()\n",
227 | "\n",
228 | "print(date_1 - date_2)"
229 | ]
230 | },
231 | {
232 | "cell_type": "markdown",
233 | "metadata": {},
234 | "source": [
235 | "### Question 7:\n",
236 | "\n",
237 | "Write a Python program to convert a string to datetime.\n",
238 | "\n",
239 | "myString = Oct 12 2002 12:45 PM\n",
240 | "\n",
241 | "#### Expected Output:\n",
242 | "\n",
243 | "2002-10-12 12:45:00"
244 | ]
245 | },
246 | {
247 | "cell_type": "code",
248 | "execution_count": 7,
249 | "metadata": {},
250 | "outputs": [
251 | {
252 | "name": "stdout",
253 | "output_type": "stream",
254 | "text": [
255 | "2002-10-12 12:45:00\n"
256 | ]
257 | }
258 | ],
259 | "source": [
260 | "from datetime import datetime\n",
261 | "\n",
262 | "myString = 'Oct 12 2002 12:45 PM'\n",
263 | " \n",
264 | "date_object = datetime.strptime(myString, '%b %d %Y %I:%M %p')\n",
265 | "print(date_object)"
266 | ]
267 | },
268 | {
269 | "cell_type": "markdown",
270 | "metadata": {},
271 | "source": [
272 | "### Question 8:\n",
273 | "\n",
274 | "Print the date, day name, and time in the format given below.\n",
275 | "\n",
276 | "Day_number-Month_number-Year - Day_name - Hours:Minutes:Seconds\n",
277 | "\n",
278 | "dob_time = datetime(1995, 6, 14, 10, 37, 50, 0) \n",
279 | "\n",
280 | "#### Expected Output:\n",
281 | "\n",
282 | "14-06-1995 - Wed - 10:37:50"
283 | ]
284 | },
285 | {
286 | "cell_type": "code",
287 | "execution_count": 8,
288 | "metadata": {},
289 | "outputs": [
290 | {
291 | "name": "stdout",
292 | "output_type": "stream",
293 | "text": [
294 | "14-06-1995 - Wednesday - 10:37:50\n"
295 | ]
296 | }
297 | ],
298 | "source": [
299 | "from datetime import datetime\n",
300 | "\n",
301 | "dob_time = datetime(1995, 6, 14, 10, 37, 50, 0) \n",
302 | "\n",
303 | "print(dob_time.strftime('%d-%m-%Y - %A - %H:%M:%S'))"
304 | ]
305 | },
306 | {
307 | "cell_type": "markdown",
308 | "metadata": {},
309 | "source": [
310 | "### Question 9:\n",
311 | "\n",
312 | "Write a Python program to print yesterdays' date.\n",
313 | "\n",
314 | "#### Expected Output:\n",
315 | "\n",
316 | "Yesterday : 2020-10-16"
317 | ]
318 | },
319 | {
320 | "cell_type": "code",
321 | "execution_count": 9,
322 | "metadata": {},
323 | "outputs": [
324 | {
325 | "name": "stdout",
326 | "output_type": "stream",
327 | "text": [
328 | "Yesterday : 2020-10-16\n"
329 | ]
330 | }
331 | ],
332 | "source": [
333 | "import datetime \n",
334 | "\n",
335 | "yesterday = datetime.date.today() - datetime.timedelta(days = 1)\n",
336 | "\n",
337 | "print('Yesterday : ',yesterday)"
338 | ]
339 | },
340 | {
341 | "cell_type": "markdown",
342 | "metadata": {},
343 | "source": [
344 | "### Question 10:\n",
345 | "\n",
346 | "Write a Python program to print the next 5 days from the date given below.\n",
347 | "\n",
348 | "given_date = datetime(2020, 10, 17)\n",
349 | "\n",
350 | "#### Expected Output:\n",
351 | "\n",
352 | "2020-10-18 00:00:00\n",
353 | "
2020-10-19 00:00:00\n",
354 | "
2020-10-20 00:00:00\n",
355 | "
2020-10-21 00:00:00\n",
356 | "
2020-10-22 00:00:00"
357 | ]
358 | },
359 | {
360 | "cell_type": "code",
361 | "execution_count": 10,
362 | "metadata": {},
363 | "outputs": [
364 | {
365 | "name": "stdout",
366 | "output_type": "stream",
367 | "text": [
368 | "2020-10-18 00:00:00\n",
369 | "2020-10-19 00:00:00\n",
370 | "2020-10-20 00:00:00\n",
371 | "2020-10-21 00:00:00\n",
372 | "2020-10-22 00:00:00\n"
373 | ]
374 | }
375 | ],
376 | "source": [
377 | "from datetime import datetime, timedelta\n",
378 | "\n",
379 | "given_date = datetime(2020, 10, 17)\n",
380 | "\n",
381 | "for x in range(1, 6):\n",
382 | " print(given_date + timedelta(days=x))"
383 | ]
384 | }
385 | ],
386 | "metadata": {
387 | "kernelspec": {
388 | "display_name": "Python 3",
389 | "language": "python",
390 | "name": "python3"
391 | },
392 | "language_info": {
393 | "codemirror_mode": {
394 | "name": "ipython",
395 | "version": 3
396 | },
397 | "file_extension": ".py",
398 | "mimetype": "text/x-python",
399 | "name": "python",
400 | "nbconvert_exporter": "python",
401 | "pygments_lexer": "ipython3",
402 | "version": "3.8.3"
403 | }
404 | },
405 | "nbformat": 4,
406 | "nbformat_minor": 4
407 | }
408 |
--------------------------------------------------------------------------------
/practice-exercise-05.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 05"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Write a Python program to display the current date and time.\n",
17 | "\n",
18 | "#### Expected Output:\n",
19 | "\n",
20 | "Current date and time: 2020-10-17 16:02:50.583157"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": null,
26 | "metadata": {},
27 | "outputs": [],
28 | "source": []
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {},
33 | "source": [
34 | "### Question 2:\n",
35 | "\n",
36 | "Print the month and minutes from the datetime object given below.\n",
37 | "\n",
38 | "dob_time = datetime(1995, 6, 14, 10, 37, 50, 0) \n",
39 | "\n",
40 | "#### Expected Output:\n",
41 | "\n",
42 | "Month: 6\n",
43 | "
Minutes: 37"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": null,
49 | "metadata": {},
50 | "outputs": [],
51 | "source": []
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "metadata": {},
56 | "source": [
57 | "### Question 3:\n",
58 | "\n",
59 | "Print the date in the format given below.\n",
60 | "\n",
61 | "Day_number Month_name(abbr.) Year - Day_name(abbr.)\n",
62 | "\n",
63 | "given_date = datetime(2011, 10, 12)\n",
64 | "\n",
65 | "#### Expected Output:\n",
66 | "\n",
67 | "12 Oct 2011 - Wed"
68 | ]
69 | },
70 | {
71 | "cell_type": "code",
72 | "execution_count": null,
73 | "metadata": {},
74 | "outputs": [],
75 | "source": []
76 | },
77 | {
78 | "cell_type": "markdown",
79 | "metadata": {},
80 | "source": [
81 | "### Question 4:\n",
82 | "\n",
83 | "Write a Python program to find the day of the week of a given date?\n",
84 | "\n",
85 | "given_date = datetime(2008, 5, 15)\n",
86 | "\n",
87 | "#### Expected Output:\n",
88 | "\n",
89 | "Thursday"
90 | ]
91 | },
92 | {
93 | "cell_type": "code",
94 | "execution_count": null,
95 | "metadata": {},
96 | "outputs": [],
97 | "source": []
98 | },
99 | {
100 | "cell_type": "markdown",
101 | "metadata": {},
102 | "source": [
103 | "### Question 5:\n",
104 | "\n",
105 | "Write a Python program to add 2 days and 10 hours to a given date.\n",
106 | "\n",
107 | "given_date = datetime(2016, 2, 27, 11, 30, 0)\n",
108 | "\n",
109 | "#### Expected Output:\n",
110 | "\n",
111 | "2016-02-29 21:30:00"
112 | ]
113 | },
114 | {
115 | "cell_type": "code",
116 | "execution_count": null,
117 | "metadata": {},
118 | "outputs": [],
119 | "source": []
120 | },
121 | {
122 | "cell_type": "markdown",
123 | "metadata": {},
124 | "source": [
125 | "### Question 6:\n",
126 | "\n",
127 | "Write a Python program to calculate the number of days between two given dates.\n",
128 | "\n",
129 | "date_1 = datetime(2020, 7, 21).date()\n",
130 | "
date_2 = datetime(2020, 3, 10).date()\n",
131 | "\n",
132 | "#### Expected Output:\n",
133 | "\n",
134 | "133 days, 0:00:00"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": null,
140 | "metadata": {},
141 | "outputs": [],
142 | "source": []
143 | },
144 | {
145 | "cell_type": "markdown",
146 | "metadata": {},
147 | "source": [
148 | "### Question 7:\n",
149 | "\n",
150 | "Write a Python program to convert a string to datetime.\n",
151 | "\n",
152 | "myString = Oct 12 2002 12:45 PM\n",
153 | "\n",
154 | "#### Expected Output:\n",
155 | "\n",
156 | "2002-10-12 12:45:00"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": null,
162 | "metadata": {},
163 | "outputs": [],
164 | "source": []
165 | },
166 | {
167 | "cell_type": "markdown",
168 | "metadata": {},
169 | "source": [
170 | "### Question 8:\n",
171 | "\n",
172 | "Print the date, day name, and time in the format given below.\n",
173 | "\n",
174 | "Day_number-Month_number-Year - Day_name - Hours:Minutes:Seconds\n",
175 | "\n",
176 | "dob_time = datetime(1995, 6, 14, 10, 37, 50, 0) \n",
177 | "\n",
178 | "#### Expected Output:\n",
179 | "\n",
180 | "14-06-1995 - Wed - 10:37:50"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": null,
186 | "metadata": {},
187 | "outputs": [],
188 | "source": []
189 | },
190 | {
191 | "cell_type": "markdown",
192 | "metadata": {},
193 | "source": [
194 | "### Question 9:\n",
195 | "\n",
196 | "Write a Python program to print yesterdays' date.\n",
197 | "\n",
198 | "#### Expected Output:\n",
199 | "\n",
200 | "Yesterday : 2020-10-16"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": null,
206 | "metadata": {},
207 | "outputs": [],
208 | "source": []
209 | },
210 | {
211 | "cell_type": "markdown",
212 | "metadata": {},
213 | "source": [
214 | "### Question 10:\n",
215 | "\n",
216 | "Write a Python program to print the next 5 days from the date given below.\n",
217 | "\n",
218 | "given_date = datetime(2020, 10, 17)\n",
219 | "\n",
220 | "#### Expected Output:\n",
221 | "\n",
222 | "2020-10-18 00:00:00\n",
223 | "
2020-10-19 00:00:00\n",
224 | "
2020-10-20 00:00:00\n",
225 | "
2020-10-21 00:00:00\n",
226 | "
2020-10-22 00:00:00"
227 | ]
228 | },
229 | {
230 | "cell_type": "code",
231 | "execution_count": null,
232 | "metadata": {},
233 | "outputs": [],
234 | "source": []
235 | }
236 | ],
237 | "metadata": {
238 | "kernelspec": {
239 | "display_name": "Python 3",
240 | "language": "python",
241 | "name": "python3"
242 | },
243 | "language_info": {
244 | "codemirror_mode": {
245 | "name": "ipython",
246 | "version": 3
247 | },
248 | "file_extension": ".py",
249 | "mimetype": "text/x-python",
250 | "name": "python",
251 | "nbconvert_exporter": "python",
252 | "pygments_lexer": "ipython3",
253 | "version": "3.8.3"
254 | }
255 | },
256 | "nbformat": 4,
257 | "nbformat_minor": 4
258 | }
259 |
--------------------------------------------------------------------------------
/practice-exercise-06-solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 06 - Solution"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Write a Python function to check whether the number given below is present in between 5 to 10 (both included) or not.\n",
17 | "\n",
18 | "num = 7\n",
19 | "\n",
20 | "#### Expected Output:\n",
21 | "\n",
22 | "7 is present between 5-10."
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": 1,
28 | "metadata": {},
29 | "outputs": [
30 | {
31 | "name": "stdout",
32 | "output_type": "stream",
33 | "text": [
34 | "7 is present between 5-10.\n"
35 | ]
36 | }
37 | ],
38 | "source": [
39 | "num = 7\n",
40 | "\n",
41 | "def num_range_check(x):\n",
42 | " \n",
43 | " if x in range(5, 11):\n",
44 | " print(f'{str(x)} is present between 5-10.')\n",
45 | " else :\n",
46 | " print(f'{str(x)} is not present between 5-10.')\n",
47 | " \n",
48 | "num_range_check(num)"
49 | ]
50 | },
51 | {
52 | "cell_type": "markdown",
53 | "metadata": {},
54 | "source": [
55 | "### Question 2:\n",
56 | "\n",
57 | "Write a Python function that accepts two arguments and calculates the addition and subtraction of it. Also, print both the arithmetic results in a single return call. \n",
58 | "\n",
59 | "#### Expected Output:\n",
60 | "\n",
61 | "(Addition, Substraction) : (50, 30)"
62 | ]
63 | },
64 | {
65 | "cell_type": "code",
66 | "execution_count": 2,
67 | "metadata": {},
68 | "outputs": [
69 | {
70 | "name": "stdout",
71 | "output_type": "stream",
72 | "text": [
73 | "(Addition, Substraction) : (50, 30)\n"
74 | ]
75 | }
76 | ],
77 | "source": [
78 | "def arithematic_operations(x, y):\n",
79 | " return x + y, x - y\n",
80 | "\n",
81 | "result = arithematic_operations(40, 10)\n",
82 | "print(\"(Addition, Substraction) : \", result)"
83 | ]
84 | },
85 | {
86 | "cell_type": "markdown",
87 | "metadata": {},
88 | "source": [
89 | "### Question 3:\n",
90 | "\n",
91 | "Create a function in Python that displays the name and results of a candidate. The function should accept the candidate's name and his/her results as \"Pass/Fail.\" If the result is missing in the function call, show it as \"Pass.\"\n",
92 | "\n",
93 | "#### Expected Output:\n",
94 | "\n",
95 | "Sam is Pass.\n",
96 | "
Judy is Fail."
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 3,
102 | "metadata": {},
103 | "outputs": [
104 | {
105 | "name": "stdout",
106 | "output_type": "stream",
107 | "text": [
108 | "Sam is Pass.\n",
109 | "Judy is Fail.\n"
110 | ]
111 | }
112 | ],
113 | "source": [
114 | "def exam_results(name, results = \"Pass\"):\n",
115 | " print(f'{name} is {results}.')\n",
116 | " \n",
117 | "exam_results(\"Sam\")\n",
118 | "exam_results(\"Judy\", \"Fail\")"
119 | ]
120 | },
121 | {
122 | "cell_type": "markdown",
123 | "metadata": {},
124 | "source": [
125 | "### Question 4:\n",
126 | "\n",
127 | "We have a list of numbers given below. Write a Python function to print all the odd-indexed items from the list.\n",
128 | "\n",
129 | "n = [2, 3, 5, 6, 8, 9]\n",
130 | "\n",
131 | "#### Expected Output:\n",
132 | "\n",
133 | "(3, 6, 9)"
134 | ]
135 | },
136 | {
137 | "cell_type": "code",
138 | "execution_count": 4,
139 | "metadata": {},
140 | "outputs": [
141 | {
142 | "name": "stdout",
143 | "output_type": "stream",
144 | "text": [
145 | "(3, 6, 9)\n"
146 | ]
147 | }
148 | ],
149 | "source": [
150 | "n = [2, 3, 5, 6, 8, 9]\n",
151 | "\n",
152 | "def odd_indexed(*num): \n",
153 | " print(num[1::2])\n",
154 | "\n",
155 | "odd_indexed(*n)"
156 | ]
157 | },
158 | {
159 | "cell_type": "markdown",
160 | "metadata": {},
161 | "source": [
162 | "### Question 5:\n",
163 | "\n",
164 | "We have the names of six countries given below. Write a Python function to print all the countries that start with the letter 'A.'\n",
165 | "\n",
166 | "'Austraiia', 'India', 'Austria', 'America', 'Russia', 'Iran'\n",
167 | "\n",
168 | "#### Expected Output:\n",
169 | "\n",
170 | "['Austraiia', 'Austria', 'America']"
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": 5,
176 | "metadata": {},
177 | "outputs": [
178 | {
179 | "name": "stdout",
180 | "output_type": "stream",
181 | "text": [
182 | "['Austraiia', 'Austria', 'America']\n"
183 | ]
184 | }
185 | ],
186 | "source": [
187 | "result = []\n",
188 | "\n",
189 | "def alphabet_check(*countries):\n",
190 | " \n",
191 | " for x in countries:\n",
192 | " \n",
193 | " if x.startswith('A'):\n",
194 | " result.append(x)\n",
195 | " else:\n",
196 | " pass\n",
197 | " \n",
198 | "alphabet_check('Austraiia', 'India', 'Austria', 'America', 'Russia', 'Iran')\n",
199 | "print(result)"
200 | ]
201 | },
202 | {
203 | "cell_type": "markdown",
204 | "metadata": {},
205 | "source": [
206 | "### Question 6:\n",
207 | "\n",
208 | "A list of tuples is given below, containing the candidate's name and their heights(in cm). Sort this list using Lambda functions according to the heights of the candidates.\n",
209 | "\n",
210 | "candidate_details = [('Harry', 168), ('Jhonny', 160), ('Brad', 178), ('Chris', 172)]\n",
211 | "\n",
212 | "#### Expected Output:\n",
213 | "\n",
214 | "[('Jhonny', 160), ('Harry', 168), ('Chris', 172), ('Brad', 178)]"
215 | ]
216 | },
217 | {
218 | "cell_type": "code",
219 | "execution_count": 6,
220 | "metadata": {},
221 | "outputs": [
222 | {
223 | "name": "stdout",
224 | "output_type": "stream",
225 | "text": [
226 | "[('Jhonny', 160), ('Harry', 168), ('Chris', 172), ('Brad', 178)]\n"
227 | ]
228 | }
229 | ],
230 | "source": [
231 | "candidate_details = [('Harry', 168), ('Jhonny', 160), ('Brad', 178), ('Chris', 172)]\n",
232 | "\n",
233 | "candidate_details.sort(key = lambda x: x[1])\n",
234 | "\n",
235 | "print(candidate_details)"
236 | ]
237 | },
238 | {
239 | "cell_type": "markdown",
240 | "metadata": {},
241 | "source": [
242 | "### Question 7:\n",
243 | "\n",
244 | "Write a Python function to find 'Mall' from the dictionary 'map_details' given below.\n",
245 | "\n",
246 | "map_details = {101:'Park', 102:'Zoo', 103:'Mall'}\n",
247 | "\n",
248 | "#### Expected Output:\n",
249 | "\n",
250 | "103"
251 | ]
252 | },
253 | {
254 | "cell_type": "code",
255 | "execution_count": 7,
256 | "metadata": {},
257 | "outputs": [
258 | {
259 | "name": "stdout",
260 | "output_type": "stream",
261 | "text": [
262 | "103\n"
263 | ]
264 | }
265 | ],
266 | "source": [
267 | "map_details = {'Park': 101, 'Zoo':102, 'Mall':103}\n",
268 | "\n",
269 | "def find(**dict_1):\n",
270 | " print(dict_1['Mall'])\n",
271 | " \n",
272 | "find(**map_details)"
273 | ]
274 | },
275 | {
276 | "cell_type": "markdown",
277 | "metadata": {},
278 | "source": [
279 | "### Question 8:\n",
280 | "\n",
281 | "Write a Python program to add the three lists given below using Python map and Lambda function.\n",
282 | "\n",
283 | "list_1 = [1, 5, 8]\n",
284 | "
list_2 = [3, 2, 5]\n",
285 | "
list_3 = [2, 3, 6]\n",
286 | "\n",
287 | "#### Expected Output:\n",
288 | "\n",
289 | "Resultant List: [6, 10, 19]"
290 | ]
291 | },
292 | {
293 | "cell_type": "code",
294 | "execution_count": 8,
295 | "metadata": {},
296 | "outputs": [
297 | {
298 | "name": "stdout",
299 | "output_type": "stream",
300 | "text": [
301 | "Resultant List: [6, 10, 19]\n"
302 | ]
303 | }
304 | ],
305 | "source": [
306 | "list_1 = [1, 5, 8]\n",
307 | "list_2 = [3, 2, 5]\n",
308 | "list_3 = [2, 3, 6]\n",
309 | "\n",
310 | "result = map(lambda x, y, z: x + y + z, list_1, list_2, list_3) \n",
311 | "\n",
312 | "print('Resultant List:', list(result))"
313 | ]
314 | },
315 | {
316 | "cell_type": "markdown",
317 | "metadata": {},
318 | "source": [
319 | "### Question 9:\n",
320 | "\n",
321 | "We have the marks of a student in each subject given below. Write a Python function that takes two parameters 'name' and 'subjects_marks.' Finally, print the student's name and all the subjects in which his/her marks are above 60. If the 'name' is not provided, print 'None.' \n",
322 | "\n",
323 | "Mathematics = 80, Physics = 58, Chemistry = 62, English = 72, Biology = 50\n",
324 | "\n",
325 | "#### Expected Output:\n",
326 | "\n",
327 | "Name: Brandon - Subjects: {'Chemistry', 'English', 'Mathematics'}"
328 | ]
329 | },
330 | {
331 | "cell_type": "code",
332 | "execution_count": 9,
333 | "metadata": {},
334 | "outputs": [
335 | {
336 | "name": "stdout",
337 | "output_type": "stream",
338 | "text": [
339 | "Name: Brandon - Subjects: {'Chemistry', 'English', 'Mathematics'}\n"
340 | ]
341 | }
342 | ],
343 | "source": [
344 | "def marks_check(name = None, **subject_marks):\n",
345 | " res = {subject_name for (subject_name, marks) in subject_marks.items() if marks > 60}\n",
346 | " print(\"Name: \", name,\"- Subjects: \", res)\n",
347 | " \n",
348 | "marks_check(name = 'Brandon', Mathematics = 80, Physics = 58, Chemistry = 62, English = 72, Biology = 50)"
349 | ]
350 | },
351 | {
352 | "cell_type": "markdown",
353 | "metadata": {},
354 | "source": [
355 | "### Question 10:\n",
356 | "\n",
357 | "Using Lambda function, extract and print the year from the datetime object given below.\n",
358 | "\n",
359 | "given_date = datetime(2008, 6, 12, 10, 30, 0)\n",
360 | "\n",
361 | "#### Expected Output:\n",
362 | "\n",
363 | "2008"
364 | ]
365 | },
366 | {
367 | "cell_type": "code",
368 | "execution_count": 10,
369 | "metadata": {},
370 | "outputs": [
371 | {
372 | "name": "stdout",
373 | "output_type": "stream",
374 | "text": [
375 | "2008\n"
376 | ]
377 | }
378 | ],
379 | "source": [
380 | "from datetime import datetime\n",
381 | "\n",
382 | "given_date = datetime(2008, 6, 12, 10, 30, 0)\n",
383 | "\n",
384 | "year = lambda x : x.year\n",
385 | "\n",
386 | "print(year(given_date))"
387 | ]
388 | }
389 | ],
390 | "metadata": {
391 | "kernelspec": {
392 | "display_name": "Python 3",
393 | "language": "python",
394 | "name": "python3"
395 | },
396 | "language_info": {
397 | "codemirror_mode": {
398 | "name": "ipython",
399 | "version": 3
400 | },
401 | "file_extension": ".py",
402 | "mimetype": "text/x-python",
403 | "name": "python",
404 | "nbconvert_exporter": "python",
405 | "pygments_lexer": "ipython3",
406 | "version": "3.8.3"
407 | }
408 | },
409 | "nbformat": 4,
410 | "nbformat_minor": 4
411 | }
412 |
--------------------------------------------------------------------------------
/practice-exercise-06.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 06"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Write a Python function to check whether the number given below is present in between 5 to 10 (both included) or not.\n",
17 | "\n",
18 | "num = 7\n",
19 | "\n",
20 | "#### Expected Output:\n",
21 | "\n",
22 | "7 is present between 5-10."
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": null,
28 | "metadata": {},
29 | "outputs": [],
30 | "source": []
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "### Question 2:\n",
37 | "\n",
38 | "Write a Python function that accepts two arguments and calculates the addition and subtraction of it. Also, print both the arithmetic results in a single return call. \n",
39 | "\n",
40 | "#### Expected Output:\n",
41 | "\n",
42 | "(Addition, Substraction) : (50, 30)"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": null,
48 | "metadata": {},
49 | "outputs": [],
50 | "source": []
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | "### Question 3:\n",
57 | "\n",
58 | "Create a function in Python that displays the name and results of a candidate. The function should accept the candidate's name and his/her results as \"Pass/Fail.\" If the result is missing in the function call, show it as \"Pass.\"\n",
59 | "\n",
60 | "#### Expected Output:\n",
61 | "\n",
62 | "Sam is Pass.\n",
63 | "
Judy is Fail."
64 | ]
65 | },
66 | {
67 | "cell_type": "code",
68 | "execution_count": null,
69 | "metadata": {},
70 | "outputs": [],
71 | "source": []
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "### Question 4:\n",
78 | "\n",
79 | "We have a list of numbers given below. Write a Python function to print all the odd-indexed items from the list.\n",
80 | "\n",
81 | "n = [2, 3, 5, 6, 8, 9]\n",
82 | "\n",
83 | "#### Expected Output:\n",
84 | "\n",
85 | "(3, 6, 9)"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": null,
91 | "metadata": {},
92 | "outputs": [],
93 | "source": []
94 | },
95 | {
96 | "cell_type": "markdown",
97 | "metadata": {},
98 | "source": [
99 | "### Question 5:\n",
100 | "\n",
101 | "We have the names of six countries given below. Write a Python function to print all the countries that start with the letter 'A.'\n",
102 | "\n",
103 | "'Austraiia', 'India', 'Austria', 'America', 'Russia', 'Iran'\n",
104 | "\n",
105 | "#### Expected Output:\n",
106 | "\n",
107 | "['Austraiia', 'Austria', 'America']"
108 | ]
109 | },
110 | {
111 | "cell_type": "code",
112 | "execution_count": null,
113 | "metadata": {},
114 | "outputs": [],
115 | "source": []
116 | },
117 | {
118 | "cell_type": "markdown",
119 | "metadata": {},
120 | "source": [
121 | "### Question 6:\n",
122 | "\n",
123 | "A list of tuples is given below, containing the candidate's name and their heights(in cm). Sort this list using Lambda functions according to the heights of the candidates.\n",
124 | "\n",
125 | "candidate_details = [('Harry', 168), ('Jhonny', 160), ('Brad', 178), ('Chris', 172)]\n",
126 | "\n",
127 | "#### Expected Output:\n",
128 | "\n",
129 | "[('Jhonny', 160), ('Harry', 168), ('Chris', 172), ('Brad', 178)]"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": null,
135 | "metadata": {},
136 | "outputs": [],
137 | "source": []
138 | },
139 | {
140 | "cell_type": "markdown",
141 | "metadata": {},
142 | "source": [
143 | "### Question 7:\n",
144 | "\n",
145 | "Write a Python function to find 'Mall' from the dictionary 'map_details' given below.\n",
146 | "\n",
147 | "map_details = {101:'Park', 102:'Zoo', 103:'Mall'}\n",
148 | "\n",
149 | "#### Expected Output:\n",
150 | "\n",
151 | "103"
152 | ]
153 | },
154 | {
155 | "cell_type": "code",
156 | "execution_count": null,
157 | "metadata": {},
158 | "outputs": [],
159 | "source": []
160 | },
161 | {
162 | "cell_type": "markdown",
163 | "metadata": {},
164 | "source": [
165 | "### Question 8:\n",
166 | "\n",
167 | "Write a Python program to add the three lists given below using Python map and Lambda function.\n",
168 | "\n",
169 | "list_1 = [1, 5, 8]\n",
170 | "
list_2 = [3, 2, 5]\n",
171 | "
list_3 = [2, 3, 6]\n",
172 | "\n",
173 | "#### Expected Output:\n",
174 | "\n",
175 | "Resultant List: [6, 10, 19]"
176 | ]
177 | },
178 | {
179 | "cell_type": "code",
180 | "execution_count": null,
181 | "metadata": {},
182 | "outputs": [],
183 | "source": []
184 | },
185 | {
186 | "cell_type": "markdown",
187 | "metadata": {},
188 | "source": [
189 | "### Question 9:\n",
190 | "\n",
191 | "We have the marks of a student in each subject given below. Write a Python function that takes two parameters 'name' and 'subjects_marks.' Finally, print the student's name and all the subjects in which his/her marks are above 60. If the 'name' is not provided, print 'None.' \n",
192 | "\n",
193 | "Mathematics = 80, Physics = 58, Chemistry = 62, English = 72, Biology = 50\n",
194 | "\n",
195 | "#### Expected Output:\n",
196 | "\n",
197 | "Name: Brandon - Subjects: {'Chemistry', 'English', 'Mathematics'}"
198 | ]
199 | },
200 | {
201 | "cell_type": "code",
202 | "execution_count": null,
203 | "metadata": {},
204 | "outputs": [],
205 | "source": []
206 | },
207 | {
208 | "cell_type": "markdown",
209 | "metadata": {},
210 | "source": [
211 | "### Question 10:\n",
212 | "\n",
213 | "Using Lambda function, extract and print the year from the datetime object given below.\n",
214 | "\n",
215 | "given_date = datetime(2008, 6, 12, 10, 30, 0)\n",
216 | "\n",
217 | "#### Expected Output:\n",
218 | "\n",
219 | "2008"
220 | ]
221 | },
222 | {
223 | "cell_type": "code",
224 | "execution_count": null,
225 | "metadata": {},
226 | "outputs": [],
227 | "source": []
228 | }
229 | ],
230 | "metadata": {
231 | "kernelspec": {
232 | "display_name": "Python 3",
233 | "language": "python",
234 | "name": "python3"
235 | },
236 | "language_info": {
237 | "codemirror_mode": {
238 | "name": "ipython",
239 | "version": 3
240 | },
241 | "file_extension": ".py",
242 | "mimetype": "text/x-python",
243 | "name": "python",
244 | "nbconvert_exporter": "python",
245 | "pygments_lexer": "ipython3",
246 | "version": "3.8.3"
247 | }
248 | },
249 | "nbformat": 4,
250 | "nbformat_minor": 4
251 | }
252 |
--------------------------------------------------------------------------------
/practice-exercise-07-solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 07 - Solution"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "We have defined a variable x equals 2. Then we have the if-else condition, which checks if x is equal to 2 or not. But we get an error in the output. Decode the error and rectify the program to get the expected output.\n",
17 | "\n",
18 | "#### Expected Output:\n",
19 | "\n",
20 | "Exact match!"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": 1,
26 | "metadata": {},
27 | "outputs": [
28 | {
29 | "name": "stdout",
30 | "output_type": "stream",
31 | "text": [
32 | "Exact match!\n"
33 | ]
34 | }
35 | ],
36 | "source": [
37 | "x = 2\n",
38 | "\n",
39 | "if x == 2:\n",
40 | " print('Exact match!')\n",
41 | "else:\n",
42 | " print('Not a match!')"
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | "#### Explanation:\n",
50 | "\n",
51 | "When comparing, we need to use the equality operator(==), not the assignment operator(=)."
52 | ]
53 | },
54 | {
55 | "cell_type": "markdown",
56 | "metadata": {},
57 | "source": [
58 | "### Question 2:\n",
59 | "\n",
60 | "There is a message given below. When we try to print the message, we are getting a syntax error. Look at the error and rectify it.\n",
61 | "\n",
62 | "
msg = 'Don't run in the park.'\n",
63 | "\n",
64 | "#### Expected Output:\n",
65 | "\n",
66 | "Don't run in the park."
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": 2,
72 | "metadata": {},
73 | "outputs": [
74 | {
75 | "name": "stdout",
76 | "output_type": "stream",
77 | "text": [
78 | "Don't run in the park.\n"
79 | ]
80 | }
81 | ],
82 | "source": [
83 | "msg = 'Don\\'t run in the park.'\n",
84 | "\n",
85 | "print(msg)"
86 | ]
87 | },
88 | {
89 | "cell_type": "markdown",
90 | "metadata": {},
91 | "source": [
92 | "### Question 3:\n",
93 | "\n",
94 | "We have a program given below that generates an *AttributeError* exception. Make changes in the program to handle this exception and print the message *'An error occured.'*\n",
95 | "\n",
96 | "#### Expected Output:\n",
97 | "\n",
98 | "An error occurred."
99 | ]
100 | },
101 | {
102 | "cell_type": "code",
103 | "execution_count": 3,
104 | "metadata": {},
105 | "outputs": [
106 | {
107 | "name": "stdout",
108 | "output_type": "stream",
109 | "text": [
110 | "An error occurred.\n"
111 | ]
112 | }
113 | ],
114 | "source": [
115 | "try:\n",
116 | " num = (5, 7, 9, 11)\n",
117 | " num.append(13)\n",
118 | "except AttributeError:\n",
119 | " print('An error occurred.')"
120 | ]
121 | },
122 | {
123 | "cell_type": "markdown",
124 | "metadata": {},
125 | "source": [
126 | "### Question 4:\n",
127 | "\n",
128 | "We have a function given below which prints an element from the list. But what if the user calls an invalid index? So, I want you to re-write the following function using the try-except block to handle the *IndexError* exception.\n",
129 | "\n",
130 | "#### Expected Output:\n",
131 | "\n",
132 | "Invalid index, try again."
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "execution_count": 4,
138 | "metadata": {},
139 | "outputs": [
140 | {
141 | "name": "stdout",
142 | "output_type": "stream",
143 | "text": [
144 | "Invalid index, try again.\n"
145 | ]
146 | }
147 | ],
148 | "source": [
149 | "def getData(data, index):\n",
150 | " try:\n",
151 | " return data[index]\n",
152 | " except IndexError:\n",
153 | " print('Invalid index, try again.')\n",
154 | "\n",
155 | "names = ['Alpha', 'Victor', 'Charles']\n",
156 | "\n",
157 | "getData(names, 5)"
158 | ]
159 | },
160 | {
161 | "cell_type": "markdown",
162 | "metadata": {},
163 | "source": [
164 | "### Question 5:\n",
165 | "\n",
166 | "We have a function given below that adds elements of two lists. However, I can think of two exceptions that can occur in this program. \n",
167 | "\n",
168 | "1. The index entered by the user during the function call may be out of bound.\n",
169 | "2. The list entered by the user can consist of int objects, str objects, or even a combination of both. And what if we try to add an int object and an str object?\n",
170 | "\n",
171 | "Re-write this program to handle the two exceptions mentioned above.\n",
172 | "\n",
173 | "#### Expected Output:\n",
174 | "\n",
175 | "Invalid index, try again."
176 | ]
177 | },
178 | {
179 | "cell_type": "code",
180 | "execution_count": 5,
181 | "metadata": {},
182 | "outputs": [
183 | {
184 | "name": "stdout",
185 | "output_type": "stream",
186 | "text": [
187 | "Invalid index, try again.\n"
188 | ]
189 | }
190 | ],
191 | "source": [
192 | "def add_elements(data_1, data_2, index):\n",
193 | " try:\n",
194 | " return data_1[index] + data_2[index]\n",
195 | " except IndexError:\n",
196 | " print('Invalid index, try again.')\n",
197 | " return\n",
198 | " except TypeError:\n",
199 | " print('Invalid addition of objects, check the lists.')\n",
200 | " return\n",
201 | "\n",
202 | "numList_1 = [1, 5, 9, 15]\n",
203 | "numList_2 = [2, 7, 11, 18]\n",
204 | "\n",
205 | "add_elements(numList_1, numList_2, 5)"
206 | ]
207 | },
208 | {
209 | "cell_type": "markdown",
210 | "metadata": {},
211 | "source": [
212 | "### Question 6:\n",
213 | "\n",
214 | "Write a Python program to find the day of the week for the date given below. Also, we don't know what errors we might encounter while executing the program. So, wrap the code inside a try-except block and handle the exceptions by printing the message *'Oops! An error occurred.'*\n",
215 | "\n",
216 | "given_date = datetime(2010, 6, 12)\n",
217 | "\n",
218 | "#### Expected Output:\n",
219 | "\n",
220 | "Saturday"
221 | ]
222 | },
223 | {
224 | "cell_type": "code",
225 | "execution_count": 6,
226 | "metadata": {},
227 | "outputs": [
228 | {
229 | "name": "stdout",
230 | "output_type": "stream",
231 | "text": [
232 | "Saturday\n"
233 | ]
234 | }
235 | ],
236 | "source": [
237 | "from datetime import datetime\n",
238 | "try:\n",
239 | " given_date = datetime(2010, 6, 12)\n",
240 | " print(given_date.strftime('%A')) \n",
241 | "except:\n",
242 | " print('Oops! An error occured.')"
243 | ]
244 | },
245 | {
246 | "cell_type": "markdown",
247 | "metadata": {},
248 | "source": [
249 | "### Question 7:\n",
250 | "\n",
251 | "Write a function to reverse a string if it's length is an even number. I think of the *TypeError* exception if a numeric value is passed inside the function call. But there might be other exceptions too that can occur while executing the program. \n",
252 | "
So, wrap the function around a try-except block and handle the *TypeError* exception with the message *'Check the string.'* For all other exceptions, print the message *'Something went wrong.'*\n",
253 | "\n",
254 | "*Input String = 'Python'*\n",
255 | "\n",
256 | "#### Expected Output:\n",
257 | "\n",
258 | "nohtyP"
259 | ]
260 | },
261 | {
262 | "cell_type": "code",
263 | "execution_count": 7,
264 | "metadata": {},
265 | "outputs": [
266 | {
267 | "name": "stdout",
268 | "output_type": "stream",
269 | "text": [
270 | "nohtyP\n"
271 | ]
272 | }
273 | ],
274 | "source": [
275 | "def reverse_string(text):\n",
276 | " try:\n",
277 | " if len(text) % 2 == 0:\n",
278 | " return ''.join(reversed(text)) \n",
279 | " else:\n",
280 | " print('String\\'s length is an odd number. Could not reverse.')\n",
281 | " return\n",
282 | " except TypeError:\n",
283 | " print('Check the string.') \n",
284 | " return\n",
285 | " except:\n",
286 | " print('Something went wrong.') \n",
287 | " return\n",
288 | " \n",
289 | "print(reverse_string('Python'))"
290 | ]
291 | },
292 | {
293 | "cell_type": "markdown",
294 | "metadata": {},
295 | "source": [
296 | "### Question 8:\n",
297 | "\n",
298 | "Write a function to extract the first ten letters of a string passed as an argument. But if the string is less than ten characters long, raise a *ValueError* and handle it using the message *'Oops! Too short string.'*\n",
299 | "\n",
300 | "#### Expected Output:\n",
301 | "\n",
302 | "Oops! Too short string."
303 | ]
304 | },
305 | {
306 | "cell_type": "code",
307 | "execution_count": 8,
308 | "metadata": {},
309 | "outputs": [
310 | {
311 | "name": "stdout",
312 | "output_type": "stream",
313 | "text": [
314 | "Oops! Too short string.\n"
315 | ]
316 | }
317 | ],
318 | "source": [
319 | "def extract_five_letters(text):\n",
320 | " try:\n",
321 | " if len(text) > 10:\n",
322 | " return text[:10] \n",
323 | " else:\n",
324 | " raise ValueError\n",
325 | " except:\n",
326 | " print('Oops! Too short string.')\n",
327 | " \n",
328 | "extract_five_letters('Python')"
329 | ]
330 | },
331 | {
332 | "cell_type": "markdown",
333 | "metadata": {},
334 | "source": [
335 | "### Question 9:\n",
336 | "\n",
337 | "Write a program to square the number entered by the user. But what if the user enters a string or an alphanumeric value, or if some other unexpected exceptions occur. So, wrap the program inside the try-except block to handle the exceptions, and the program should run until the user enters a numeric value.\n",
338 | "\n",
339 | "#### Expected Output:\n",
340 | "\n",
341 | "*Enter a number*: aa\n",
342 | "
Enter a valid input.\n",
343 | "
*Enter a number*: 5\n",
344 | "
25"
345 | ]
346 | },
347 | {
348 | "cell_type": "code",
349 | "execution_count": 9,
350 | "metadata": {},
351 | "outputs": [
352 | {
353 | "name": "stdout",
354 | "output_type": "stream",
355 | "text": [
356 | "Enter a number: aa\n",
357 | "Enter a valid input.\n",
358 | "Enter a number: 5\n",
359 | "25\n"
360 | ]
361 | }
362 | ],
363 | "source": [
364 | "while True:\n",
365 | " try:\n",
366 | " n = int(input('Enter a number: ')) \n",
367 | " print(n**2) \n",
368 | " break\n",
369 | " except:\n",
370 | " print('Enter a valid input.') "
371 | ]
372 | },
373 | {
374 | "cell_type": "markdown",
375 | "metadata": {},
376 | "source": [
377 | "### Question 10:\n",
378 | "\n",
379 | "Write a program to process the results of the user. The program should consist of a user-defined exception class *'InvalidNumError'* to raise an error if the marks entered by the user is less than 0 or greater than 100. Otherwise, print the message *'Results processing.'*\n",
380 | "\n",
381 | "#### Expected Output:\n",
382 | "\n",
383 | "Results processing.\n",
384 | "
Error! Try again."
385 | ]
386 | },
387 | {
388 | "cell_type": "code",
389 | "execution_count": 10,
390 | "metadata": {},
391 | "outputs": [
392 | {
393 | "name": "stdout",
394 | "output_type": "stream",
395 | "text": [
396 | "Results processing.\n",
397 | "Error! Try again.\n"
398 | ]
399 | }
400 | ],
401 | "source": [
402 | "class InvalidNumError(Exception):\n",
403 | " pass\n",
404 | "\n",
405 | "def marks_check(marks):\n",
406 | " try:\n",
407 | " if int(marks) < 0:\n",
408 | " raise InvalidNumError\n",
409 | " elif int(marks) > 100:\n",
410 | " raise InvalidNumError\n",
411 | " else:\n",
412 | " print('Results processing.')\n",
413 | " except InvalidNumError:\n",
414 | " print('Error! Try again.')\n",
415 | " \n",
416 | "marks_check(50)\n",
417 | "marks_check(105) "
418 | ]
419 | }
420 | ],
421 | "metadata": {
422 | "kernelspec": {
423 | "display_name": "Python 3",
424 | "language": "python",
425 | "name": "python3"
426 | },
427 | "language_info": {
428 | "codemirror_mode": {
429 | "name": "ipython",
430 | "version": 3
431 | },
432 | "file_extension": ".py",
433 | "mimetype": "text/x-python",
434 | "name": "python",
435 | "nbconvert_exporter": "python",
436 | "pygments_lexer": "ipython3",
437 | "version": "3.8.3"
438 | }
439 | },
440 | "nbformat": 4,
441 | "nbformat_minor": 4
442 | }
443 |
--------------------------------------------------------------------------------
/practice-exercise-08-question-02.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LearningJournal/Python-Foundation-Course/9a99503550ae62ace918c80ea9aa52b9ba51ae17/practice-exercise-08-question-02.zip
--------------------------------------------------------------------------------
/practice-exercise-08-question-05.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LearningJournal/Python-Foundation-Course/9a99503550ae62ace918c80ea9aa52b9ba51ae17/practice-exercise-08-question-05.zip
--------------------------------------------------------------------------------
/practice-exercise-08.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Practice Exercise - 08"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Question 1:\n",
15 | "\n",
16 | "Write a python class *Student*, which contains the student's name and his marks in 3 subjects given below. Define a class method to calculate and return the average percentage of the student. Finally, call the class and display all the information.\n",
17 | "\n",
18 | "name = 'Robin'\n",
19 | "
sub_1 = 85\n",
20 | "
sub_2 = 74\n",
21 | "
sub_3 = 90\n",
22 | "\n",
23 | "#### Expected Output:\n",
24 | "\n",
25 | "Robin\n",
26 | "
83.0"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {},
33 | "outputs": [],
34 | "source": []
35 | },
36 | {
37 | "cell_type": "markdown",
38 | "metadata": {},
39 | "source": [
40 | "### Question 2:\n",
41 | "\n",
42 | "Create a module *calculator.py*, consisting of 4 methods performing arithmetic operations on two numbers: addition, subtraction, multiplication, and division. Now import this module in another python file *test.py* and perform the addition and division operations by calling the functions from the *calculator.py* file.\n",
43 | "\n",
44 | "#### Expected Output:\n",
45 | "\n",
46 | "Addition: 125\n",
47 | "
Division: 4.0"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": null,
53 | "metadata": {},
54 | "outputs": [],
55 | "source": []
56 | },
57 | {
58 | "cell_type": "markdown",
59 | "metadata": {},
60 | "source": [
61 | "### Question 3:\n",
62 | "\n",
63 | "Create a class *Student* initialized with name, roll_number, and the student's total percentage. Also, define a class attribute that provides the institution's name, and it will be the same for all the students since they are all studying under a single institution. Then create two objects of this class and display the information.\n",
64 | "\n",
65 | "#### Expected Output:\n",
66 | "\n",
67 | "Student 1: Marcus of Cambridge @ 25 has got 95%.\n",
68 | "
Student 2: Derek of Cambridge @ 12 has got 84%."
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": null,
74 | "metadata": {},
75 | "outputs": [],
76 | "source": []
77 | },
78 | {
79 | "cell_type": "markdown",
80 | "metadata": {},
81 | "source": [
82 | "### Question 4:\n",
83 | "\n",
84 | "Write a python class *reverse_string* to reverse the words of a string.\n",
85 | "\n",
86 | "Input String: Learning Journal - Python Foundation Course\n",
87 | "\n",
88 | "#### Expected Output:\n",
89 | "\n",
90 | "Output String: Course Foundation Python - Journal Learning"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": null,
96 | "metadata": {},
97 | "outputs": [],
98 | "source": []
99 | },
100 | {
101 | "cell_type": "markdown",
102 | "metadata": {},
103 | "source": [
104 | "### Question 5:\n",
105 | "\n",
106 | "Create two modules *medical.py* and *report.py*. The *medical.py* file should take the details given below and calculate the BMI. The *report.py* file should display the message regarding the customer's fitness depending upon the BMI calculated from *medical.py*. Display only the message but not the BMI calculated from *medical.py*. It should only be displayed when we execute the *medical.py* file.\n",
107 | "\n",
108 | "Name: David\n",
109 | "
Weight: 83 Kgs\n",
110 | "
Height: 1.85 m\n",
111 | "\n",
112 | "BMI = Weight / (Height * Height)\n",
113 | "\n",
114 | "BMI > 25 ----> Overweight\n",
115 | "
BMI < 18.5 ----> Underweight\n",
116 | "
Otherwise ----> Normal Weight\n",
117 | "\n",
118 | "#### Expected Output:\n",
119 | "\n",
120 | "Overweight"
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": null,
126 | "metadata": {},
127 | "outputs": [],
128 | "source": []
129 | },
130 | {
131 | "cell_type": "markdown",
132 | "metadata": {},
133 | "source": [
134 | "### Question 6:\n",
135 | "\n",
136 | "Create a class *Mensuration*, which is initialized with length, breadth, and height. Now, create another class *cuboid* that will inherit the *Mensuration* class, and the child class will contain an instance method *volume* to calculate and display the volume of a cuboid.\n",
137 | "\n",
138 | "Length = 5\n",
139 | "
Breadth = 3\n",
140 | "
Height = 4\n",
141 | "\n",
142 | "#### Expected Output:\n",
143 | "\n",
144 | "Volume of Cuboid: 60"
145 | ]
146 | },
147 | {
148 | "cell_type": "code",
149 | "execution_count": null,
150 | "metadata": {},
151 | "outputs": [],
152 | "source": []
153 | },
154 | {
155 | "cell_type": "markdown",
156 | "metadata": {},
157 | "source": [
158 | "### Question 7:\n",
159 | "\n",
160 | "Write a python class *Cube* that contains three methods to calculate the curved surface area, total surface area, and volume of the cube. The side of the cube is five units.\n",
161 | "\n",
162 | "#### Expected Output:\n",
163 | "\n",
164 | "Curved Surface Area: 100\n",
165 | "
Total Surface Area: 150\n",
166 | "
Volume: 125"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": null,
172 | "metadata": {},
173 | "outputs": [],
174 | "source": []
175 | },
176 | {
177 | "cell_type": "markdown",
178 | "metadata": {},
179 | "source": [
180 | "### Question 8:\n",
181 | "\n",
182 | "Create a class *Sports*, which is initialized with the number of players required to play that sport and the country in which it is the national sport. Define an instance method to print all the information. Define two other classes, '*Cricket*' and '*Basketball*' that inherits the class sports and display all the information.\n",
183 | "\n",
184 | "#### Expected Output:\n",
185 | "\n",
186 | "Cricket: National Sport of England, and it requires 11 players from each team.\n",
187 | "
Basketball: National Sport of Lithuania, and it requires 5 players from each team."
188 | ]
189 | },
190 | {
191 | "cell_type": "code",
192 | "execution_count": null,
193 | "metadata": {},
194 | "outputs": [],
195 | "source": []
196 | },
197 | {
198 | "cell_type": "markdown",
199 | "metadata": {},
200 | "source": [
201 | "### Question 9:\n",
202 | "\n",
203 | "Create a class *Vehicle*, which is initialized with the type of vehicle (i.e., 2-wheeler or 4-wheeler), name, and mileage. Define an instance method '*print_info*' to print all these pieces of information. Create two other classes, '*Bike*' and '*Bus*,' that inherits the *Vehicle* class. Using a class attribute display the color of both the child classes as *Red*.\n",
204 | "\n",
205 | "#### Expected Output:\n",
206 | "\n",
207 | "KTM Duke is a 2-Wheeler vehicle, and the color of the vehicle is Red.\n",
208 | "
Volkswagen Polo is a 4-Wheeler vehicle, and the color of the vehicle is Red."
209 | ]
210 | },
211 | {
212 | "cell_type": "code",
213 | "execution_count": null,
214 | "metadata": {},
215 | "outputs": [],
216 | "source": []
217 | },
218 | {
219 | "cell_type": "markdown",
220 | "metadata": {},
221 | "source": [
222 | "### Question 10:\n",
223 | "\n",
224 | "Create a python class *airport_taxi*, which is initialized with a kilometer. Define an instance method that calculates the trip's fare. If the number of kilometers is greater than 50, the fare is Rs.6 per kilometer. Otherwise, the fare is Rs.per kilometer. Create another class, *uber_taxi*, which inherits the *airport_taxi* class. But the fare of uber_taxi is Rs.5.5 per kilometer irrespective of the number of kilometers. Print the fare for both the taxi if the passenger is traveling 40 km.\n",
225 | "\n",
226 | "#### Expected Output:\n",
227 | "\n",
228 | "200\n",
229 | "
220.0"
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "execution_count": null,
235 | "metadata": {},
236 | "outputs": [],
237 | "source": []
238 | }
239 | ],
240 | "metadata": {
241 | "kernelspec": {
242 | "display_name": "Python 3",
243 | "language": "python",
244 | "name": "python3"
245 | },
246 | "language_info": {
247 | "codemirror_mode": {
248 | "name": "ipython",
249 | "version": 3
250 | },
251 | "file_extension": ".py",
252 | "mimetype": "text/x-python",
253 | "name": "python",
254 | "nbconvert_exporter": "python",
255 | "pygments_lexer": "ipython3",
256 | "version": "3.8.3"
257 | }
258 | },
259 | "nbformat": 4,
260 | "nbformat_minor": 4
261 | }
262 |
--------------------------------------------------------------------------------