summaryrefslogtreecommitdiff
path: root/Computer_Programming_Theory_and_Practice_/Chapter3.ipynb
blob: 04d24871c8fe9f15c8b16976b991fb798a022573 (plain)
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
{
 "metadata": {
  "name": "",
  "signature": "sha256:0ff7bb47f00bae9dc5a49237df7011debd17443ba836341d55e8fe830338ade7"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 3: Input/Output Functions and Statements"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 1 , Page number: CP-39"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#program to add and find product of two numbers\n",
      "\n",
      "#variable declaration\n",
      "a = 5\n",
      "b = 3\n",
      "\n",
      "# calculation of sum and product \n",
      "\n",
      "summ = a + b\n",
      "product = a * b\n",
      "\n",
      "print a,b\n",
      "print summ,product"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5 3\n",
        "8 15\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 2 , Page number: CP-45"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to convert degree fahrenheit to celcius \n",
      "# variable declaration\n",
      "\n",
      "f = 105.00\n",
      "print \"Degree fahrenheit ? %d\" % f\n",
      "# calculation of degree in celcius \n",
      "\n",
      "c = 5.0/9.0 * (f-32)\n",
      "\n",
      "# result \n",
      "print\n",
      "print \"Degree centigrade =%6.2f\" % c"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Degree fahrenheit ? 105\n",
        "\n",
        "Degree centigrade = 40.56\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 3 , Page number: CP-45"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# To find area of a triangle\n",
      "# variable declaration\n",
      "\n",
      "a = 5\n",
      "b = 4\n",
      "c = 6\n",
      "\n",
      "\n",
      "# calculation of area\n",
      "s = float((a+b+c))/2\n",
      "area = (s*(s-a)*(s-b)*(s-c)) ** 0.5\n",
      "\n",
      "# result\n",
      "print \"Enter three sides : %d\" % a,b,c\n",
      "print \n",
      "print \"Area of triangle = %0.2f Sq.units\" % area"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter three sides : 5 4 6\n",
        "\n",
        "Area of triangle = 9.92 Sq.units\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4 , Page number: CP-51"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# To print ASCII value of a given character \n",
      "# Variable declaration\n",
      "\n",
      "ch = \"A\"\n",
      "print \"Enter a character : \" , ch\n",
      "\n",
      "# Calculation of ASCII value of a character\n",
      "\n",
      "print \n",
      "print \"ASCII value of \" + ch + \" is\" ,ord(ch)\n",
      "print \"Press any key to stop. . .\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a character :  A\n",
        "\n",
        "ASCII value of A is 65\n",
        "Press any key to stop. . .\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5 , Page number: CP-51"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# To print electricity for consumers\n",
      "# Variable declaration\n",
      "\n",
      "sno = \"TMR65358\"\n",
      "pmr = 4305\n",
      "cmr = 4410\n",
      "\n",
      "print \"Enter service number :\" ,sno\n",
      "print \"Previous meter reading ?\",pmr\n",
      "print \"Current meter reading ?\",cmr\n",
      "\n",
      "# Calculation of electricity charges\n",
      "\n",
      "units = cmr - pmr\n",
      "amt = units * 1.50\n",
      "\n",
      "# Result\n",
      "\n",
      "print\n",
      "print \"         Electricity Bill\"\n",
      "print \"         ----------------\"\n",
      "print \"Service No :\",sno\n",
      "print \"Unit Consumed :\",units\n",
      "print \"Electricity Charges : Rs.%0.2f\" % amt"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter service number : TMR65358\n",
        "Previous meter reading ? 4305\n",
        "Current meter reading ? 4410\n",
        "\n",
        "         Electricity Bill\n",
        "         ----------------\n",
        "Service No : TMR65358\n",
        "Unit Consumed : 105\n",
        "Electricity Charges : Rs.157.50\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6 , Page number: CP-52"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to swap value of two variables\n",
      "# Variable declaration\n",
      "\n",
      "a = 15\n",
      "b = 250 \n",
      "\n",
      "print \"Enter value to A :\",a\n",
      "print \"Enter value to B :\",b\n",
      "\n",
      "# Swapping\n",
      "\n",
      "temp = a\n",
      "a = b\n",
      "b = temp\n",
      "\n",
      "print \n",
      "print \"Value of A =\",a\n",
      "print \"Value of B =\",b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter value to A : 15\n",
        "Enter value to B : 250\n",
        "\n",
        "Value of A = 250\n",
        "Value of B = 15\n"
       ]
      }
     ],
     "prompt_number": 7
    }
   ],
   "metadata": {}
  }
 ]
}