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
|
{
"metadata": {
"name": "",
"signature": "sha256:dc372127172fa28ae076732c5c6607c2a4f0be3eed0d850c34c59df8b47971c6"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Hour 14: Uderstanding Scope and Storage Classes"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 14.1, Page No 225"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"i=32\n",
"print \"Within the outer block: i=%d\" % i\n",
"#There are no direct blocks in Python. We will do it in different way\n",
"print \"Within the inner block\"\n",
"def fu():\n",
" j = 10\n",
" for i in range(11):\n",
" print \"i=\", i, \"j=\",j\n",
" j -= 1\n",
"fu()\n",
"print \"Within the outer block: i=%d\" % i"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Within the outer block: i=32\n",
"Within the inner block\n",
"i= 0 j= 10\n",
"i= 1 j= 9\n",
"i= 2 j= 8\n",
"i= 3 j= 7\n",
"i= 4 j= 6\n",
"i= 5 j= 5\n",
"i= 6 j= 4\n",
"i= 7 j= 3\n",
"i= 8 j= 2\n",
"i= 9 j= 1\n",
"i= 10 j= 0\n",
"Within the outer block: i=32\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 14.2, Page No 227"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Python does'nt provide block creating facility like using just {} with out any name hence i have implemented in this way\n",
"\n",
"def function_1():\n",
" x = 1234\n",
" y = 1.234567\n",
" print \"From fuction_1:\"\n",
" print \"x=\", x, \", y=\", y\n",
"\n",
"x = 4321\n",
"function_1()\n",
"print \"Within the main block:\" \n",
"print \"x=%d, y=%f\" %(x, y)\n",
"\n",
"y = 7.654321\n",
"function_1()\n",
"print \"Within the nested block:\"\n",
"print \"x=%d, y=%f\" %(x, y)\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"From fuction_1:\n",
"x= 1234 , y= 1.234567\n",
"Within the main block:\n",
"x=4321, y=7.654321\n",
"From fuction_1:\n",
"x= 1234 , y= 1.234567\n",
"Within the nested block:\n",
"x=4321, y=7.654321\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 14.3, Page No 230"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"counter =0\n",
"def add_two(x,y):\n",
" global counter\n",
" counter = counter+1\n",
" print \"This is the function call of %d,\" % counter\n",
" return (x+y)\n",
"j = 5\n",
"for i in range(5):\n",
" print \"The addition of \"+ str(i) +\" and \"+str(j)+\" is \"+str(add_two(i,j))\n",
" j=j-1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"This is the function call of 1,\n",
"The addition of 0 and 5 is 5\n",
"This is the function call of 2,\n",
"The addition of 1 and 4 is 5\n",
"This is the function call of 3,\n",
"The addition of 2 and 3 is 5\n",
"This is the function call of 4,\n",
"The addition of 3 and 2 is 5\n",
"This is the function call of 5,\n",
"The addition of 4 and 1 is 5\n"
]
}
],
"prompt_number": 11
}
],
"metadata": {}
}
]
}
|