Skip to content

Commit cf5b31e

Browse files
Please read Comments & Markdown for more clearity.
1 parent f53a8e7 commit cf5b31e

File tree

1 file changed

+326
-0
lines changed

1 file changed

+326
-0
lines changed

Assignment-3.ipynb

+326
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Benefits of using Python\n",
8+
"- Python is easy to learn \n",
9+
"- Its easy to implement, which speeds up the development of the project, which indirectly saves python developer's time\n",
10+
"- It is widely used in different domains\n",
11+
"- Pyhton has Jargon of packages and Libraries which can be used for Data Analysis,Ml Algoriths,IOT, Web Development etc."
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 19,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"## The code in the function runs when it is called, not specified\n",
21+
"def sum(a,b):\n",
22+
" res = a+b\n",
23+
" return res"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 23,
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"name": "stdout",
33+
"output_type": "stream",
34+
"text": [
35+
"Intital value returned by sum(): 5\n",
36+
"Reusing the return value by performing substraction: 2\n"
37+
]
38+
}
39+
],
40+
"source": [
41+
"## here i have called that function\n",
42+
"t = sum(2,3)\n",
43+
"print(\"Intital value returned by sum(): \",t)\n",
44+
"## importance of return statement\n",
45+
"print(\"Reusing the return value by performing substraction: \",t-3)"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 3,
51+
"metadata": {},
52+
"outputs": [
53+
{
54+
"data": {
55+
"text/plain": [
56+
"'Akash'"
57+
]
58+
},
59+
"execution_count": 3,
60+
"metadata": {},
61+
"output_type": "execute_result"
62+
}
63+
],
64+
"source": [
65+
"## def causes a feature to be created followed by your function name.\n",
66+
"## This function can be reusable to reduce complexcity of code.\n",
67+
"def myName(name):\n",
68+
" yourName = name\n",
69+
" return yourName\n",
70+
"## calling the above function \n",
71+
"myName(\"Akash\")"
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"metadata": {},
77+
"source": [
78+
"## The distinction between a function and a call to a function is that function holds some operation with data and call to a function holds data in it."
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 8,
84+
"metadata": {},
85+
"outputs": [
86+
{
87+
"name": "stdout",
88+
"output_type": "stream",
89+
"text": [
90+
"global : 1\n",
91+
"Inside f() the value of variable a is : 1\n",
92+
"global : 1\n",
93+
"Inside g() the value of variable a is : 2\n",
94+
"global : 1\n",
95+
"Inside h() the value of variable a is : 3\n",
96+
"global : 3\n"
97+
]
98+
}
99+
],
100+
"source": [
101+
"## In this program I have demonstrated how we can access global variables,local variables and how to modify global variables \n",
102+
"a = 1\n",
103+
" \n",
104+
"# Uses global because there is no local 'a'\n",
105+
"\n",
106+
"def f():\n",
107+
"\n",
108+
" print('Inside f() the value of variable a is : ', a)\n",
109+
" \n",
110+
"# Variable 'a' is redefined as a local\n",
111+
"\n",
112+
"def g(): \n",
113+
"\n",
114+
" a = 2\n",
115+
"\n",
116+
" print('Inside g() the value of variable a is : ', a)\n",
117+
" \n",
118+
"# Uses global keyword to modify global 'a'\n",
119+
"\n",
120+
"def h(): \n",
121+
"\n",
122+
" global a\n",
123+
"\n",
124+
" a = 3\n",
125+
"\n",
126+
" print('Inside h() the value of variable a is : ', a)\n",
127+
" \n",
128+
"# Global scope\n",
129+
"\n",
130+
"print('global : ',a)\n",
131+
"f()\n",
132+
"\n",
133+
"print('global : ',a)\n",
134+
"g()\n",
135+
"\n",
136+
"print('global : ',a)\n",
137+
"h()\n",
138+
"\n",
139+
"print('global : ',a)"
140+
]
141+
},
142+
{
143+
"cell_type": "markdown",
144+
"metadata": {},
145+
"source": [
146+
"## Q.When a function call returns, what happens to variables in the local scope?\n",
147+
"## Ans. If you observe previous code in function g() we have local variable a = 2.This basically creates new local variable a and assignes value as 2 because if a local variable has the same name as a global variable the local variable will always take precedence."
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"metadata": {},
153+
"source": [
154+
"## Q.What is the concept of a return value? Is it possible to have a return value in an expression?\n",
155+
"## Ans. Yes it is possible to have a return value in an expression.But for that you need to store the return value in a variable and then use it.\n",
156+
"- Refer 1st block of code to get more clearity."
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 24,
162+
"metadata": {},
163+
"outputs": [
164+
{
165+
"name": "stdout",
166+
"output_type": "stream",
167+
"text": [
168+
"5\n"
169+
]
170+
},
171+
{
172+
"ename": "TypeError",
173+
"evalue": "unsupported operand type(s) for +: 'NoneType' and 'int'",
174+
"output_type": "error",
175+
"traceback": [
176+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
177+
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
178+
"\u001b[1;32m<ipython-input-24-554cba20108f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mv\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;31m## now we will get error as function is not returning any value as we are only printing in function\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
179+
"\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'NoneType' and 'int'"
180+
]
181+
}
182+
],
183+
"source": [
184+
"## Q.What is the return value of a call to a function that does not have a return statement?\n",
185+
"## Ans. You cannot reuse the value generated by that function\n",
186+
"\n",
187+
"def sub(a,b):\n",
188+
" print(a-b)\n",
189+
"## calling the above function\n",
190+
"v=sub(10,5)\n",
191+
"## now we will get error as function is not returning any value as we are only printing in function\n",
192+
"print(v+2)"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": 6,
198+
"metadata": {},
199+
"outputs": [
200+
{
201+
"data": {
202+
"text/plain": [
203+
"'My name is Akash Borgalli. I work as ML Enginner'"
204+
]
205+
},
206+
"execution_count": 6,
207+
"metadata": {},
208+
"output_type": "execute_result"
209+
}
210+
],
211+
"source": [
212+
"## To function variable refer to the global variable we will add global keyword inside the function\n",
213+
"\n",
214+
"designation = 'ML Enginner'\n",
215+
"def concat():\n",
216+
" ## accessing global variable\n",
217+
" global designation\n",
218+
" return \"My name is Akash Borgalli. I work as {}\".format(designation)\n",
219+
"concat()\n"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": 4,
225+
"metadata": {},
226+
"outputs": [
227+
{
228+
"data": {
229+
"text/plain": [
230+
"NoneType"
231+
]
232+
},
233+
"execution_count": 4,
234+
"metadata": {},
235+
"output_type": "execute_result"
236+
}
237+
],
238+
"source": [
239+
"## DataType of None is NonType \n",
240+
"x= None\n",
241+
"type(x)"
242+
]
243+
},
244+
{
245+
"cell_type": "markdown",
246+
"metadata": {},
247+
"source": [
248+
"## The sentence import areallyourpetsnamederic does is that it imports areallyourpetsnamederic package in your notebook."
249+
]
250+
},
251+
{
252+
"cell_type": "code",
253+
"execution_count": null,
254+
"metadata": {},
255+
"outputs": [],
256+
"source": [
257+
"## This is how i will import bacon() feature in a spam module\n",
258+
"import spam as sp\n",
259+
"sp.bacon()"
260+
]
261+
},
262+
{
263+
"cell_type": "markdown",
264+
"metadata": {},
265+
"source": [
266+
"## I would do Exception Handling so that my program is avoided from crashing"
267+
]
268+
},
269+
{
270+
"cell_type": "markdown",
271+
"metadata": {},
272+
"source": [
273+
"## The purpose of try block is you write code that can cause errors and the purpose of except block is if any error occurs in try block it will execute and try to find that exception defined in except block for printing custom error messages or perform some other task."
274+
]
275+
},
276+
{
277+
"cell_type": "code",
278+
"execution_count": 5,
279+
"metadata": {},
280+
"outputs": [
281+
{
282+
"name": "stdout",
283+
"output_type": "stream",
284+
"text": [
285+
"Try to make DataType Similar\n"
286+
]
287+
}
288+
],
289+
"source": [
290+
"# Always write Exception at last as it is root class which handles all type of exception\n",
291+
"try:\n",
292+
" a=1\n",
293+
" b='Akash'\n",
294+
" sum= a+b\n",
295+
"except NameError:\n",
296+
" print(\"The user have not defined the variable\")\n",
297+
"except TypeError:\n",
298+
" print(\"Try to make DataType Similar\")\n",
299+
"except Exception as ex:\n",
300+
" print(ex)\n",
301+
" "
302+
]
303+
}
304+
],
305+
"metadata": {
306+
"kernelspec": {
307+
"display_name": "Python 3",
308+
"language": "python",
309+
"name": "python3"
310+
},
311+
"language_info": {
312+
"codemirror_mode": {
313+
"name": "ipython",
314+
"version": 3
315+
},
316+
"file_extension": ".py",
317+
"mimetype": "text/x-python",
318+
"name": "python",
319+
"nbconvert_exporter": "python",
320+
"pygments_lexer": "ipython3",
321+
"version": "3.8.5"
322+
}
323+
},
324+
"nbformat": 4,
325+
"nbformat_minor": 4
326+
}

0 commit comments

Comments
 (0)