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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
{
"metadata": {
"name": "",
"signature": "sha256:2b98cee86e6322a41a7dca7a85df32b68e0995a4fde2b6249b408a952c9387e4"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 6: Scope & Extent"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example scope1.c, page no. 202"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#as mentioned in the textbook, the statement j = 30 will not give an error as in Python you can declare a vairable anywhere\n",
"#however, the statement does not refer to the variable j of the function.\n",
"def func():\n",
" j = 20\n",
" print \"j is a variable in the function\"\n",
" print \"Value of j is \",j\n",
"i = 10\n",
"print \"i is a variable in main.\"\n",
"print \"Value of i is \", i\n",
"print \"Calling func...\"\n",
"func()\n",
"j = 30"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"i is a variable in main.\n",
"Value of i is 10\n",
"Calling func...\n",
"j is a variable in the function\n",
"Value of j is 20\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example global.c, page no. 202"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"g = 0\n",
"def func():\n",
" global g\n",
" print \"In func. g is visbile here, since it is global\"\n",
" print \"Incrementing g in func...\"\n",
" g += 1\n",
" return g\n",
"print \"In main. g is visible here, since it is global\"\n",
"print \"assigning 20 to g in main...\"\n",
"g = 20\n",
"print \"Callint func...\"\n",
"func()\n",
"print \"func returned g is \", g"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"In main. g is visible here, since it is global\n",
"assigning 20 to g in main...\n",
"Callint func...\n",
"In func. g is visbile here, since it is global\n",
"Incrementing g in func...\n",
"func returned g is 21\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example colcolon.c, page no. 203"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#there is no concept of scope resolution operator in Python. we will do it in different way\n",
"a = 10\n",
"def func():\n",
" global a\n",
" b = 20\n",
" print b\n",
" print a\n",
"func()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"20\n",
"10\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.1, page no. 204"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#there is no concept of block in Python. writing if True is smiliar to it but still, output will differ\n",
"j = 144\n",
"print \"The value of j outside the block is \", j\n",
"if True:\n",
" j = 12\n",
" print \"The value of j inside the block is \", j\n",
"print \"The value of j outside the block is \", j"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The value of j outside the block is 144\n",
"The value of j inside the block is 12\n",
"The value of j outside the block is 12\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.2, page no. 205"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"i = 144\n",
"j = 132\n",
"print \"i = \", i\n",
"if True:\n",
" k = 12\n",
" k = int(raw_input())\n",
" i = i%k\n",
"if i == 0:\n",
" print \"i is a divisor of %d \" %k"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"i = 144\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"25\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.3, page no. 205"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"iNum = 5\n",
"jNum = 30\n",
"if True:\n",
" iNum = 10\n",
" print \"%d %d\" %(iNum, jNum)\n",
"print \"%d %d\" %(iNum, jNum)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10 30\n",
"10 30\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 7, page no. 207"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cName = raw_input(\"Enter a string: \")\n",
"print \"The reverse of the string is: \"\n",
"print cName[::-1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a string: this is a string\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The reverse of the string is: \n",
"gnirts a si siht\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.4 pageno : 206"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"iGlobal = 35\n",
"Number = 47\n",
"iVal = 99\n",
"Number = 1000\n",
"print \"The value of local variable Number is %d\"%Number\n",
"print \"The value of global variable iGlobal is %d\"%iGlobal\n",
"print \"The value of global variable Number is %d\"%Number\n",
"print \"The value of static global variable iVal is %d\"%iVal\n",
"\n",
"# Python has no static functionality. and also local variable changes value when value assigned to it."
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The value of local variable Number is 1000\n",
"The value of global variable iGlobal is 35\n",
"The value of global variable Number is 1000\n",
"The value of static global variable iVal is 99\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.5, page no. 208"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Count = 1\n",
"def PrintCount():\n",
" global Count\n",
" print \"Count = %d\" %Count\n",
" Count = Count + 1\n",
"PrintCount()\n",
"PrintCount()\n",
"PrintCount()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Count = 1\n",
"Count = 2\n",
"Count = 3\n"
]
}
],
"prompt_number": 19
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.6, page no.208"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"uF1 = 0\n",
"uF2 = 1\n",
"def PrintNextFibo():\n",
" global uF1,uF2\n",
" uFpre = uF1 + uF2\n",
" print \"%d\" %uFpre ,\n",
" uF2 = uF1\n",
" uF1 = uFpre\n",
"print \"Fibonacci sequences using static variables :\"\n",
"print \"Enter how many number are to be generated :\",\n",
"itotNum = int(raw_input())\n",
"print \"The first %d fibonacci numbers are\" %itotNum\n",
"for iIndex in range(1,itotNum+1):\n",
" PrintNextFibo()\n",
"\n",
"# it starts with zero but here function created called uses global variable so we need to print/assume manually zero value."
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Fibonacci sequences using static variables :\n",
"Enter how many number are to be generated :"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" The first 5 fibonacci numbers are\n",
"1 1 2 3 5\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example on page no. 209"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import linsrch\n",
"import bubble\n",
"arr = []\n",
"num = 5\n",
"i = 0\n",
"pos = 1\n",
"print \"Enter five elements\"\n",
"while i < num:\n",
" arr.append(int(raw_input()))\n",
" i+=1\n",
"print \"Enter element to be searched: \",\n",
"key = int(raw_input())\n",
"pos = linsrch.LinSrch(arr, num, key)\n",
"if pos:\n",
" print \"Element %d found at %d position\" %(key, pos)\n",
"else:\n",
" print \"Ellement %d not found\" %key\n",
"bubble.BubSort(arr, num)\n",
"print \"The sorted element are\"\n",
"for ele in arr:\n",
" print ele,"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five elements\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"12\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"15\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"56\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"48\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"22\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter element to be searched: "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"22\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" Element 22 found at 5 position\n",
"The sorted element are\n",
"12 15 22 48 56\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example on page no. 211"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import myglobal\n",
"globalVar = 10\n",
"print \"globalVar before calling = \", globalVar\n",
"myglobal.inc_global(globalVar)\n",
"print \"globalVar after calling \", globalVar"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"globalVar before calling = 10\n",
"globalVar after calling 10\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example on page no. 212"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import myglobal\n",
"\n",
"globalVar = 10\n",
"print \"globalVar before calling = \", globalVar\n",
"g = myglobal.inc_global(globalVar)\n",
"print \"globalVar after calling \", g"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"globalVar before calling = 10\n",
"globalVar after calling 11\n"
]
}
],
"prompt_number": 7
}
],
"metadata": {}
}
]
}
|