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
|
{
"metadata": {
"name": "Chapter 10"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": "Chapter 10: C Preprocessor"
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 10.1, Page number: 172"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 10.1.py\n# To print the contents of two arrays upto 10 indices\n\n\n# Variable declaration\ndata = []\ntwice = []\n\n# Calculation and result\nfor index in range (0, 10) :\n data.append(index)\n twice.append(2 * index)\n\nprint (data)\nprint (twice)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n"
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 10.2, Page number: 173"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 10.2.py\n# To print the contents of two arrays upto 20 indices \n\n\n# Variable declaration\ndata = []\ntwice = []\n\n# Calculation and result\nfor index in range (0, 20) :\n data.append(index)\n twice.append(2 * index)\n\nprint (data)\nprint (twice)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]\n"
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 10.3, Page number: 175"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 10.3.py\n# To print the value of variable 'index' \n\n\n# Variable declaration\nBIG_NUMBER = 10 * 10\nindex = 1\n\n# Calculation and result\nwhile (index < BIG_NUMBER) :\n index = index * 8\n print ('%d' % index)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "8\n64\n512\n"
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 10.4, Page number: 176"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 10.4.py\n# To determine the square of ALL_PARTS \n\n\n# Variable declaration\nFIRST_PART = 7\nLAST_PART = 5\nALL_PARTS = FIRST_PART + LAST_PART\n\n# Calculation and result\nprint ('The square of all the parts is %d' % (ALL_PARTS * ALL_PARTS))",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "The square of all the parts is 144\n"
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 10.5, Page number: 177"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 10.5.py\n# To print 'Hi there' 10 times \n\n\n# Calculation and result\nfor i in reversed (range(10)) :\n print ('Hi there')",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Hi there\nHi there\nHi there\nHi there\nHi there\nHi there\nHi there\nHi there\nHi there\nHi there\n"
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 10.6, Page number: 177"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 10.6.py\n# To print the value of 'size' \n\n\n# Variable declaration\nsize = 10\nfudge = size - 2\n\n# Calculation and result\nsize = fudge\nprint ('Size is %d' % size)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Size is 8\n"
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 10.7, Page number: 178"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 10.7.py\n# To exit the program based on a specific condition \n\n\n# Variable declaration\nimport sys\nvalue = 1\n\n# Calculation and result\nif (value < 0) :\n sys.exit()\n\nprint ('We did not die')",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "We did not die\n"
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 10.8, Page number: 184"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 10.8.py\n# To display the square of numbers from 1 to 5 \n\n\n# Function declaration, calculation and result\ndef SQR (x) :\n return x * x\n \nfor counter in range (0, 5) :\n print ('x %d, x squared %d\\n' % (counter + 1, SQR (counter + 1)))",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "x 1, x squared 1\n\nx 2, x squared 4\n\nx 3, x squared 9\n\nx 4, x squared 16\n\nx 5, x squared 25\n\n"
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 10.9, Page number: 184"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 10.9.py\n# To display the square of numbers from 1 to 5 \n\n\n# Variable declaration\ncounter = 0\n\n# Function declaration, calculation and result\ndef SQR (x) :\n return x * x\n \nwhile (counter < 5) :\n print ('x %d square %d\\n' % (counter + 1, SQR (counter + 1)))\n counter += 1",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "x 1 square 1\n\nx 2 square 4\n\nx 3 square 9\n\nx 4 square 16\n\nx 5 square 25\n\n"
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Example 10.10, Page number: 185"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Example 10.10.py\n# To calculate the reciprocal of numbers from 1 to 9\n\n\n# Function declaration, calculation and result\ndef reciprocal (number) :\n return 1 / number\n \nfor counter in range (1, 10) :\n print ('1/%f = %f\\n' % (counter, reciprocal (counter)))",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "1/1.000000 = 1.000000\n\n1/2.000000 = 0.000000\n\n1/3.000000 = 0.000000\n\n1/4.000000 = 0.000000\n\n1/5.000000 = 0.000000\n\n1/6.000000 = 0.000000\n\n1/7.000000 = 0.000000\n\n1/8.000000 = 0.000000\n\n1/9.000000 = 0.000000\n\n"
}
],
"prompt_number": 10
}
],
"metadata": {}
}
]
}
|