1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
{
"metadata": {
"name": "Chapter 8"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "<h1>Chapter 8: Functions,Part Two: References, Overloading, and Default Arguments<h1>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.1, Page Number: 158<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Example for call by value'''\n\ndef sqr_it(x):\n x=x*x\n return x\n\n#Variable decleration\nt=10\n\n#Result; function calling\nprint sqr_it(t),' ',t\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "100 10\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.2, Page Number: 159<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Program to swap two values; implementing call by pointers'''\n\ndef swap(x,y):\n temp=x\n x=y\n y=temp\n return x, y\n\n#Variable decleration\ni=10\nj=20 \n\n#Initial values\nprint \"Initial values of i and j: \",\nprint i,' ',j\n\n#Calling function to swap\ni, j=swap(i,j)\n\n#Result\nprint \"Swapped values of i and j: \",\nprint i,' ',j",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Initial values of i and j: 10 20\nSwapped values of i and j: 20 10\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.3, Page Number: 161<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Example to implement call by reference in python'''\n\ndef f(i):\n i=10\n return i #Returning the value since the function cannot access the variables in the calling scope.\n\n#Variable Decleration\nval=1\n\nprint \"Old value for val: \",val\nval=f(val) #Function call\n\n#Result\nprint \"New value for val: \",val\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Old value for val: 1\nNew value for val: 10\n"
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.4, Page Number: 162<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Program to swap two values; implementing call by reference'''\n\ndef swap(i,j):\n temp=i[0]\n i[0]=j[0]\n j[0]=temp\n return i, j\n\n#Variable decleration\ni=[]\nj=[]\ni.append(10)\nj.append(20)\n\n#Initial values\nprint \"Initial values of i and j: \",\nprint i[0],' ',j[0]\n\n#Calling function to swap\ni, j=swap(i,j)\n\n#Result\nprint \"Swapped values of i and j: \",\nprint i[0],' ',j[0]",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Initial values of i and j: 10 20\nSwapped values of i and j: 20 10\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.5, Page Number: 164<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''An example program'''\n#Since python cannot return references, the value is changed using the variable itself\n\n#Variable Decleration\nval = 100.0\n\ndef f():\n global val\n return val\n\n#Result\nprint val\n\nnewval=f() #function call\nprint newval\n\nval=99.1 #change val's value\nprint f() #print val's new value\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "100.0\n100.0\n99.1\n"
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.6, Page Number: 166<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Changing contents of a list'''\n#Since python cannot return references, the program has been modified.\n\n#Function definition\ndef change_it(i,n):\n global vals\n vals[i]=n\n\n#Variable Decleration\nvals=[1.1,2.2,3.3,4.4,5.5]\n\nprint \"Here are the original values: \",\nfor i in range(5):\n print vals[i],\nprint\n \n#Function call\nchange_it(1,5298.23)\nchange_it(3,-98.8)\n\n#Result\nprint \"Here are the changed values: \",\nfor i in range(5):\n print vals[i],",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Here are the original values: 1.1 2.2 3.3 4.4 5.5\nHere are the changed values: 1.1 5298.23 3.3 -98.8 5.5\n"
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.7, Page Number: 167<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementing a simple safe array'''\n\n#Variable Decleration\nvals=[None]*10\nerror=-1\n\n#put values into the array\ndef put(i,n):\n global vals\n if i>=0 and i<10:\n vals[i]=n\n else:\n print \"Bounds Error!\"\n error=n\n\n#obtain a value from the array\ndef get(i):\n if i>=0 and i<10:\n return vals[i]\n else:\n print \"Bounds error!\"\n return -1\n \n#put values into the array\nput(0,10)\nput(1,20)\nput(9,30)\n\n#Result\nprint get(0),' ',get(1),' ',get(9),\n\n#now, intentionally generate an errer\nput(12,1)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "10 20 30 Bounds Error!\n"
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.8, Page Number: 169<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementing references in pyhton'''\n\n#Variable Decleration\ni=[]\nj=i #independent reference\n\nj.append(10) #Here i and j are just references to [10]\n\nprint j[0],\" \",i[0] \n\nk=121\ni[0]=k #copies k's value into j[0] \n\n#Result\nprint j[0]",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "10 10\n121\n"
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.9, Page Number: 170<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Function overloading in python'''\n#Overloading is not supported by python, hence it is implemented by default arguments.\n\ndef f(i,j=None):\n if j==None: \n if isinstance(i,int): #for 1st function\n print \"In f(int), i is \",i \n else: #for 3rd function\n print \"In f(double), k is \",i\n else: #for 2nd arguments\n print \"In f(int,int), i is \",i,\", j is \",j\n \n#calling function\nf(10)\nf(10,20)\nf(12.23)\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "In f(int), i is 10\nIn f(int,int), i is 10 , j is 20\nIn f(double), k is 12.23\n"
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.10, Page Number: 171<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Create an overloaded function of abs called myabs()'''\n\n#myabs() in three ways\ndef myabs(i):\n if isinstance(i,int): #first instance\n print \"Using integer myabs(): \",\n if i<0:\n return -i\n else:\n return i\n elif isinstance(i,float): #second instance\n print \"Using double myabs(): \",\n if(i<0.0):\n return -i\n else:\n return i\n elif isinstance(i,long): #third instance\n print \"Using long myabs(): \",\n if i<0:\n return -i\n else:\n return i\n\n#Result; calling the function \nprint myabs(-10)\nprint myabs(-11.0)\nprint myabs(-9L)\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Using integer myabs(): 10\nUsing double myabs(): 11.0\nUsing long myabs(): 9\n"
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.11, Page Number: 174<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''An example of default arguments'''\n\nimport os\n\ndef clrscr(size=25):\n while(size):\n print \"\"\n size-=1\n \nfor i in range(30):\n print i\n clrscr() #clears 25 lines\n\nfor i in range(30):\n print i\n clrscr(10) #clears 10 lines\n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "0\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n1\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n2\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n3\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n4\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n5\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n6\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n7\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n8\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n9\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n10\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n11\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n12\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n13\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n14\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n15\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n16\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n17\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n18\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n19\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n20\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n21\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n22\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n23\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n24\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n25\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n26\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n27\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n28\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n29\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n0\n \n \n \n \n \n \n \n \n \n \n1\n \n \n \n \n \n \n \n \n \n \n2\n \n \n \n \n \n \n \n \n \n \n3\n \n \n \n \n \n \n \n \n \n \n4\n \n \n \n \n \n \n \n \n \n \n5\n \n \n \n \n \n \n \n \n \n \n6\n \n \n \n \n \n \n \n \n \n \n7\n \n \n \n \n \n \n \n \n \n \n8\n \n \n \n \n \n \n \n \n \n \n9\n \n \n \n \n \n \n \n \n \n \n10\n \n \n \n \n \n \n \n \n \n \n11\n \n \n \n \n \n \n \n \n \n \n12\n \n \n \n \n \n \n \n \n \n \n13\n \n \n \n \n \n \n \n \n \n \n14\n \n \n \n \n \n \n \n \n \n \n15\n \n \n \n \n \n \n \n \n \n \n16\n \n \n \n \n \n \n \n \n \n \n17\n \n \n \n \n \n \n \n \n \n \n18\n \n \n \n \n \n \n \n \n \n \n19\n \n \n \n \n \n \n \n \n \n \n20\n \n \n \n \n \n \n \n \n \n \n21\n \n \n \n \n \n \n \n \n \n \n22\n \n \n \n \n \n \n \n \n \n \n23\n \n \n \n \n \n \n \n \n \n \n24\n \n \n \n \n \n \n \n \n \n \n25\n \n \n \n \n \n \n \n \n \n \n26\n \n \n \n \n \n \n \n \n \n \n27\n \n \n \n \n \n \n \n \n \n \n28\n \n \n \n \n \n \n \n \n \n \n29\n \n \n \n \n \n \n \n \n \n \n"
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.12, Page Number: 176<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''A customized version of strcat'''\n\n#Variable Decleration\nstr1 = \"This is a test\"\nstr2 = \"0123456789\"\n\n#function for concatenation\ndef mystrcat(s1,s2,l=-1):\n if l==-1:\n l=len(str2)\n s2=s2[:l] #truncates s2\n s1=s1+s2 #concatenates the 2 strings\n return s1\n\nstr1=mystrcat(str1,str2,5) #concatenate 5 chars\nprint str1\n\nstr1 = \"this is a test\" #reset str1\n\nstr1=mystrcat(str1, str2) #concatenate entire string\nprint str1",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "This is a test01234\nthis is a test0123456789\n"
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.13, Page Number: 177<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Overload ambiguity'''\n#Here there wont be any ambiguity because float and double are the same.\n\ndef myfunc(i):\n return i\n \nprint myfunc(10.1),\nprint myfunc(10)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "10.1 10\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.14, Page Number: 178<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Another ambiguity'''\n\nfrom ctypes import *\n\ndef myfunc(ch):\n if isinstance(ch,c_int):\n return chr(ch.value+1)\n elif isinstance(ch,c_uint):\n return chr(ch.value-1)\n \n \nprint myfunc(c_int(ord('c'))),\nprint myfunc(c_uint(88))",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": " d W\n"
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 8.15, Page Number: 179<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''More ambiguity'''\n\ndef myfunc(i,j=1):\n if j==1:\n return i*j\n else:\n return i\n \n \nprint myfunc(4,5),\nprint myfunc(10)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "4 10\n"
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|