summaryrefslogtreecommitdiff
path: root/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch2.ipynb
blob: 606044d03e6784e6db9170a969adf997c94226d9 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
{
 "metadata": {
  "name": "CH2"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.1 Boolean Variables\n'''\n\n# prints the value of a boolean variable:\nflag=False\nprint \"flag = %r\" % flag\nflag = True\nprint \"flag = %r\" % flag",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "flag = False\nflag = True\n"
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.2 Character Variables\n'''\n\n# prints the character and its internally stored\nc='A'\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\nc='t'\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\nc='\\t' # the tab character\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\nc='!'\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "c = A, int(c) = 65\nc = t, int(c) = 116\nc = \t, int(c) = 9\nc = !, int(c) = 33\n"
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.3 Integer Type Ranges\nThis program prints the numeric ranges of the Python\n'''\nimport sys\n# defines the constants SHRT_MIN, etc.\nprint 'maximum limit int : ',\nprint sys.maxint\nprint 'float info'\nprint sys.float_info",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "maximum limit int :  2147483647\nfloat info\nsys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)\n"
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.4 Integer Arithmetic\nThis example illustrates how the arithmetic operators work.\n'''\n\n# tests operators +, -, *, /, and %:\nm=54\nn=20\nprint \"m = %d and n = %d\" %(m,n)\nprint \"m+n = %d\" % (m+n) # 54+20 = 74\nprint \"m-n = %d\" % (m-n)  # 54-20 = 34\nprint \"m*n = %d\" % (m*n)# 54*20 = 1080\nprint \"m/n = %d\" % (m/n) # 54/20 = 2\nprint \"m modulo by n = %d\" % (m%n)  # 54%20 = 14",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "m = 54 and n = 20\nm+n = 74\nm-n = 34\nm*n = 1080\nm/n = 2\nm modulo by n = 14\n"
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.5 Applying the Pre-increment and Post-increment Operators\n'''\n\n# shows the difference between m++ and ++m:\nm = 44\nm += 1\nn = m\nprint \"m = %d , n = %d\" %(m,n)\nm = 44\nn = m # the post-increment operator is applied to m\nm += 1\nprint \"m = %d , n = %d\" %(m,n)\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "m = 45 , n = 45\nm = 45 , n = 44\n"
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.6 Applying Composite Arithmetic Assignment Operators\n'''\n\n# tests arithmetic assignment operators:\nn=22\nprint  \"n = %d\" % n\nn += 9 # adds 9 to n\nprint  \"After n += 9, n = %d\" % n\nn -= 5 # subtracts 5 from n\nprint  \"After n -= 5, n = %d\" % n\nn *= 2 # multiplies n by 3\nprint  \"After n *= 2, n = %d\" % n \nn /= 3 # divides n by 9\nprint  \"After n /= 3, n = %d\" % n \nn %= 7 # reduces n to the remainder from dividing by 4\nprint 'After n modulo by 7   n = %d' %n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "n = 22\nAfter n += 9, n = 31\nAfter n -= 5, n = 26\nAfter n *= 2, n = 52\nAfter n /= 3, n = 17\nAfter n modulo by 7   n = 3\n"
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.7 Floating-Point Arithmetic\nThis program is nearly the same as the one in Example 2.4. The important difference is that these\nvariables are declared to have the floating-point type double instead of the integer type int.\n'''\n\n# tests the floating-point operators +, -, *, and /:\nx=54.0\ny=20.0\nprint  \"x = %f and y = %f\" %(x,y)\nprint  \"x+y = %f\"  %(x+y) # 54.0+20.0 = 74.0\nprint  \"x-y = %f\" % (x-y) # 54.0-20.0 = 34.0\nprint  \"x*y = %f\" %( x*y) # 54.0*20.0 = 1080.0\nprint  \"x/y = %f\" % (x/y) # 54.0/20.0 = 2.7\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "x = 54.000000 and y = 20.000000\nx+y = 74.000000\nx-y = 34.000000\nx*y = 1080.000000\nx/y = 2.700000\n"
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.8 Using the sizeof Operator\nThis program tells you how much space each of the undamental types uses:\nNote : Python has few number of data types so output would be differ.\n'''\nimport sys\n# prints the storage sizes of the fundamental types:\nprint \"Number of bytes used:\\n\"\n\nprint \" char: %d \" % sys.getsizeof('a')\nprint \" int : %d \" % sys.getsizeof(int(1))\nprint \" string : %d \" % sys.getsizeof(str('hellololdei'))\nprint \"float : %d\" % sys.getsizeof(float(1.1))",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Number of bytes used:\n\n char: 22 \n int : 12 \n string : 32 \nfloat : 16\n"
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.9 Reading from the <cfloat> Header File\nThis program tells you the precision and magnitude range that the float type has on your system:\n'''\nimport sys\n# prints the storage sizes of the fundamental types:\nfbits = 8*sys.getsizeof(float(123))\n\n# each byte contains 8 bits\nprint \"float uses : %d bits:\\n\\t\" %  fbits \n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "float uses : 128 bits:\n\t\n"
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nThis program casts a double value into int value:\n'''\n\n# casts a double value as an int:\nv = 1234.56789\nn = int(v);\nprint \"v = %f, n = %d\" %(v,n)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "v = 1234.567890, n = 1234\n"
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.11 Promotion of Types\nThis program promotes a char to a short to an int to a float to a double:\nNote : Python is type independent. So would give output differ.\n'''\n# prints promoted vales of 65 from char to double:\nc='A'\nprint \"char c = \" + c\nk=c;\nprint \"k = \" + k \nm=k;\nprint \"m = \" + m \nn=m\nprint \"n = \" + n\nx=m\nprint \"x = \" + x \ny=x\nprint \"y = \" + y",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "char c = A\nk = A\nm = A\nn = A\nx = A\ny = A\n"
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.12 Integer Overflow\nThis program repeatedly multiplies n by 1000 until it overflows.\n'''\n# prints n until it overflows:\nn=1000\nprint \"n =  %d\" % n\nn *= 1000 # multiplies n by 1000\nprint \"n =  %d\" % n\nn *= 1000 # multiplies n by 1000\nprint \"n =  %d\" % n\nn *= 1000 # multiplies n by 1000\nprint \"n =  %d\" % n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "n =  1000\nn =  1000000\nn =  1000000000\nn =  1000000000000\n"
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.13 Floating-point Overflow\nThis program is similar to the one in Example 2.12. It repeatedly squares x until it overflows.\n'''\n\n# prints x until it overflows:\nx=1000.0\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "x = 1000.000000\nx = 1000000.000000\nx = 1000000000000.000000\nx = 999999999999999983222784.000000\nx = 1000000000000000043845843045076197354634047651840.000000\n"
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.14 Round-off Error\nThis program does some simple arithmetic to illustrate roundoff error:\n'''\n\n# illustrates round-off error::\nx = 1000/3.0\nprint \"x = %f\" %x # x = 1000/3\ny = x - 333.0\nprint \"y = %f\" % y # y = 1/3\nz = 3*y - 1.0\nprint \"z = %f\" %z # z = 3(1/3) - 1\nif (z == 0):\n    print \"z == 0.\\n\"\nelse:\n    print \"z does not equal 0.\\n\" # z != 0\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "x = 333.333333\ny = 0.333333\nz = -0.000000\nz does not equal 0.\n\n"
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.15 Hidden Round-off Error\nThis program implements the quadratic formula to solve quadratic equations.\n'''\nimport math\n\n# implements the quadratic formula\na = float(raw_input(\"Enter the coefficients of a quadratic equation:\\n a : \"))\nb = float(raw_input('b : '))\nc = float(raw_input('c : '))\n\nprint \"The equation is: \",\nprint a,\nprint \"*x*x + \",\nprint b,\nprint \"*x + \" ,\nprint c,\nprint  \" = 0\" \n\nd = b*b - 4*a*c # discriminant\nsqrtd = math.sqrt(d)\nx1 = (-b + sqrtd)/(2*a)\nx2 = (-b - sqrtd)/(2*a)\nprint \"The solutions are:\"\nprint \"\\tx1 = %f\" % x1\nprint \"\\tx2 = %f\" % x2\nprint \"Check:\" \nprint \"\\ta*x1*x1 + b*x1 + c = %f\" %( a*x1*x1 + b*x1 + c)\nprint \"\\ta*x2*x2 + b*x2 + c =  %f\" %( a*x2*x2 + b*x2 + c)\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "Enter the coefficients of a quadratic equation:\n a : 2\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "b : 1\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "c : -3\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "The equation is:  2.0 *x*x +  1.0 *x +  -3.0  = 0\nThe solutions are:\n\tx1 = 1.000000\n\tx2 = -1.500000\nCheck:\n\ta*x1*x1 + b*x1 + c = 0.000000\n\ta*x2*x2 + b*x2 + c =  0.000000\n"
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.17 Scientific Format\nThis program shows how floating-point values may be input in scientific format:\n'''\n\n# prints double values in scientific e-format:\nx = float(raw_input(\"Enter float: \"))\nprint \"Its reciprocal is: \",\nprint  1/x ",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "Enter float: 234.567e89\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Its reciprocal is:  4.2631742743e-92\n"
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.18 Scope of Variables\nNOte : Python does not have variable scopes. Once variable is declared then it stays live until program gets exit.\nso it wont give any errors.\n\n'''\n# illustrates the scope of variables:\nx = 11\n# ERROR: this is not in the scope of x\nif True:\n    x = 22 # OK: this is in the scope of x\n    y = 33 # ERROR: this is not in the scope of y\n    x = 44 # OK: this is in the scope of x\n    y = 55 # OK: this is in the scope of y\nx = 66 # OK: this is in the scope of x\ny = 77 # ERROR: this is not in the scope of y\n",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 2.19 Nested and Parallel Scopes\nNOte : Python does not have variable scopes. Once variable is declared then it stays live until program gets exit.\nso output would be differ.\n'''\n# this x is global\nx = 11\n\nif True:\n    # illustrates the nested and parallel scopes:\n    x = 22\n    # begin scope of internal block\n    if True:\n        x = 33\n        print \"In block inside main(): x =  %d \" % x\n    # end scope of internal block\nprint \"In main(): x =  %d\" %x \nprint \"In main(): x = %d \"% x",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "In block inside main(): x =  33 \nIn main(): x =  33\nIn main(): x = 33 \n"
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "",
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}