summaryrefslogtreecommitdiff
path: root/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb')
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb694
1 files changed, 694 insertions, 0 deletions
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb
new file mode 100644
index 00000000..6ef37c88
--- /dev/null
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch5.ipynb
@@ -0,0 +1,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": {}
+ }
+ ]
+} \ No newline at end of file