summaryrefslogtreecommitdiff
path: root/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb
blob: 6ef37c88ed11dac7dc612fe55e54f716451b4859 (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
{
 "metadata": {
  "name": "ch5"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.1 The Square Root Function sqrt()\n'''\nimport math\n\n# tests the sqrt() function:\nfor i in range(0,6):\n    print \"\\t %d \\t %f\" %(i,math.sqrt(i))",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\t 0 \t 0.000000\n\t 1 \t 1.000000\n\t 2 \t 1.414214\n\t 3 \t 1.732051\n\t 4 \t 2.000000\n\t 5 \t 2.236068\n"
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.2 Testing a Trigonometry Identity\n'''\nimport math\n# tests the identity sin 2x = 2 sin x cos x:\nx = 0\nwhile x < 2:\n    print \"%f  \\t\\t %f \\t %f\" %(x,math.sin(2*x),2*math.sin(x)*math.cos(x))\n    x += 0.2\n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "0.000000  \t\t 0.000000 \t 0.000000\n0.200000  \t\t 0.389418 \t 0.389418\n0.400000  \t\t 0.717356 \t 0.717356\n0.600000  \t\t 0.932039 \t 0.932039\n0.800000  \t\t 0.999574 \t 0.999574\n1.000000  \t\t 0.909297 \t 0.909297\n1.200000  \t\t 0.675463 \t 0.675463\n1.400000  \t\t 0.334988 \t 0.334988\n1.600000  \t\t -0.058374 \t -0.058374\n1.800000  \t\t -0.442520 \t -0.442520\n2.000000  \t\t -0.756802 \t -0.756802\n"
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.3 A cube() Function\nHere is a simple example of a user-defined function:\n'''\n\ndef cube(x):\n    # returns cube of x:\n    return x*x*x\n\n",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.4 A Test Driver for the cube() Function\nHere is a complete program that includes the definition of the cube() function from Example 5.4\ntogether with a test driver for it:\n'''\n\ndef cube(x):\n    # returns cube of x:\n    return x*x*x\n\n# tests the cube() function:\nn=1\nwhile (n != 0):\n    n = int(raw_input())\n    print  \"\\tcube( %d ) =  %d\" %(n,cube(n))",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "4\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tcube( 4 ) =  64\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "2\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tcube( 2 ) =  8\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "9\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tcube( 9 ) =  729\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "0\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tcube( 0 ) =  0\n"
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.5 A Test Driver for the max() Function\nHere is a function with two parameters. It returns the larger of the two values passed to it.\n'''\n\ndef maximum(x,y):\n    # returns larger of the two given integers:\n    if (x < y):\n        return y\n    else:\n        return x\n\n# tests the max() function:\nm = 1\nn = 1\nwhile m != 0: \n    m = int(raw_input())\n    n = int(raw_input())\n    print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "5\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "2\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tmax( 5 , 2 ) = 5\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "0\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "3\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tmax( 0 , 3 ) = 3\n"
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.6 The max() Function with Declaration Separate from Definition\n'''\n\ndef maximum(x,y):\n    # returns larger of the two given integers:\n    if (x < y):\n        return y\n    else:\n        return x\n\n# tests the max() function:\nm = 1\nn = 1\nwhile m != 0: \n    m = int(raw_input())\n    n = int(raw_input())\n    print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "5\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "2\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tmax( 5 , 2 ) = 5\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "0\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "3\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tmax( 0 , 3 ) = 3\n"
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.8 The max() Function Compiled Separately\n'''\n\n\n# returns larger of the two given integers:\n\nm = 1\nn = 1\nwhile m!=0:\n    m = int(raw_input())\n    n = int(raw_input())\n    print \"\\tmax(%d,%d) = %d\" %(m,n, max(m,n))\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "5\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "4\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tmax(5,4) = 5\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "4\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "3\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tmax(4,3) = 4\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "8\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "0\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tmax(8,0) = 8\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "0\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "5\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\tmax(0,5) = 5\n"
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.9 The Factorial Function\n'''\n\ndef fact(n):\n    if (n < 0):\n        return 0\n    f = 1\n    while (n > 1):\n        f *= n\n        n -= 1\n    return f\n\nfor i in range(-1,6):\n    print fact(i),\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "0 1 1 2 6 24 120\n"
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.10 The Permutation Function\n'''\ndef fact(n):\n    if (n < 0):\n        return 0\n    f = 1\n    while (n > 1):\n        f *= n\n        n -= 1\n    return f\n\n\ndef perm(n,k):\n    # returns P(n,k), the number of permutations of k from n:\n    if (n < 0 or k < 0 or k > n):\n        return 0\n    return fact(n)/fact(n-k)\n\nfor i in range(-1,8):\n    for j in range(-1,i+2):\n        print perm(i,j),\n    print ''\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "0 0 \n0 1 0 \n0 1 1 0 \n0 1 2 2 0 \n0 1 3 6 6 0 \n0 1 4 12 24 24 0 \n0 1 5 20 60 120 120 0 \n0 1 6 30 120 360 720 720 0 \n0 1 7 42 210 840 2520 5040 5040 0 \n"
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.11 A Function that Prints Dates\n'''\n\ndef printDate(m,d,y):\n    # prints the given date in literal form:\n    if (m < 1 or m > 12 or d < 1 or d > 31 or y < 0):\n        print \"Error: parameter out of range.\\n\"\n        return\n    if m == 1:\n        print \"January \",\n    elif m ==2:\n        print \"February \",\n    elif m==3 :\n        print \"March \",\n    elif m==4:\n        print \"April \",\n    elif m==5:\n        print \"May \",\n    elif m==6:\n        print \"June \",\n    elif m==7:\n        print \"July \",\n    elif m==8:\n        print \"August \",\n    elif m==9:\n        print \"September \",\n    elif m==10:\n        print \"October \",\n    elif m==1:\n        print \"November \",\n    else:\n        print \"December \",\n    print d , \", \",  y \n\n# tests the printDate() function:\nmonth = 1\nwhile month > 0:\n    month = int(raw_input())\n    day = int(raw_input())\n    year = int(raw_input())\n    printDate(month,day,year)\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "9\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "12\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "1989\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "September  12 ,  1989\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "0\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "5\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "2001\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Error: parameter out of range.\n\n"
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.12 Classifying Characters\n'''\nimport string\ndef ispunct(s):\n    return all(c in string.punctuation for c in s)\ndef printCharCategory(c):\n    # prints the category to which the given character belongs:\n    print \"The character [\" + c + \"] is a \",\n    if(c.isdigit()):\n        print \"digit.\\n\"\n    elif (c.islower()):\n        print \"lower-case letter.\\n\"\n    elif (c.isupper()): \n        print \"capital letter.\\n\"\n    elif (c.isspace()):\n        print \"white space character.\\n\"\n    elif (ord(c) >= 10 and ord(c) <= 15 or ord(c) == 0):\n        print \"control character.\\n\"\n    elif (ispunct(c)):\n        print \"punctuation mark.\\n\"\n    else:\n        print \"Error.\\n\"\n\n# prints the category to which the given character belongs;\n# tests the printCharCategory() function:\nfor c in range(128):\n    printCharCategory(chr(c))\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": " The character [\u0000] is a  control character.\n\nThe character [\u0001] is a  Error.\n\nThe character [\u0002] is a  Error.\n\nThe character [\u0003] is a  Error.\n\nThe character [\u0004] is a  Error.\n\nThe character [\u0005] is a  Error.\n\nThe character [\u0006] is a  Error.\n\nThe character [\u0007] is a  Error.\n\nThe character [\b] is a  Error.\n\nThe character [\t] is a  white space character.\n\nThe character [\n] is a  white space character.\n\nThe character [\u000b] is a  white space character.\n\nThe character [\f] is a  white space character.\n\nThe character [\r] is a  white space character.\n\nThe character [\u000e] is a  control character.\n\nThe character [\u000f] is a  control character.\n\nThe character [\u0010] is a  Error.\n\nThe character [\u0011] is a  Error.\n\nThe character [\u0012] is a  Error.\n\nThe character [\u0013] is a  Error.\n\nThe character [\u0014] is a  Error.\n\nThe character [\u0015] is a  Error.\n\nThe character [\u0016] is a  Error.\n\nThe character [\u0017] is a  Error.\n\nThe character [\u0018] is a  Error.\n\nThe character [\u0019] is a  Error.\n\nThe character [\u001a] is a  Error.\n\nThe character [\u001b] is a  Error.\n\nThe character [\u001c] is a  Error.\n\nThe character [\u001d] is a  Error.\n\nThe character [\u001e] is a  Error.\n\nThe character [\u001f] is a  Error.\n\nThe character [ ] is a  white space character.\n\nThe character [!] is a  punctuation mark.\n\nThe character [\"] is a  punctuation mark.\n\nThe character [#] is a  punctuation mark.\n\nThe character [$] is a  punctuation mark.\n\nThe character [%] is a  punctuation mark.\n\nThe character [&] is a  punctuation mark.\n\nThe character ['] is a  punctuation mark.\n\nThe character [(] is a  punctuation mark.\n\nThe character [)] is a  punctuation mark.\n\nThe character [*] is a  punctuation mark.\n\nThe character [+] is a  punctuation mark.\n\nThe character [,] is a  punctuation mark.\n\nThe character [-] is a  punctuation mark.\n\nThe character [.] is a  punctuation mark.\n\nThe character [/] is a  punctuation mark.\n\nThe character [0] is a  digit.\n\nThe character [1] is a  digit.\n\nThe character [2] is a  digit.\n\nThe character [3] is a  digit.\n\nThe character [4] is a  digit.\n\nThe character [5] is a  digit.\n\nThe character [6] is a  digit.\n\nThe character [7] is a  digit.\n\nThe character [8] is a  digit.\n\nThe character [9] is a  digit.\n\nThe character [:] is a  punctuation mark.\n\nThe character [;] is a  punctuation mark.\n\nThe character [<] is a  punctuation mark.\n\nThe character [=] is a  punctuation mark.\n\nThe character [>] is a  punctuation mark.\n\nThe character [?] is a  punctuation mark.\n\nThe character [@] is a  punctuation mark.\n\nThe character [A] is a  capital letter.\n\nThe character [B] is a  capital letter.\n\nThe character [C] is a  capital letter.\n\nThe character [D] is a  capital letter.\n\nThe character [E] is a  capital letter.\n\nThe character [F] is a  capital letter.\n\nThe character [G] is a  capital letter.\n\nThe character [H] is a  capital letter.\n\nThe character [I] is a  capital letter.\n\nThe character [J] is a  capital letter.\n\nThe character [K] is a  capital letter.\n\nThe character [L] is a  capital letter.\n\nThe character [M] is a  capital letter.\n\nThe character [N] is a  capital letter.\n\nThe character [O] is a  capital letter.\n\nThe character [P] is a  capital letter.\n\nThe character [Q] is a  capital letter.\n\nThe character [R] is a  capital letter.\n\nThe character [S] is a  capital letter.\n\nThe character [T] is a  capital letter.\n\nThe character [U] is a  capital letter.\n\nThe character [V] is a  capital letter.\n\nThe character [W] is a  capital letter.\n\nThe character [X] is a  capital letter.\n\nThe character [Y] is a  capital letter.\n\nThe character [Z] is a  capital letter.\n\nThe character [[] is a  punctuation mark.\n\nThe character [\\] is a  punctuation mark.\n\nThe character []] is a  punctuation mark.\n\nThe character [^] is a  punctuation mark.\n\nThe character [_] is a  punctuation mark.\n\nThe character [`] is a  punctuation mark.\n\nThe character [a] is a  lower-case letter.\n\nThe character [b] is a  lower-case letter.\n\nThe character [c] is a  lower-case letter.\n\nThe character [d] is a  lower-case letter.\n\nThe character [e] is a  lower-case letter.\n\nThe character [f] is a  lower-case letter.\n\nThe character [g] is a  lower-case letter.\n\nThe character [h] is a  lower-case letter.\n\nThe character [i] is a  lower-case letter.\n\nThe character [j] is a  lower-case letter.\n\nThe character [k] is a  lower-case letter.\n\nThe character [l] is a  lower-case letter.\n\nThe character [m] is a  lower-case letter.\n\nThe character [n] is a  lower-case letter.\n\nThe character [o] is a  lower-case letter.\n\nThe character [p] is a  lower-case letter.\n\nThe character [q] is a  lower-case letter.\n\nThe character [r] is a  lower-case letter.\n\nThe character [s] is a  lower-case letter.\n\nThe character [t] is a  lower-case letter.\n\nThe character [u] is a  lower-case letter.\n\nThe character [v] is a  lower-case letter.\n\nThe character [w] is a  lower-case letter.\n\nThe character [x] is a  lower-case letter.\n\nThe character [y] is a  lower-case letter.\n\nThe character [z] is a  lower-case letter.\n\nThe character [{] is a  punctuation mark.\n\nThe character [|] is a  punctuation mark.\n\nThe character [}] is a  punctuation mark.\n\nThe character [~] is a  punctuation mark.\n\nThe character [\u007f] is a  Error.\n\n"
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.13 A Function that Tests Primality\n'''\nimport math\ndef isPrime(n):\n    # returns True if n is prime, False otherwise:\n    sqrtn = math.sqrt(n)\n    if (n < 2):\n        return False\n    # 0 and 1 are not primes\n    if (n < 4):\n        return True\n    # 2 and 3 are the first primes\n    if (n%2 == 0):\n        return False\n    # 2 is the only even prime\n    for d in range(3,int(sqrtn+1),2):\n        if (n%d == 0):\n            return False\n        # n has a nontrivial divisor\n    return True;\n\nfor n in range(0,80):\n    if (isPrime(n)):\n        print n,\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79\n"
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.14 A Leap Year Function\n'''\ndef isLeapYear(y):\n    # returns true iff y is a leap year:\n    return (y % 4 == 0 and y % 100 != 0 or y % 400 == 0)\n\n# tests the isLeapYear() function:\nn = 2\nwhile n > 1:\n    n = int(raw_input())\n    if (isLeapYear(n)):\n        print \"%d is a leap year.\" % n\n    else:\n        print \"%d is not a leap year.\" %n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "2004\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "2004 is a leap year.\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "2006\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "2006 is not a leap year.\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "2013\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "2013 is not a leap year.\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "0\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "0 is a leap year.\n"
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.15 A Function for Reading the User's Age\n'''\n\ndef age():\n    # prompts the user to input his/her age, and returns that value:\n    while (True):\n        print \"How old are you: \"\n        n = int(raw_input())\n        if (n < 0):\n            print \"\\a\\tYour age could not be negative.\"\n        elif (n > 120):\n            print \"\\a\\tYou could not be over 120.\"\n        else:\n            return n\n        print \"\\n\\tTry again.\\n\"\n\na = age();\nprint \"\\nYou are %d years old.\" %a\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "How old are you: \n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "-12\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\u0007\tYour age could not be negative.\n\n\tTry again.\n\nHow old are you: \n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "125\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\u0007\tYou could not be over 120.\n\n\tTry again.\n\nHow old are you: \n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "24\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "\nYou are 24 years old.\n"
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.16 The swap() Function\n'''\n\ndef swap(x,y):\n    # exchanges the values of x and y:\n    x[0],y[0] = y[0],x[0]\n\na = [22.2]\nb = [44.4]\nprint \"a = %.2f , b =  %.2f \" %(a[0],b[0])\nswap(a,b)\nprint \"a = %.2f , b =  %.2f \" %(a[0],b[0])\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "a = 22.20 , b =  44.40 \na = 44.40 , b =  22.20 \n"
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.17 \nNote : Python doesn't support pass value by reference. but can be done by passing list.\n'''\n\ndef f(x,y):\n    x[0]= 88\n    y[0] = 99\n\n# tests the f() function:\na = [22]\nb = [44]\nprint \"a = %.2f , b =  %.2f \" %(a[0],b[0])\nf(a,b)\nprint \"a = %.2f , b =  %.2f \" %(a[0],b[0])\nf(2*a,b)\nprint \"a = %.2f , b =  %.2f \" %(a[0],b[0])\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "a = 22.00 , b =  44.00 \na = 88.00 , b =  99.00 \na = 88.00 , b =  99.00 \n"
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.18 Returning More than One Value\n'''\n\ndef computeCircle(r):\n    # returns the area and circumference of a circle with radius r:\n    PI = 3.141592653589793\n    area = PI*r*r\n    circumference = 2*PI*r\n    return area,circumference\n\n# tests the computeCircle() function:\nprint \"Enter radius: \"\nr = int(raw_input())\na,c = computeCircle(r)\nprint \"area = %.2f , circumference = %.2f\" %(a,c)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Enter radius: \n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "5\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "area = 78.54 , circumference = 31.42\n"
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.19\nNote : Python passes variable by value and not by reference. So output would be differ.\n'''\n\n\ndef f(x,y,z):\n    x[0] += z[0]\n    y[0] += z[0]\n    print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n\nx = [22]\ny = [33]\nz = [44]\n\nprint \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\nf(x,y,z)\nprint \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\nx[0] = 2*x[0] - 3\nf(x,y,z)\nprint \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "x = 22 , y = 33 , z = 44\nx = 66 , y = 77 , z = 44\nx = 66 , y = 77 , z = 44\nx = 173 , y = 121 , z = 44\nx = 173 , y = 121 , z = 44\n"
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.20\n'''\ndef cube(x):\n    # returns cube of x:\n    return x*x*x\n\n# tests the cube() function:\nprint cube(4)\nx = int(raw_input())\ny = cube(2*x-3)\nprint y\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "64\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "5\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "343\n"
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.21 Nested and Parallel Scopes\nPython has it's own scope so output would be differ.\n'''\nx = 11\n\ndef f():\n    x = 44\n    print \"In f(): x = %d\" % x \n\ndef g():\n    print \"In g(): x = %d\" % x \n\nx = 22\nx = 33\nprint \"In block inside main(): x = %d\" % x\n\n\nprint \"In main(): x = %d\" % x \nprint \"In main(): ::x = %d\" % x \nf()\ng()\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "In block inside main(): x = 33\nIn main(): x = 33\nIn main(): ::x = 33\nIn f(): x = 44\nIn g(): x = 33\n"
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.22 \n'''\n\ndef max_(x, y,z=0):\n    if x > y and x > y:\n        return x\n    elif y > x and y > z:\n        return y\n    else:\n        return z\n    \n        \nprint max(99,77),  \" \" , max(55,66,33)\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "99   66\n"
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.23 Using the return Statement to Terminate a Program\n'''\n\n# prints the quotient of two input integers:\nprint \"Enter two integers: \"\nn = int(raw_input())\nd = int(raw_input())\nif (d == 0):\n    import sys\n    sys.exit(0)\nprint n , \"/\" , d , \" = \" , n/d \n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Enter two integers: \n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "8\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "2\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "8 / 2  =  4\n"
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.24 Using the exit() Function to Terminate a Program\n'''\n\ndef reciprocal(x):\n    #returns the reciprocal of x:\n    if (x == 0):\n        import sys\n        sys.exit(1); # terminate the program\n    return 1.0/x\n\nx = float(raw_input())\nprint reciprocal(x)\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "25\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "0.04\n"
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 5.25 Default Parameters\nThis function evaluates the third degree polynomial a0 + a1x + a2x2 + a3x3. \n'''\ndef p(x,a0,a1=0,a2=0,a3=0):\n    # returns a0 + a1*x + a2*x^2 + a3*x^3:\n    return (a0 + (a1 + (a2 + a3*x)*x)*x)\n\n\n# tests the p() function:\nx = 2.0003\nprint \"p(x,7) = %f\" % p(x,7)\nprint \"p(x,7,6) = %f\" % p(x,7,6)\nprint \"p(x,7,6,5) = %f\" % p(x,7,6,5)\nprint \"p(x,7,6,5,4) = %f\" % p(x,7,6,5,4)\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "p(x,7) = 7.000000\np(x,7,6) = 19.001800\np(x,7,6,5) = 39.007800\np(x,7,6,5,4) = 71.022203\n"
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "",
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}