Skip to content

Commit 6ba74e9

Browse files
authored
Add files via upload
1 parent eac15bb commit 6ba74e9

File tree

1 file changed

+329
-0
lines changed

1 file changed

+329
-0
lines changed

Nesting_Functions.ipynb

Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"toc_visible": true
8+
},
9+
"kernelspec": {
10+
"name": "python3",
11+
"display_name": "Python 3"
12+
},
13+
"language_info": {
14+
"name": "python"
15+
}
16+
},
17+
"cells": [
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {
21+
"id": "cnAUJprJdh9_"
22+
},
23+
"source": [
24+
"# Welcome to our Lab Practice!"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {
30+
"id": "vVYcayGedmms"
31+
},
32+
"source": [
33+
"This lab is all about defining nested functions. Are you ready? Let's get started!"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {
39+
"id": "JP8eGBVyeSQx"
40+
},
41+
"source": [
42+
"You will find some small tasks in sections below. After defining functions, call them to test."
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {
48+
"id": "D8pWdoN7d6Dy"
49+
},
50+
"source": [
51+
"## Define nestted functions."
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"source": [
57+
"###Task: Calculator\n",
58+
"\n",
59+
"1. Define a function that ask the user to input a number\n",
60+
"2. Define a function that computes the sum of two numbers\n",
61+
"3. Define a function that computes the substraction of two numbers\n",
62+
"4. Define a function that computes the product of two numbers\n",
63+
"5. Define a function that computes the division of two numbers\n",
64+
"6. Define a function that print the arithmetic operation result of two numbers by calling functions defined above\n",
65+
"\n",
66+
"\n"
67+
],
68+
"metadata": {
69+
"id": "TTqVA_ew48Sn"
70+
}
71+
},
72+
{
73+
"cell_type": "code",
74+
"source": [
75+
"def input_no():\n",
76+
" number=int(input(\"Input a number\",))\n",
77+
" print (number)\n",
78+
"\n",
79+
"def sum_2_nos(x,y):\n",
80+
" sum=x+y\n",
81+
" return sum\n",
82+
"\n",
83+
"def sub_2_nos(x,y):\n",
84+
" subtract=x-y\n",
85+
" return subtract\n",
86+
"\n",
87+
"def prod_2_nos(x,y):\n",
88+
" product=x*y\n",
89+
" return product\n",
90+
"\n",
91+
"def div_2_nos(x,y):\n",
92+
" div=x/y\n",
93+
" return div\n",
94+
"\n",
95+
"def arith_ops_2_nos(x,y):\n",
96+
" sum=sum_2_nos(x,y)\n",
97+
" subtract=sub_2_nos(x,y)\n",
98+
" product=prod_2_nos(x,y)\n",
99+
" division=div_2_nos(x,y)\n",
100+
" print(\"sum:\",sum,\"subtraction:\",subtract,\"product\",product,\"division\",division)"
101+
],
102+
"metadata": {
103+
"id": "4l8oECs25O26"
104+
},
105+
"execution_count": 10,
106+
"outputs": []
107+
},
108+
{
109+
"cell_type": "code",
110+
"source": [
111+
"arith_ops_2_nos(2,3)\n"
112+
],
113+
"metadata": {
114+
"colab": {
115+
"base_uri": "https://localhost:8080/"
116+
},
117+
"id": "E98NPy3dXWJk",
118+
"outputId": "468bf5b8-49e4-426c-90ad-b8a057b72855"
119+
},
120+
"execution_count": 11,
121+
"outputs": [
122+
{
123+
"output_type": "stream",
124+
"name": "stdout",
125+
"text": [
126+
"sum: 5 subtraction: -1 product 6 division 0.6666666666666666\n"
127+
]
128+
}
129+
]
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"source": [
134+
"###Task: Statistics\n",
135+
"\n",
136+
"1. Define a function that ask the user to input a number\n",
137+
"2. Define a function that computes the min of three numbers\n",
138+
"3. Define a function that computes the max of three numbers\n",
139+
"4. Define a function that computes the total of three numbers\n",
140+
"5. Define a function that computes the average of three numbers\n",
141+
"6. Define a function that print the statistics of the three numbers by calling functions defineda bove"
142+
],
143+
"metadata": {
144+
"id": "9hfJ0kha5G_A"
145+
}
146+
},
147+
{
148+
"cell_type": "code",
149+
"source": [
150+
"def user_input():\n",
151+
" num=int(input(\"Input a number:\",))\n",
152+
" print(num)\n",
153+
"\n",
154+
"def min_3(x,y,z):\n",
155+
" min=x\n",
156+
" if y<min:\n",
157+
" min=y\n",
158+
" if z<min:\n",
159+
" min=z\n",
160+
" return (min)\n",
161+
"\n",
162+
"def max_3(x,y,z):\n",
163+
" max=x\n",
164+
" if y>max:\n",
165+
" max=y\n",
166+
" if z>max:\n",
167+
" max=z\n",
168+
" return (max)\n",
169+
"\n",
170+
"def total_3(x,y,z):\n",
171+
" total=x+y+z\n",
172+
" return (total)\n",
173+
"\n",
174+
"def avg_3(x,y,z):\n",
175+
" avg=(x+y+z)/3\n",
176+
" return (avg)\n",
177+
"\n",
178+
"def print_stats(x,y,z):\n",
179+
" min=min_3(x,y,z)\n",
180+
" max=max_3(x,y,z)\n",
181+
" total=total_3(x,y,z)\n",
182+
" avg=avg_3(x,y,z)\n",
183+
" print(\"min:\",min,\"max:\",max,\"total:\",total,\"avg:\",avg)\n",
184+
"\n",
185+
"print_stats(3,4,5)\n"
186+
],
187+
"metadata": {
188+
"id": "qL379kld5PX0",
189+
"colab": {
190+
"base_uri": "https://localhost:8080/"
191+
},
192+
"outputId": "cbbd357f-5292-4432-c538-b664060bc47b"
193+
},
194+
"execution_count": 9,
195+
"outputs": [
196+
{
197+
"output_type": "stream",
198+
"name": "stdout",
199+
"text": [
200+
"min: 3 max: 5 total: 12 avg: 4.0\n"
201+
]
202+
}
203+
]
204+
},
205+
{
206+
"cell_type": "markdown",
207+
"metadata": {
208+
"id": "rXgbUIBRgQoG"
209+
},
210+
"source": [
211+
"###Task: Simple Calculator\n",
212+
"\n",
213+
"\n",
214+
"1. Define a function that increment an integer x by 1, x is a parameter\n",
215+
"2. Define a function that decrement an integer x by 1, x is a parameter\n",
216+
"3. Define a function that computes *x* $+$ *y* by using functions defined above, where x and y are parameters and you cannot directly use x $+$ y in your definition.\n",
217+
"4. Define a function that computes *x* $-$ *y* by using functions defined above, where x and y are parameters and you cannot directly use x $-$ y in your definition.\n",
218+
"5. Define a function that computes *x* $\\times$ *y* by using functions defined above, where x and y are parameters and you cannot directly use x * y in your definition.\n",
219+
"6. Define a function that computes *x* $**$ *y* by using functios defined above, where x and y are parameters and you cannot directly use *x* $**$ *y* in your definition\n",
220+
"7. Define a function that takes two arguments, x and y, and print the addition (x $+$ y), substraction(x $-$ y), product(x $\\times$ y), and exponent(x**y) of these two arguments"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"metadata": {
226+
"id": "oIKwmW2mfe3d",
227+
"colab": {
228+
"base_uri": "https://localhost:8080/"
229+
},
230+
"outputId": "1743bb21-25a3-4392-8354-42472e7d2c80"
231+
},
232+
"source": [
233+
"def increment(x):\n",
234+
" return x + 1\n",
235+
"\n",
236+
"def decrement(x):\n",
237+
" return x - 1\n",
238+
"\n",
239+
"def plus(x, y):\n",
240+
" for i in range(y):\n",
241+
" x = increment(x)\n",
242+
" return x\n",
243+
"\n",
244+
"def minus(x, y):\n",
245+
" for i in range(y):\n",
246+
" x = decrement(x)\n",
247+
" return x\n",
248+
"\n",
249+
"def times(x, y):\n",
250+
" result = 0\n",
251+
" for i in range(y):\n",
252+
" result = plus(result, x)\n",
253+
" return result\n",
254+
"\n",
255+
"def power(x, y):\n",
256+
" result = 1\n",
257+
" for i in range(y):\n",
258+
" result = times(result, x)\n",
259+
" return result\n",
260+
"\n",
261+
"def arith(x, y):\n",
262+
" print(x, '+', y, '=', plus(x, y))\n",
263+
" print(x, '-', y, '=', minus(x, y))\n",
264+
" print(x, '*', y, '=', times(x, y))\n",
265+
" print(x, '**', y, '=', power(x, y))\n",
266+
"\n",
267+
"arith(3, 2)\n",
268+
""
269+
],
270+
"execution_count": 17,
271+
"outputs": [
272+
{
273+
"output_type": "stream",
274+
"name": "stdout",
275+
"text": [
276+
"3 + 2 = 5\n",
277+
"3 - 2 = 1\n",
278+
"3 * 2 = 6\n",
279+
"3 ** 2 = 9\n"
280+
]
281+
}
282+
]
283+
},
284+
{
285+
"cell_type": "code",
286+
"source": [],
287+
"metadata": {
288+
"id": "erjdbe9w4XPo"
289+
},
290+
"execution_count": 15,
291+
"outputs": []
292+
},
293+
{
294+
"cell_type": "code",
295+
"source": [
296+
"plus(1,2)"
297+
],
298+
"metadata": {
299+
"colab": {
300+
"base_uri": "https://localhost:8080/"
301+
},
302+
"id": "w-2bFdvv5g3-",
303+
"outputId": "77fe7405-ee80-4572-fd70-1cb0b925e0b4"
304+
},
305+
"execution_count": 16,
306+
"outputs": [
307+
{
308+
"output_type": "execute_result",
309+
"data": {
310+
"text/plain": [
311+
"3"
312+
]
313+
},
314+
"metadata": {},
315+
"execution_count": 16
316+
}
317+
]
318+
},
319+
{
320+
"cell_type": "code",
321+
"source": [],
322+
"metadata": {
323+
"id": "Etra8ZkP5jUe"
324+
},
325+
"execution_count": null,
326+
"outputs": []
327+
}
328+
]
329+
}

0 commit comments

Comments
 (0)