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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Chapter 11: Storage Class<h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 11.1, Page number: 376<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Working of auto variable\n",
"\n",
"import sys\n",
"\n",
"#Functio definitions\n",
"def call1():\n",
" v = 20\n",
" sys.stdout.write(\"\\nV = %d\"%(v))\n",
" \n",
"def call2():\n",
" v = 30\n",
" call1()\n",
" sys.stdout.write(\"\\nV = %d\"%(v))\n",
" \n",
"#Variable Initialization\n",
"v = 10\n",
"\n",
"#Function call\n",
"call2()\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nV = %d\"%(v))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"V = 20\n",
"V = 30\n",
"V = 10"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 11.2, Page number: 376<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Working of auto variables in different blocks\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"x = 10\n",
"\n",
"sys.stdout.write(\"\\nX = %d\"%(x))\n",
"\n",
"x = 20\n",
"sys.stdout.write(\"\\nX = %d\"%(x))\n",
"\n",
"x = 10\n",
"sys.stdout.write(\"\\nX = %d\"%(x))\n",
"\n",
"#In python, block of statements are indicated using intendation.\n",
"#block of statements can be written for conditional,loop or function statements"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"X = 10\n",
"X = 20\n",
"X = 10"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 11.3, Page number: 377<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Use same variable in different blocks with different data types\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization & Result\n",
"x = 10\n",
"sys.stdout.write(\"\\nX = %d\"%(x))\n",
"\n",
"x = 2.22\n",
"sys.stdout.write(\"\\nX = %g\"%(x))\n",
"\n",
"x = \"Auto Storage Class\"\n",
"sys.stdout.write(\"\\nX = %s\"%(x))\n",
"\n",
"x = 10\n",
"sys.stdout.write(\"\\nX = %d\"%(x))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"X = 10\n",
"X = 2.22\n",
"X = Auto Storage Class\n",
"X = 10"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 11.4, Page number: 378<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Working of external variables\n",
"\n",
"import sys\n",
"\n",
"#Function definitions\n",
"def call1():\n",
" sys.stdout.write(\"\\nIn Call1() V = %d\"%(v))\n",
" \n",
"def call2():\n",
" sys.stdout.write(\"\\nIn call2() V = %d\"%(v))\n",
" \n",
"#Variable Initialization\n",
"global v\n",
"v = 10\n",
"\n",
"call1()\n",
"call2()\n",
"sys.stdout.write(\"\\nIn main() V = %d\"%(v))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"In Call1() V = 10\n",
"In call2() V = 10\n",
"In main() V = 10"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 11.5, Page number: 379<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Working of auto and global variables with same name\n",
"\n",
"import sys\n",
"\n",
"#Function definitions\n",
"def call1():\n",
" v = 20\n",
" sys.stdout.write(\"\\nIn Call1() V = %d\"%(v))\n",
" \n",
"def call2():\n",
" sys.stdout.write(\"\\nIn call2() V = %d\"%(v))\n",
" \n",
"#Variable Initialization\n",
"global v\n",
"v = 10\n",
"\n",
"#Function call\n",
"call1()\n",
"call2()\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nIn main() V = %d\"%(v))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"In Call1() V = 20\n",
"In call2() V = 10\n",
"In main() V = 10"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 11.6, Page number: 380<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Declare external variables using extern keyword\n",
"\n",
"#Variable Initialization\n",
"global m\n",
"m = 10\n",
"\n",
"#There is no extern keyword in python, global is used instead\n",
"sys.stdout.write(\"\\nM = %d\"%(m))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"M = 10"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 11.7, Page number: 380<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Use of static variable\n",
"\n",
"import sys\n",
"\n",
"#Function definition\n",
"def print1(m):\n",
" sys.stdout.write(\"\\nm = %d\"%(m))\n",
" if m == 3:\n",
" return\n",
" \n",
"m = 0\n",
"\n",
"for i in range(0,3):\n",
" m += 1\n",
" print1(m)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"m = 1\n",
"m = 2\n",
"m = 3"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 11.8, Page number: 381<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Difference between variables of auto and static class\n",
"\n",
"import sys\n",
"\n",
"#Result\n",
"y = 0\n",
"sys.stdout.write(\"\\nx = %d & y = %d\"%(x,y))\n",
"\n",
"#Since x display the junk value which stored already, it differs in different systems"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"x = 10 & y = 0"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 11.9, Page number: 382<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Register class variable\n",
"\n",
"import sys\n",
"\n",
"#There is no register keyword in python\n",
"m = 1\n",
"\n",
"#Result\n",
"for m in range(1,5+1):\n",
" sys.stdout.write(\"\\t%d\"%(m))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\t1\t2\t3\t4\t5"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 11.10, Page number: 382<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Register class variable\n",
"\n",
"import sys\n",
"\n",
"#There is no register class variable in python\n",
"m = 1\n",
"\n",
"for m in range(1,6):\n",
" sys.stdout.write(\"\\t%d\"%(m))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\t1\t2\t3\t4\t5"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|