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
|
{
"metadata": {
"name": "Chapter 2"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "<h1>Chapter 2: An Overview of C++<h1>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.1, Page Number: 12<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Program 1:A first Python(C++) program'''\n\n#Result\nprint \"This is my first C++ program.\"",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "This is my first C++ program.\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.2, Page Number: 17<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Program 2: Using a variable'''\n\n#Variable Declaration\nx=None \n\nx=1023 #this assigns 1023 to x\n\n#Result\nprint \"This program prints the value of x: \",x #prints x,i.e, 1023\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "This program prints the value of x: 1023\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.3, Page Number: 18<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''This program converts gallons to liters'''\n\n#Variable declaration\ngallons=10 #User input\nliters=None\n\nliters=gallons*4 #convert to liters\n\n#Result\nprint \"Liters: \",liters",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Liters: 40\n"
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.4, Page Number: 20<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''This program converts gallons to liters using floating point numbers'''\n\n#Variable declaration\ngallons=10.20 #User input\nliters=None\n\nliters=gallons*3.7854 #convert to liters\n\n#Result\nprint \"Liters: \",liters",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Liters: 38.61108\n"
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.5, Page Number: 21<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''This program uses a function'''\n\n#myfunc's definition\ndef myfunc():\n print \"Inside myfunc() \"\n \nprint \"In main()\"\nmyfunc() #call myfunc()\nprint \"Back in main()\"",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "In main()\nInside myfunc() \nBack in main()\n"
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.6, Page Number: 22<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Using the abs() function'''\n\n#Result\nprint abs(-10)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "10\n"
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.7, Page Number: 23<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''A simple program that demonstrates mul()'''\n\n#mul's definition\ndef mul(x,y):\n print x*y,\n\n#calling mul\nmul(10,20)\nmul(5,6)\nmul(8,9)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "200 30 72\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.8, Page Number: 24<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Returning a value'''\n\n#mul()'s definition; this function returns a value\ndef mul(x,y):\n return x*y #return product of x and y\n\n#Variable declaration\nanswer=mul(10,11) #assign return values\n\n#Result\nprint \"The answer is: \",answer\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "The answer is: 110\n"
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.9, Page Number: 26<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''This demonstrates how to got to the next line'''\n\nprint \"one\"\nprint \"two\" #prints in different line\nprint \"three\",\"four\" #prints all in same line",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "one\ntwo\nthree four\n"
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.10, Page Number: 27<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''This program illustrates the if statement'''\n\n#Variable declaration\na=10 #user input for two numbers\nb=20\n\n#Result\nif a<b:\n print \"First number is less than second. \"",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "First number is less than second. \n"
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.11, Page Number: 28<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''A program that illustrates the for loop'''\n\nfor count in range(1,100+1):\n print count,",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "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\n"
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 2.12, Page Number: 30<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''This program demonstrates a block of code'''\n\n#Variable declaration\na=10 #User input for two numbers\nb=20\n\n#Result\nif a<b:\n print \"First number is less than second\"\n print \"Their difference is: \",b-a\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "First number is less than second\nTheir difference is: 10\n"
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|