diff options
author | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
commit | 206d0358703aa05d5d7315900fe1d054c2817ddc (patch) | |
tree | f2403e29f3aded0caf7a2434ea50dd507f6545e2 /Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version) | |
parent | c6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff) | |
download | Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2 Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip |
adding book
Diffstat (limited to 'Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)')
23 files changed, 8167 insertions, 0 deletions
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/README.txt b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/README.txt new file mode 100644 index 00000000..3d9a2b47 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/README.txt @@ -0,0 +1,10 @@ +Contributed By: Ashutosh Kumar +Course: btech +College/Institute/Organization: Indian Institute of Technology - Bombay, CHAR Lab 2 +Department/Designation: Electrical Department +Book Title: Engineering Thermodynamics: A Computer Approach (SI Units Version) +Author: R. K. Rajput +Publisher: Laxmi Pulications (P) Ltd., New Delhi +Year of publication: 2007 +Isbn: 9780763782726 +Edition: 3rd
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch1.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch1.ipynb new file mode 100644 index 00000000..f6589ebe --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch1.ipynb @@ -0,0 +1,250 @@ +{ + "metadata": { + "name": "ch1" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "\"\"\"\nExample 1.1\n\"\"\"\nprint \"Hello, World!\\n\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Hello, World!\n\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nexample 1.2\nprints 'hello world' with comments.\n'''\n\n# prints \"Hello, World!\":\nprint \"Hello, World!\\n\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Hello, World!\n\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nexample 1.3\nanother version of hello world.\n'''\n\n# prints \"Hello, World!\":\nprint \"Hel\" + \"lo, Wo\" + \"rld!\" \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Hello, World!\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "\"\"\"\nexample 1.4\nanother version of hello world.\n\"\"\"\n\n# prints \"Hello, World!\":\nprint \"Hello, W\" + 'o' + \"rld\" + '!' ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Hello, World!\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.5 Inserting Numeric Literals into the Standard Output Stream\n'''\n\n# prints \"The Millennium ends Dec 31 2000.\":\nprint \"The Millennium ends Dec %d %d \" %(31,2000)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The Millennium ends Dec 31 2000 \n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.6 Using Integer Variables\nIn this example, the integer 44 is assigned to the variable m, and the value of the expression m + 33\nis assigned to the variable n:\n'''\n# prints \"m = 44 and n = 77\":\n\nm = 44 # assigns the value 44 to the variable m\nprint \"m = %d \" % m,\nn = m + 33 # assigns the value 77 to the variable n\nprint \"and n = %d \" % n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "m = 44 and n = 77 \n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.7 A Program's Tokens\n'''\n\n# prints \"n = 44:\nn=44\nprint \"n = %d\" % n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 44\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.8 \n'''\n\n# Python does not have semicolons so wont give any errors.\nn=44\nprint \"n = %d\" % n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 44\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.9 Initializing Variables\nThis program contains one variable that is not initialized and one that is initialized.\nNote : This program would give you differ output then c gives.\n'''\n\n# prints \"m = ?? and n = 44\":\nm = 0 #In python we do not have declaration of variables, we just initialize it and use it.\nn=44\nprint \"m = %d and n = %d\" %(m,n)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "m = 0 and n = 44\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.10 The const Specifier\nThis program illustrates constant definitions:\n'''\n\n# defines constants; has no output:\nBEEP = '\\b'\nMAXINT = 2147483647\nN = MAXINT/2\nKM_PER_MI = 1.60934\nPI = 3.14159265358979323846\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.11 Using the Input Operator\n'''\n\n# tests the input of integers, floats, and characters:\nprint \"Enter two integers: \"\nm = int(raw_input())\nn = int(raw_input())\nprint \"m = %d , n = %d \" %(m,n)\n\nprint \"Enter three decimal numbers: \"\nx = float(raw_input())\ny = float(raw_input())\nz = float(raw_input())\n\nprint \"x = %f , y = %f , z = %f\" %(x,y,z)\n\nprint \"Enter four characters: \";\nc1 = raw_input()\nc2 = raw_input()\nc3 = raw_input()\nc4 = raw_input()\nprint \"c1 = \" + c1 + \", c2 = \" + c2 + \", c3 = \" + c3 + \", c4 = \" + c4 ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "22\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "m = 22 , n = 44 \nEnter three decimal numbers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2.2\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4.4\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "6.6\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 2.200000 , y = 4.400000 , z = 6.600000\nEnter four characters: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "A\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "B\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "C\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "D\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "c1 = A, c2 = B, c3 = C, c4 = D\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb new file mode 100644 index 00000000..4b6ac8a8 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb @@ -0,0 +1,286 @@ +{ + "metadata": { + "name": "ch10" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.1 Implementing the Ratio Class\n'''\nclass Ratio:\n def __init__(self):\n self.num = 0\n self.den = 0\n def assign(self,n,d):\n self.num = n\n self.den = d\n def convert(self):\n return float(self.num)/self.den\n def invert(self):\n self.num,self.den = self.den,self.num\n def print_(self):\n print self.num , '/' , self.den ,\n \nx = Ratio()\nx.assign(22,7)\nprint \"x = \",\nx.print_()\nprint \" = \" , x.convert() \nx.invert()\nprint \"1/x = \",\nx.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 22 / 7 = 3.14285714286\n1/x = 7 / 22\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.2 Class example\n'''\nclass Ratio:\n def __init__(self):\n self.num = 0\n self.den = 0\n def assign(self,n,d):\n self.num = n\n self.den = d\n def convert(self):\n return float(self.num)/self.den\n def invert(self):\n self.num,self.den = self.den,self.num\n def print_(self):\n print self.num , '/' , self.den ,\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.3\nA Constructor Function for the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n,d):\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\nx = Ratio(-1,3)\ny = Ratio(22,7)\nprint \"x = \",\nx.print_()\nprint \" and y = \",\ny.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = -1 / 3 and y = 22 / 7\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.4 Constructors to the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if n==None:\n self.num = 0\n self.den = 1\n elif d==None:\n self.num = n\n self.den = 1\n else:\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\nx = Ratio()\ny = Ratio(4)\nz = Ratio(22,7)\nprint \"x = \",\nx.print_()\nprint \"\\ny = \",\ny.print_()\nprint \"\\nz = \",\nz.print_()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 0 / 1 \ny = 4 / 1 \nz = 22 / 7\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.5\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if n==None:\n self.num = 0\n self.den = 1\n elif d==None:\n self.num = n\n self.den = 1\n else:\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.6 Using Default Parameter Values in the Ratio Class Constructor\n'''\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\n\nx = Ratio()\ny = Ratio(4)\nz = Ratio(22,7)", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.7 Access Functions in the Ratio Class\n'''\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n\nx = Ratio(22,7)\nprint x.numerator() , '/' , x.denominator() ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "22 / 7\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.8\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(100,-360)\nx.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "-5 / 18\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.9 Adding a Copy Constructor to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=None):\n if d == None:\n self.num = n.num\n self.den = n.den\n else: \n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(100,360)\ny = Ratio(x)\nprint \"x = \",\nx.print_()\nprint \"y = \",\ny.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 5 / 18 y = 5 / 18\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.10 Tracing Calls to the Copy Constructor\n'''\n'''\nEXAMPLE 10.9 Adding a Copy Constructor to the Ratio Class\n'''\n'''\nEXAMPLE 10.8\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=None):\n if d == None:\n print \"COPY CONSTRUCTOR CALLED\"\n self.num = n.num\n self.den = n.den\n else: \n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\ndef f(r):\n s = Ratio(r)\n\nx = Ratio(22,7)\ny = Ratio(x) #calls the copy constructor, copying x to y\nf(y)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "COPY CONSTRUCTOR CALLED\nCOPY CONSTRUCTOR CALLED\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.11 Including a Destructor in the Ratio Class\nNote : Python objects die when program gets exit.\n'''\n\nclass Ratio:\n def __init__(self):\n print \"OBJECT IS BORN.\"\n def __del__(self):\n print \"OBJECT DIES.\"\n\nx = Ratio()\nprint \"Now x is alive.\"\nprint \"Now between blocks.\"\ny = Ratio()\nprint \"Now y is alive.\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "OBJECT IS BORN.\nNow x is alive.\nNow between blocks.\nOBJECT IS BORN.\nNow y is alive.\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.12\n'''\nclass X:\n def __init(self):\n data = 0\n\np = X()\np.data = 22\nprint \"p.data = \" , p.data , \" = \" , p.data\np.data = 44\nprint \" p.data = \" , p.data , \" = \" , p.data ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "p.data = 22 = 22\n p.data = 44 = 44\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.13 A Node Class for Linked Lists\n'''\nclass Node:\n def __init__(self,d,q=None):\n self.data = d\n self.next = q\n\nn = int(raw_input())\nq = Node(n)\nwhile True:\n n = int(raw_input())\n if n<=0:\n break\n p = Node(n, q)\n q = p\nk = p\nwhile k != None:\n print k.data , '->' , \n k = k.next\nprint '*'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "22\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "66\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "77 -> 66 -> 55 -> 44 -> 33 -> 22 -> *\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.14 \nPython does not support static data type.\nPython automatically handles local variable so we need not to delete it.\n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , count , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , count , 'widgets' \nprint \"Now there are \" , count , 'widgets'\ny = Widget()\nprint \"Now there are \" , count , 'widgets'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "OBJECT DIES.\nNow there are 2 widgets\nOBJECT DIES.\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.15 \n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n def numWidgets(self):\n global count\n return count\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , w.numWidgets() , 'widgets' \nprint \"Now there are \" , w.numWidgets() , 'widgets'\ny = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Now there are 2 widgets\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.16\n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n def numWidgets(self):\n global count\n return count\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , w.numWidgets() , 'widgets' \nprint \"Now there are \" , w.numWidgets() , 'widgets'\ny = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Now there are 2 widgets\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb new file mode 100644 index 00000000..3ddc5480 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb @@ -0,0 +1,211 @@ +{ + "metadata": { + "name": "ch11" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.1\nAdding an Assignment Operator to the Ratio Class\nHere is a class interface for the Ratio class, showing the default constructor, the copy constructor,\nand the assignment operator:\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if d==None:\n self.num = n.num\n self.den = n.den\n elif n==None:\n self.num = 0\n self.den = 1\n else:\n self.num = n\n self.den = d\n \n def equals(self):\n return self # retuns calling object.", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.2 The Preferred Prototype for an Overloaded Assignment Operator\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n pass\n \n def equals(self):\n pass\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.3 Implementation of the Assignment Operator for the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if d==None:\n self.num = n.num\n self.den = n.den\n elif n==None:\n self.num = 0\n self.den = 1\n else:\n self.num = n\n self.den = d\n \n\nz = Ratio(22,7)\ny = z\nx = z\n\nx = Ratio(22,7)\ny = Ratio(x)\nz = x\nw = x", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.4\nmultiplication operator overload\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n def __mul__(self,y):\n pass\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.5 The Ratio Class with Assignment and Multiplication Operators\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\nx = Ratio(22,7)\ny = Ratio(-3,8)\nz = x\nz.print_()\nx = y*z\nx.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "22 / 7\n-33 / 28\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.6 The Ratio Class with an Overloaded *= Operator\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def print_(self):\n print self.num , '/', self.den", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.7 Overloading the Equality Operator == in the Ratio Class\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def __eq__(self,y):\n return (x.num * y.den == y.num * x.den)\n def print_(self):\n print self.num , '/', self.den\n\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.8 \nPython does not use << operator for printing. So here we are just declaring function name as print_.\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def __eq__(self,y):\n return (x.num * y.den == y.num * x.den)\n def print_(self):\n print self.num , '/', self.den\n\n\nx = Ratio(22,7)\ny = Ratio(-3,8)\nx.print_() , y.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "22 / 7\n-3 / 8\n" + }, + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 8, + "text": "(None, None)" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.9 \nPython does not have >> for input. so we will use input function.\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def input(self):\n self.num = int(raw_input('Numerator : '))\n self.den = int(raw_input('Denominator : '))\n self.reduce()\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\n\nx = Ratio()\ny = Ratio()\nx.input()\ny.input()\nx.print_()\ny.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Numerator : -10\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Denominator : -24\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Numerator : 36\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Denominator : -20\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "5 / 12\n-9 / 5\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.10\nAdding a Conversion Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def input(self):\n self.num = int(raw_input('Numerator : '))\n self.den = int(raw_input('Denominator : '))\n self.reduce()\n def __float__(self):\n return float(self.num)/self.den\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 15.py\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(-5,8)\nprint \"x = \" , \nx.print_() \nprint \", float(x) = \" , float(x) \nP = Ratio(22,7)\nPI = float(P)\nprint \"P = \" ,\nP.print_() \nprint \", PI = \" , PI\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = -5 / 8\n, float(x) = -0.625\nP = 22 / 7\n, PI = 3.14285714286\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.11 Adding a Pre-Increment Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __iadd__(self,n):\n self.num += self.den\n return self\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\nx += 1\ny = x\nprint \"y = \" ,\ny.print_()\nprint \", x = \",\nx.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "y = 29 / 7\n, x = 29 / 7\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.12 Adding a Post-Increment Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __iadd__(self,n):\n self.num += self.den\n return self\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\ny = Ratio(x.num,x.den)\nx += 1\nprint \"y = \" ,\ny.print_()\nprint \", x = \",\nx.print_()\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "y = 22 / 7\n, x = 29 / 7\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.13\nAdding a Subscript Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __getitem__(self,k):\n if k == 1:\n return self.num\n else:\n return self.den\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\nprint \"x = \" ,\nx.print_()\nprint \"x[1] = \" , x[1] , \", x[2] = \" , x[2]\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 22 / 7\nx[1] = 22 , x[2] = 7\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb new file mode 100644 index 00000000..304a3ccb --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch12.ipynb @@ -0,0 +1,228 @@ +{ + "metadata": { + "name": "ch12" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.1 A Person Class\nHere is a simple definition for a class to represent people.\n'''\nclass Person:\n def __init__(self,n=\"\",nat=\"U.S.A.\",s=1):\n self.name = n\n self.nationality = nat\n self.sex = s\n\n def printName(self):\n print self.name,\n \n def printNationality(self):\n print self.nationality,\n\ncreator = Person(\"Bjarne Stroustrup\", \"Denmark\")\nprint \"The creator of C++ was \" ,\ncreator.printName() \nprint \", who was born in \" ,\ncreator.printNationality() \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The creator of C++ was Bjarne Stroustrup , who was born in Denmark\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.2 A Date Class\n'''\n\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\npeace = Date(11,11,1918)\nprint \"World War I ended on \" ,\npeace.print_()\npeace.setDate(8,14,1945)\nprint \"World War II ended on \" ,\npeace.print_()\nprint \"Enter month, day, and year: \"\ndate = Date()\ndate.input()\nprint \"The date is \" , \ndate.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "World War I ended on November 11 , 1918\nWorld War II ended on August 14 , 1945\nEnter month, day, and year: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "7\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1976\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The date is July 4 , 1976\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.3 Composing the Date Class with the Person Class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nauthor = Person(\"Thomas Jefferson\", 1)\nauthor.setDOB(4,13,1743)\nauthor.setDOD(7,4,1826)\nprint \"The author of the Declaration of Independence was \",\nauthor.printName()\nprint \".\\nHe was born on \",\nauthor.printDOB()\nprint \" and died on \",\nauthor.printDOD()\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The author of the Declaration of Independence was Thomas Jefferson .\nHe was born on April 13 , 1743\n and died on July 4 , 1826\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.4 Deriving a Student Class from the Person Class\nStudents are people. So it is natural to use the Person class to derive a Student class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nclass Student(Person):\n def __init__(self,n,s=0,i=\"\"):\n Person.__init__(self,n,s)\n self.id = i\n self.credits = 0\n self.gpa = 0\n self.dom = Date()\n\n def setDOM(self,m,d,y):\n self.dom.setDate(m, d, y)\n def printDOM(self):\n self.dom.print_()\n\nx = Student(\"Ann Jones\", 0, \"219360061\")\nx.setDOB(5, 13, 1977)\nx.setDOM(8, 29, 1995)\nx.printName()\nprint \"\\n\\t Born: \" ,\nx.printDOB()\nprint \"\\n\\tMatriculated: \",\nx.printDOM()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Ann Jones \n\t Born: May 13 , 1977\n\n\tMatriculated: August 29 , 1995\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.5 The Person Class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nclass Student(Person):\n def __init__(self,n,s=0,i=\"\"):\n Person.__init__(self,n,s)\n self.id = i\n self.credits = 0\n self.gpa = 0\n self.dom = Date()\n\n def setDOM(self,m,d,y):\n self.dom.setDate(m, d, y)\n def printDOM(self):\n self.dom.print_()\n def printSex(self):\n if self.sex == 1:\n print \"male\"\n else:\n print 'female'\n\nx = Student(\"Ann Jones\", 0, \"219360061\")\nx.setDOB(5, 13, 1977)\nx.setDOM(8, 29, 1995)\nx.setDOD(7,4,1826)\nx.printName()\nprint \"\\n\\t Born: \" , \nx.printDOB()\nprint \"\\n\\t Sex: \" ,\nx.printSex()\nprint \"\\n\\tMatriculated: \",\nx.printDOM()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Ann Jones \n\t Born: May 13 , 1977\n\n\t Sex: female\n\n\tMatriculated: August 29 , 1995\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.6 Dominating a Data Member and Overriding a Member Function\nHere are two classes, X and Y, with Y inheriting from X.\n'''\nclass X:\n def __init__(self):\n self.a = 0\n def f(self):\n print \"X::f() executing\"\nclass Y(X):\n def __init__(self):\n self.a = 0\n def f(self):\n print \"Y::f() executing\"\nx = X()\nx.a = 22\nx.f()\nprint \"x.a = \" , x.a\ny = Y()\ny.a = 44\n# assigns 44 to the a defined in Y\ny._X__a = 66\n# assigns 66 to the a defined in X\ny.f()\n# invokes the f() defined in Y\nX.f(x)\n# invokes the f() defined in X\nprint \"y.a = \" , y.a \nprint \"y._X__a = \" , y._X__a \nz = y\nprint \"z.a = \" , z._X__a \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X::f() executing\nx.a = 22\nY::f() executing\nX::f() executing\ny.a = 44\ny._X__a = 66\nz.a = 66\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.7 Parent Constructors and Destructors\nNote : Python destuctor is called when program goes exit. So output may be differ than c.\n'''\nclass X:\n def __init__(self):\n print \"X::X() constructor executing \"\n def __del__(self):\n print \"X::X() destructor executing \"\n\nclass Y(X):\n def __init__(self):\n X.__init__(self)\n print \"Y::Y() constructor executing \"\n def __del__(self):\n print \"Y::Y() destructor executing \"\n\nclass Z(Y):\n def __init__(self,i):\n Y.__init__(self)\n print \"Z::Z(\" , i , \") constructor executing \"\n def __del__(self):\n print \"Z::Z() destructor executing \"\n \n\nZ = Z(44)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X::X() constructor executing \nY::Y() constructor executing \nZ::Z( 44 ) constructor executing \n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.8 Parent Constructors and Destructors\nHere is a demo program that uses a base class Person and a derived class Student:\nNote : Python destuctor is called when program goes exit. So output may be differ than c.\n'''\nclass Person:\n def __init__(self,s):\n self.name = s\n def __del__(self):\n pass\n\nclass Student(Person):\n def __init__(self,s,m):\n Person.__init__(self,s)\n self.major = m\n def __del__(self):\n pass\nx = Person(\"Bob\")\ny = Student(\"Sarah\", \"Biology\")\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.9\n'''\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n def setHSgraduate(self,g):\n self.hs = g\n def isHSgraduate(self):\n return hs\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.10 Using virtual Functions\nThis demo program declares p to be a pointer to objects of the base class X. First it assigns p to\npoint to an instance x of class X. Then it assigns p to point to an instance y of the derived class Y.\nNote : By default all methods in python are virtual. so output would be differ than c.\n'''\nclass X:\n def f(self):\n print \"X::f() executing\"\nclass Y(X):\n def f(self):\n print \"Y::f() executing\"\nx = X()\ny = Y()\np = x\np.f()\np = y\np.f()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X::f() executing\nY::f() executing\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.11 Polymorphism through virtual Functions\nHere is a Person class with a Student subclass and a Professor subclass:\n'''\nclass Person:\n def __init__(self,n):\n self.name = n\n def print_(self):\n print 'My name is' , self.name\n\nclass Student(Person):\n def __init__(self,s,g):\n Person.__init__(self,s)\n self.gpa = g\n def print_(self):\n print 'My name is ',self.name, ' and my G.P.A. is', self.gpa\n\nclass Professor(Person):\n def __init__(self,s,n):\n Person.__init__(self,s)\n self.publs = n\n def print_(self):\n print 'My name is ', self.name,' and i have ' , self.publs,' publications.'\n\nx = Person(\"Bob\")\np = x\np.print_()\ny = Student(\"Tom\", 3.47)\np = y\np.print_()\nz = Professor(\"Ann\", 7)\np = z\np.print_()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "My name is Bob\nMy name is Tom and my G.P.A. is 3.47\nMy name is Ann and i have 7 publications.\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.12 Memory Leaks\nThis program is similar to Example 12.6:\n'''\nclass X:\n def __init__(self):\n self.p = [0,0]\n print 'X().',\n def __del__(self):\n print '~X().'\n\nclass Y(X):\n def __init__(self):\n X.__init__(self)\n self.q = []\n for i in range(1023):\n self.q.append(0)\n print 'Y() : Y::q = ', hex(id(self.q)) ,'.',\n def __del__(self):\n print '~Y().'\n\nfor i in range(8):\n r = Y()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.13 A Hierarchy of Media Classes\nHere is a hierarchy of classes to represent various media objects:\nMedia Audio Book Periodical CD Tape Record Magazine Newspaper Journal Newsletter\nThe primary ABC is the Media class:\n'''\nclass Media:\n def __init__(self):\n self.title = ''\n def print_(self):\n pass\n def id(self):\n pass\n\nclass Book(Media):\n def __init__(self,a='',t=\"\",p=\"\",i=\"\"):\n self.author = a\n self.publisher = p\n self.isbn = i\n self.title = t\n def print_(self):\n print self.title , \" by \" , self.author\n def id(self):\n return self.isbn\n\nclass CD(Media):\n def __init__(self,t=\"\",c=\"\",m=\"\",n=\"\"):\n self.composer = c\n self.make = m\n self.number = n\n self.title = t\n def print_(self):\n print self.title , \", \" , self.composer\n def id(self):\n s = str(self.make) + ' ' + str(self.number)\n return s\n\nclass Magazine(Media):\n def __init__(self,t=\"\",i=\"\",v=0, n=0):\n self.issn = i\n self.volume = v\n self.number = n\n self.title = t\n def print_(self):\n print self.title , \" Magazine, Vol. \", self.volume , \", No.\" , self.number\n def id(self):\n return self.issn\n\nbook = Book(\"Bjarne Stroustrup\", \"The C++ Programming Language\",\"Addison-Wesley\", \"0-201-53992-6\")\nmagazine = Magazine(\"TIME\", \"0040-781X\", 145, 23)\ncd = CD(\"BACH CANTATAS\", \"Johann Sebastian Bach\",\"ARCHIV\", \"D120541\")\nbook.print_()\nprint \"\\tid: \" , book.id() \nmagazine.print_()\nprint \"\\tid: \" , magazine.id() \ncd.print_()\nprint \"\\tid: \" , cd.id()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The C++ Programming Language by Bjarne Stroustrup\n\tid: 0-201-53992-6\nTIME Magazine, Vol. 145 , No. 23\n\tid: 0040-781X\nBACH CANTATAS , Johann Sebastian Bach\n\tid: ARCHIV D120541\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb new file mode 100644 index 00000000..827142fe --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch13.ipynb @@ -0,0 +1,136 @@ +{ + "metadata": { + "name": "ch13" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.1 The swap Function Template\n'''\ndef swap(x,y):\n x[0],y[0] = y[0],x[0]\n\nm = [22]\nn = [66]\nswap(m, n)\ns1 = [\"John Adams\"]\ns2 = [\"James Madison\"]\nswap(s1, s2)\nx = [22/7]\ny = [-3]\nswap(x, y)\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.2 The Bubble Sort Template\n'''\n\ndef sort(v,n):\n for i in range(1,n):\n for j in range(n-i):\n if v[j] > v[j+1]:\n v[j],v[j+1] = v[j+1],v[j]\n\ndef print_( v,n):\n for i in range(n):\n print v[i],\n print \"\"\n \na = [55, 33, 88, 11, 44, 99, 77, 22, 66]\nprint_(a,9);\nsort(a,9)\nprint_(a,9)\ns = [\"Tom\", \"Hal\", \"Dan\", \"Bob\", \"Sue\", \"Ann\", \"Gus\"]\nprint_(s,7)\nsort(s,7)\nprint_(s,7)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "55 33 88 11 44 99 77 22 66 \n11 22 33 44 55 66 77 88 99 \nTom Hal Dan Bob Sue Ann Gus \nAnn Bob Dan Gus Hal Sue Tom \n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.3 A Stack Class Template\n'''\nclass Stack:\n def __init__(self,s=100):\n self.size = s\n self.top = -1\n self.data = []\n def push(self,x):\n self.data.append( x)\n self.top += 1\n def pop(self):\n d = self.data[self.top]\n self.data.pop(self.top)\n self.top -= 1\n return d \n def isEmpty(self):\n return self.top == -1\n def isFull(self):\n return self.top==self.size-1\n \nintStack1 = Stack(5)\nintStack2 = Stack(10)\ncharStack = Stack(8)\nintStack1.push(77)\ncharStack.push('A')\nintStack2.push(22)\ncharStack.push('E')\ncharStack.push('K')\nintStack2.push(44)\nprint intStack2.pop() \nprint intStack2.pop() \nif (intStack2.isEmpty()):\n print \"intStack2 is empty.\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "44\n22\nintStack2 is empty.\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.4 A Vector Class Template\n= operator does work in python by defaultly.\n'''\nclass Vector:\n def __init__(self,n=None):\n if type(n) == int :\n self.size = n\n self.data = []\n else:\n self.size = 8\n self.data = []\n for i in range(self.size):\n self.data.append(0)\nv = Vector()\nv.data[5] = 127\nw = v\nx = Vector(3)\nprint w.size\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.5 A Subclass Template for Vectors\n'''\nclass Vector:\n def __init__(self,n=None):\n if type(n) == int :\n self.size = n\n self.data = []\n else:\n self.size = 8\n self.data = []\n for i in range(self.size):\n self.data.append(0)\n\nclass Array(Vector):\n def __init__(self,i,j):\n Vector.__init__(self,j-i+1)\n self.i0= i\n def __setitem__(self,k,v):\n self.data[k-self.i0] = v\n def __getitem__(self,i):\n return self.data[self.i0-i]\n def firstSubscript(self):\n return self.i0\n def lastSubscript(self):\n return self.i0+self.size-1\n\nx = Array(1,3)\nx.data[0] = 3.14159\nx.data[1] = 0.08516\nx.data[2] = 5041.92\nprint \"x.size() = \" , x.size \nprint \"x.firstSubscript() = \" , x.firstSubscript() \nprint \"x.lastSubscript() = \" , x.lastSubscript()\nfor i in range(0,3):\n print \"x[\" , i + 1 , \"] = \" , x.data[i]\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x.size() = 3\nx.firstSubscript() = 1\nx.lastSubscript() = 3\nx[ 1 ] = 3.14159\nx[ 2 ] = 0.08516\nx[ 3 ] = 5041.92\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.6 A Matrix Class Template\n'''\n\nclass Matrix:\n def __init__(self,r=1,c=1):\n self.rows = r\n self.columns = c\n self.vector = []\n for i in range(r):\n a = []\n for j in range(c):\n a.append(0)\n self.vector.append(a)\n\na = Matrix(2,3)\na.vector[0][0] = 0.0\na.vector[0][1] = 0.1\na.vector[0][2] = 0.2\na.vector[1][0] = 1.0\na.vector[1][1] = 1.1\na.vector[1][2] = 1.2\n\nprint \"The matrix a has \" , a.rows , \" rows and \", a.columns , \" columns:\"\nfor i in range(2):\n for j in range(3):\n print a.vector[i][j] ,\n print \"\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The matrix a has 2 rows and 3 columns:\n0.0 0.1 0.2 \n1.0 1.1 1.2 \n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.7 A List Class Template\nA list consists of a linked sequence of nodes. Each node contains one data item and a link to the next\nnode. So we begin by defining a ListNode class template:\n'''\n\nfriends = []\n\n\nfriends.insert(0,\"Bowen, Van\")\nfriends.insert(0,\"Dixon, Tom\")\nfriends.insert(0,\"Mason, Joe\")\nfriends.insert(0,\"White, Ann\")\n\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\nfriends.remove('White, Ann')\nprint \"Removed: \" , 'White, Ann'\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "White, Ann -> Mason, Joe -> Dixon, Tom -> Bowen, Van -> *\nRemoved: White, Ann\nMason, Joe -> Dixon, Tom -> Bowen, Van -> *\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.8\n'''\nfriends = []\nfriends.append(\"Bowen, Van\")\nfriends.append(\"Dixon, Tom\")\nfriends.append(\"Mason, Joe\")\nfriends.append(\"White, Ann\")\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfriends.remove(\"Mason, Joe\")\nfriends[1] = \"Davis, Jim\"\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfriends.insert(2,\"Morse, Sam\")\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfor i in range(len(friends)):\n print \"[\" ,friends[i] , \"]\" , '->' ,\nprint '*'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Bowen, Van -> Dixon, Tom -> Mason, Joe -> White, Ann -> *\nBowen, Van -> Davis, Jim -> White, Ann -> *\nBowen, Van -> Davis, Jim -> Morse, Sam -> White, Ann -> *\n[ Bowen, Van ] -> [ Davis, Jim ] -> [ Morse, Sam ] -> [ White, Ann ] -> *\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch14.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch14.ipynb new file mode 100644 index 00000000..1618010d --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch14.ipynb @@ -0,0 +1,172 @@ +{ + "metadata": { + "name": "ch14" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.1 Using a vector of strings\n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.2 \n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.3 \n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in range(len(v)):\n print v[i]\n\nv = []\nload(v)\nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.4 Using the Generic sort() Algorithm\n'''\n\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nv.sort()\nprint_(v)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.5 Using the Assignment Operator to Duplicate a vector\n'''\n#This program demonstrates that one vector can be assigned to another.\n\n\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nw = v\nprint_(v)\nprint_(w)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\nJapan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.6 \n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nv.sort()\nprint_(v)\nprint \"v.front() = \" + v[0]\nprint \"v.back() = \" + v.pop(-1) \nprint \"v.back() = \" + v.pop(-1) \nprint \"v.back() = \" + v[-1] \nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\nv.front() = Chile\nv.back() = Zaire\nv.back() = Spain\nv.back() = Nepal\nChile\nEgypt\nItaly\nJapan\nKenya\nNepal\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.7 Using the erase() Function\n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nv.sort()\nprint_(v)\nv.pop(2) # removes Italy\nv.pop(-2) # removes Spain\nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\nChile\nEgypt\nJapan\nKenya\nNepal\nZaire\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.8 Using the insert() Function\n'''\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\nv = []\nload(v)\nv.sort()\nprint_(v)\nr = []\nfor i in range(2,len(v)-2):\n r.append(v[i]) #removes the segment Italy..Nepal\n \nfor i in r:\n v.remove(i)\nprint_(v)\nv.insert(2,\"India\")\nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\nChile\nEgypt\nSpain\nZaire\nChile\nEgypt\nIndia\nSpain\nZaire\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.9 Using the find() Function\n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n v.append(\"India\")\n v.append(\"China\")\n v.append(\"Malta\")\n v.append(\"Syria\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nprint_(v)\negypt = v.index('Egypt')\nmalta = v.index('Malta')\nw = v[egypt:malta+1]\nw.sort()\nprint_(w)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\nIndia\nChina\nMalta\nSyria\nChile\nChina\nEgypt\nIndia\nKenya\nMalta\nNepal\nZaire\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.10\n'''\ndef copy(v,x,n):\n for i in x:\n v.append(i)\n\ndef projection(v,b):\n v_size = len(v)\n w = []\n for i in range(0,v_size):\n if b[i]:\n w.append(v[i])\n return w\n\ndef print_(v):\n for i in v:\n print i,\n print ''\n\nx = [ 22.2, 33.3, 44.4, 55.5, 66.6, 77.7, 88.8, 99.9 ]\nv = []\ncopy(v, x, 8)\ny = [ False, True, False, True, True, True, False, True ]\nb = []\ncopy(b, y, 8)\nw = projection(v, b)\nprint_(v)\nprint_(w)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "22.2 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n33.3 55.5 66.6 77.7 99.9 \n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch15.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch15.ipynb new file mode 100644 index 00000000..54f50fe3 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch15.ipynb @@ -0,0 +1,1677 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 15 : Heat Transfer" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.1 page no : 792" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Calculate the rate of heat transfer per m 2 of surface area of the wall, which is 220 mm thick.\n", + "\n", + "'''\n", + "\n", + "# Variables\n", + "t1 = 60.; \t\t\t#0C\n", + "t2 = 35.; \t\t\t#0C\n", + "L = 0.22; \t\t\t#m\n", + "k = 0.51; \t\t\t#W/m 0C\n", + "\n", + "# Calculations\n", + "q = k*(t1-t2)/L;\n", + "\n", + "# Results\n", + "print (\"Rate of heat transfer per m**2 = %.3f\")% (q), (\"W/m**2\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rate of heat transfer per m**2 = 57.955 W/m**2\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.2 page no : 792" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "(i) Determine the thickness of fire brick and insulation \n", + "(ii) Calculate the heat loss \n", + "'''\n", + "\n", + "# Variables\n", + "t1 = 1325.; \t\t\t#0C\n", + "t2 = 1200.; \t\t\t#0C\n", + "t3 = 25.; \t\t\t #0C\n", + "L = 0.32; \t\t\t #m\n", + "k_A = 0.84; \t\t\t#W/m 0C\n", + "k_B = 0.16; \t\t\t#W/m 0C\n", + "\n", + "# Calculations and Results\n", + "L_A = (t1-t2)*k_A/k_B*L/((t1-t3)-(t1-t2)*k_A/k_A+(t1-t2)*k_A/k_B); \t\t\t#m\n", + "print (\"(i)L_A = %.3f\")% (L_A*1000), (\"mm\")\n", + "\n", + "L_B = 0.32-L_A; \t\t\t#m\n", + "print (\"L_B %.3f\")% (L_B*1000), (\"mm\")\n", + "\n", + "q = (t1-t2)/L_A*k_A;\n", + "print (\"(ii) Heat loss per unit area = %.3f\")% (q), (\"W/m**2\")\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i)L_A = 114.676 mm\n", + "L_B 205.324 mm\n", + "(ii) Heat loss per unit area = 915.625 W/m**2\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.3 page no : 793" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "What thickness of loosely packed rock wool insulation should be added to\n", + "reduce the heat loss or (gain) \n", + "'''\n", + "\n", + "# Variables\n", + "L_A = 0.1; \t\t\t#m\n", + "L_B = 0.04; \t\t#m\n", + "k_A = 0.7; \t\t\t#W/m 0C\n", + "k_B = 0.48; \t\t#W/m 0C\n", + "k_C = 0.065; \t\t#W/m 0C\n", + "\n", + "# Calculations\n", + "#Q2 = 0.2*Q1\n", + "L_C = 0.8*((L_A/k_A) + (L_B/k_B))*k_C/0.2;\n", + "\n", + "# Results\n", + "print (\"thickness of rock wool insulation = %.3f\")% (L_C*1000), (\"mm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "thickness of rock wool insulation = 58.810 mm\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.4 page no : 794" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''Find-\n", + "(i) To how many millimetres of insulation brick is the air layer equivalent ?\n", + "(ii) What is the temperature of the outer surface of the steel plate ?\n", + "'''\n", + "\n", + "# Variables\n", + "L_A = 0.2; \t\t\t#m\n", + "L_C = 0.006; \t\t#m\n", + "L_D = 0.1; \t\t\t#m\n", + "t1 = 1150.; \t\t#0C\n", + "t2 = 40.; \t\t\t#0C\n", + "dt = t1-t2;\n", + "k_A = 1.52; \t\t#W/m 0C\n", + "k_B = 0.138; \t\t#W/m 0C\n", + "k_D = 0.138; \t\t#W/m 0C\n", + "k_C = 45.; \t\t\t#W/m 0C\n", + "q = 400.; \t\t\t#W/m**2\n", + "\n", + "# Calculations and Results\n", + "print (\"(i) The value of x = (L_C): \")\n", + "L_B = ((t1-t2)/q - (L_A/k_A+L_C/k_C+L_D/k_D))*k_B*1000;\n", + "print (\"L_B = %.3f\")% (L_B), (\"mm\")\n", + "\n", + "\n", + "t_so = q*L_D/k_D + t2;\n", + "print (\"(ii) Temperature of the outer surface of the steel plate t_so = %.3f\")% (t_so), (\"0C\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) The value of x = (L_C): \n", + "L_B = 264.774 mm\n", + "(ii) Temperature of the outer surface of the steel plate t_so = 329.855 0C\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.5 page no : 795" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Find the heat flow rate\n", + "'''\n", + "\n", + "# Variables\n", + "k_A = 150.; \t\t\t#W/m 0C\n", + "k_B = 30.; \t\t\t#W/m 0C\n", + "k_C = 65.; \t\t\t#W/m 0C\n", + "k_D = 50.; \t\t\t#W/m 0C\n", + "L_A = 0.03; \t\t\t#m\n", + "L_B = 0.08; \t\t\t#m\n", + "L_C = L_B;\n", + "L_D = 0.05; \t\t\t#m\n", + "A_A = 0.01; \t\t\t#m**2\n", + "A_B = 0.003; \t\t\t#m**2\n", + "A_C = 0.007; \t\t\t#m**2\n", + "A_D = 0.01; \t\t\t#m**2\n", + "t1 = 400.; \t\t\t#0C\n", + "t4 = 60.; \t\t\t#0C\n", + "\n", + "# Calculations\n", + "R_thA = L_A/k_A/A_A;\n", + "R_thB = L_B/k_B/A_B;\n", + "R_thC = L_C/k_C/A_C;\n", + "R_thD = L_D/k_D/A_D;\n", + "\n", + "R_th_eq = R_thB*R_thC/(R_thB+R_thC);\n", + "R_th_total = R_thA+R_th_eq+R_thD;\n", + "Q = (t1-t4)/R_th_total;\n", + "\n", + "# Results\n", + "print (\"heat flow rate = %.3f\")% (Q), (\"W\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "heat flow rate = 1274.415 W\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.6 page no : 796" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "calculate-\n", + "(i) The rate of heat loss per m 2 of the tank surface area ;\n", + "(ii) The temperature of the outside surface of the tank.\n", + "'''\n", + "\n", + "# Variables\n", + "L = 0.012; \t\t\t#m\n", + "t_hf = 95.; \t\t\t#0C\n", + "t_cf = 15.; \t\t\t#0C\n", + "k = 50.; \t\t\t#W/m 0C\n", + "h_hf = 2850.; \t\t\t#W/m**2 0C\n", + "h_cf = 10.; \t\t\t#W/m**2 0C\n", + "\n", + "# Calculations and Results\n", + "print (\"(i) Rate of heat loss per m**2 of the tank surface area\")\n", + "U = 1./(1./h_hf + L/k + 1./h_cf);\n", + "A = 1.; \t\t\t #m**2\n", + "q = U*A*(t_hf-t_cf);\n", + "print (\"q = %.3f\")% (q), (\"W/m**2\")\n", + "\n", + "t2 = q/h_cf+t_cf;\n", + "print (\"(ii) Temperature of the outside surface of the tank = %.3f\")% (t2), (\"0C\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) Rate of heat loss per m**2 of the tank surface area\n", + "q = 795.301 W/m**2\n", + "(ii) Temperature of the outside surface of the tank = 94.530 0C\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.7 page no : 797" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Calculate\n", + "(i) The rate at which heat must be removed from the interior \n", + "(ii) The temperature on the outer surface of the metal sheet.\n", + "'''\n", + "# Variables\n", + "L_A = 0.003; \t\t\t#m\n", + "L_B = 0.05; \t\t\t#m\n", + "L_C = L_A;\n", + "k_A = 46.5; \t\t\t#W/m 0C\n", + "k_B = 0.046; \t\t\t#W/m 0C\n", + "k_C = k_A;\n", + "h0 = 11.6; \t\t\t#W/m**2 0C\n", + "hi = 14.5; \t\t\t#W/m**2 0C\n", + "t0 = 25.; \t\t\t#0C\n", + "ti = 6.; \t\t\t#0C\n", + "\n", + "# Calculations and Results\n", + "A = 0.5*0.5*2+0.5*1*4; \t\t\t#m**2\n", + "\n", + "Q = A*(t0-ti)/(1/h0 + L_A/k_A + L_B/k_B + L_C/k_C + 1/hi);\n", + "print (\"(i) The rate of removal of heat = %.3f\")% (Q), (\"W\")\n", + "\n", + "t1 = t0-Q/h0/A;\n", + "print (\"(ii) The temperature at the outer surface of the metal sheet = %.3f\")% (t1), (\"0C\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) The rate of removal of heat = 38.237 W\n", + "(ii) The temperature at the outer surface of the metal sheet = 23.681 0C\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.8 page no : 798" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Determine :\n", + "(i) The unknown thermal conductivity \u2018k\u2019 ;\n", + "(ii) The overall heat transfer coefficient ;\n", + "(iii) All surface temperatures.\n", + "'''\n", + "\n", + "#Varaible Declaration\n", + "L_A = 0.25; \t\t#m\n", + "import math \n", + "L_B = 0.1; \t\t\t#m\n", + "L_C = 0.15; \t\t#m\n", + "k_A = 1.65; \t\t#W/m \u00b0C\n", + "k_C = 9.2; \t\t\t#W/m \u00b0C\n", + "t_hf = 1250.; \t\t#\u00b0C\n", + "t1 = 1100.; \t\t\t#\u00b0C\n", + "t_cf = 25.; \t\t\t#\u00b0C\n", + "h_hf = 25.; \t\t\t#W/m**2 \u00b0C\n", + "h_cf = 12.; \t\t\t#W/m**2 \u00b0C\n", + "\n", + "\n", + "#Calculations and Results\n", + "q = h_hf*(t_hf-t1);\n", + "l = 0.0355\n", + "k_B = L_B/l #((t_hf-t_cf)/q-1/h_hf-L_A/k_A-L_C/k_C-1/h_cf);\n", + "print (\" (i)Thermal conductivity,k = %.3f\")% (k_B), (\"W/m**2 \u00b0C\")\n", + "\n", + "\n", + "R_th_total = 1./h_hf+L_A/k_A+L_B/k_B+L_C/k_C+1./h_cf;\n", + "U = 1/R_th_total\n", + "print (\"(ii) The overall transfer coefficient = %.2f\")% (U), (\"W/m**2 \u00b0C\")\n", + "\n", + "\n", + "print (\"(iii) All surface temperature \")\n", + "\n", + "print (\"t1 = %.3f\")% (t1),(\"\u00b0C\")\n", + "\n", + "t2 = t1-q*L_A/k_A;\n", + "print (\"t2 = %.3f\")% (t2), (\"\u00b0C\")\n", + "\n", + "t3 = t2-q*L_B/k_B;\n", + "print (\"t3 = %.3f\")% (t3), (\"\u00b0C\")\n", + "\n", + "t4 = t3-q*L_C/k_C;\n", + "print (\"t4 = %.3f\")% (t4), (\"\u00b0C\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " (i)Thermal conductivity,k = 2.817 W/m**2 \u00b0C\n", + "(ii) The overall transfer coefficient = 3.06 W/m**2 \u00b0C\n", + "(iii) All surface temperature \n", + "t1 = 1100.000 \u00b0C\n", + "t2 = 531.818 \u00b0C\n", + "t3 = 398.693 \u00b0C\n", + "t4 = 337.552 \u00b0C\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.9 page no : 802" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "calculate the heat loss per metre of length\n", + "'''\n", + "\n", + "import math\n", + "\n", + "# Variables\n", + "r1 = 0.01; \t\t\t#m\n", + "r2 = 0.02; \t\t\t#m\n", + "r3 = 0.05; \t\t\t#m\n", + "t1 = 600.; \t\t\t#0C\n", + "t3 = 1000.; \t\t#0C\n", + "k_B = 0.2; \t\t\t#W/m 0C\n", + "\n", + "# Calculations\n", + "q = 2*math.pi*(t1-t3)/(math.log(r3/r2)/k_B);\n", + "\n", + "# Results\n", + "print (\"Heat transfer per metre of length = %.3f\")% (q), (\"W/m\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Heat transfer per metre of length = -548.576 W/m\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.10 page no : 803" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Find the rate of heat loss from 60 m length of pipe.\n", + "'''\n", + "\n", + "import math\n", + "\n", + "# Variables\n", + "r1 = 0.06; \t\t\t#m\n", + "r2 = 0.12; \t\t\t#m\n", + "r3 = 0.16; \t\t\t#m\n", + "k_A = 0.24; \t\t\t#W/m 0C\n", + "k_B = 0.4; \t\t\t#W/m 0C\n", + "h_hf = 60.; \t\t\t#W/m**2 0C\n", + "h_cf = 12.; \t\t\t#W/m**2 0C\n", + "t_hf = 65.; \t\t\t#0C\n", + "t_cf = 20.; \t\t\t#0C\n", + "L = 60.; \t\t\t#m\n", + "\n", + "# Calculations\n", + "Q = 2*math.pi*L*(t_hf-t_cf)/(1/h_hf/r1 + math.log(r2/r1)/k_A + math.log(r3/r2)/k_B + 1/h_cf/r3);\n", + "\n", + "# Results\n", + "print (\"Rate of heat loss = %.3f\")% (Q), (\"W\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rate of heat loss = 3850.402 W\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.11 page no : 804" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "How thick should the asbestos be provided in order to limit the heat losses to 2.1 kW/m 2 ?\n", + "'''\n", + "\n", + "# Variables\n", + "r1 = 0.06; \t\t\t#m\n", + "r2 = 0.08; \t\t\t#m\n", + "k_A = 42.; \t\t\t#W/m 0C\n", + "k_B = 0.8; \t\t\t#W/m 0C\n", + "t_hf = 150.; \t\t\t#0C\n", + "t_cf = 20.; \t\t\t#0C\n", + "h_hf = 100.; \t\t\t#W/m**2 0C\n", + "h_cf = 30.; \t\t\t#W/m**2 0C\n", + "r3 = 0.105; \t\t\t#m\n", + "\n", + "# Calculations\n", + "thickness = (r3-r2)*1000; \t\t\t#mm\n", + "\n", + "# Results\n", + "print (\"Thickness of insulation = \"), (thickness), (\"mm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Thickness of insulation = 25.0 mm\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.12 page no : 807" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Find the rate of heat leakage\n", + "'''\n", + "\n", + "# Variables\n", + "r2 = 0.7; \t\t\t#m\n", + "r1 = 0.61; \t\t\t#m\n", + "dt = 220.; \t\t\t#dt = t1-t2; 0C\n", + "k = 0.083; \t\t\t#W/m 0C\n", + "\n", + "# Calculations\n", + "Q = dt/((r2-r1)/(4*math.pi*k*r1*r2));\n", + "\n", + "# Results\n", + "print (\"Rate of heat leakage = %.3f\")% (Q), (\"W\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rate of heat leakage = 1088.669 W\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.13 page no : 811" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Determine the critical thickness of insulation \n", + "\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "r1 = 0.001; \t\t\t#m\n", + "r2 = 0.0018; \t\t\t#m\n", + "k = 0.12; \t\t\t#W/m 0C\n", + "h0 = 35.; \t\t\t#W/m**2 0C\n", + "\n", + "# Calculations\n", + "rc = k/h0;\n", + "thickness = (rc-r1)*10**3; \t\t\t#mm\n", + "increase = (1/(math.log(rc/r1)/k + 1/h0/rc)-1/(math.log(r2/r1)/k + 1/h0/r2))/(1/(math.log(r2/r1)/k + 1/h0/r2))*100;\n", + "\n", + "# Results\n", + "print (\"Critical thickness of insulation = %.3f\")% (thickness), (\"mm\")\n", + "\n", + "print (\"Percentage change in heat transfer rate = %.3f\")% (increase), (\"%\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Critical thickness of insulation = 2.429 mm\n", + "Percentage change in heat transfer rate = 11.666 %\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.14 page no : 813" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "calculate the rate of heat transfer\n", + "'''\n", + "\n", + "# Variables\n", + "A = 1*1.5; \t\t\t#m**2\n", + "ts = 300.; \t\t\t#0C\n", + "tf = 20.; \t\t\t#0C\n", + "h = 20.; \t\t\t#W/m**2 0C\n", + "\n", + "# Calculations\n", + "Q = h*A*(ts-tf)/10**3; \t\t\t#kW\n", + "\n", + "# Results\n", + "print (\"Rate of heat transfer = \"), (Q), (\"kW\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rate of heat transfer = 8.4 kW\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.15 page no : 813" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "find how much electric power must be supplied to the wire to maintain the wire surface at 120\u00b0C ?\n", + "'''\n", + "\n", + "import math\n", + "\n", + "# Variables\n", + "d = 0.0015; \t\t\t#m\n", + "l = 0.15; \t\t\t#m\n", + "A = math.pi*d*l;\n", + "ts = 120.; \t\t\t#0C\n", + "tf = 100.; \t\t\t#0C\n", + "h = 4500.; \t\t\t#W/m**2 0C\n", + "\n", + "# Calculations\n", + "Q = h*A*(ts-tf);\n", + "\n", + "# Results\n", + "print (\"Electric power to be supplied = %.3f\")% (Q), (\"W\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Electric power to be supplied = 63.617 W\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.16 page no : 814" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Determine the heat transfer co-efficient and the rate of heat transfer \n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "D = 0.045; \t\t\t#m\n", + "l = 3.2; \t\t\t#m\n", + "u = 0.78; \t\t\t#m/s\n", + "k = 0.66; \t\t\t#W/m K\n", + "v = 0.478*10**(-6); \t\t\t#m**2/s\n", + "Pr = 2.98;\n", + "tw = 70.; \t\t\t#0C\n", + "tf = 50.; \t\t\t#0C\n", + "\n", + "# Calculations\n", + "A = math.pi*D*l;\n", + "Re = D*u/v;\n", + "h = 0.023*(Re)**0.8*(Pr)**0.4/D*k;\n", + "Q = h*A*(tw-tf)/10**3;\n", + "\n", + "# Results\n", + "print (\"Heat transfer co-efficient = %.3f\")% (h), (\"W/m**2 K\")\n", + "\n", + "print (\"Rate of heat transfer = %.3f\")% (Q), (\"kW\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Heat transfer co-efficient = 4078.018 W/m**2 K\n", + "Rate of heat transfer = 36.897 kW\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.17 page no : 814" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Determine the length of the tube required for developed flow.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "rho = 983.2; \t\t\t#kg/m**2\n", + "cp = 4.187; \t\t\t#kJ/kg K\n", + "k = 0.659; \t\t\t#W/m 0C\n", + "v = 0.478*10**(-6); \t\t\t#m**2/s\n", + "m = 0.5/60; \t\t\t#kg/s\n", + "D = 0.02; \t\t\t#m\n", + "ti = 20.; \t\t\t#0C\n", + "t0 = 50.; \t\t\t#0C\n", + "ts = 85.; \t\t\t#surface temperature in 0C\n", + "\n", + "# Calculations\n", + "tf = 1./2*(ts+(ti+t0)/2);\n", + "A = math.pi/4*D**2;\n", + "u = m/rho/A;\n", + "Re = D*u/v;\n", + "\n", + "#Since Re < 2000, hence the flow is laminar.\n", + "Nu = 3.65;\n", + "h = Nu*k/D;\n", + "tb = (t0+ti)/2;\n", + "L = m*cp*10**3*(t0-ti)/(ts-tb)/h/D/math.pi;\n", + "\n", + "# Results\n", + "print (\"Length of the tube required for fully developed flow = %.3f\")% (L), (\"m\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Length of the tube required for fully developed flow = 2.770 m\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.18 page no : 825" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "calculate the area of the heat exchanger.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "m_h = 0.2; \t\t\t#kg/s\n", + "m_c = 0.5; \t\t\t#kg/s\n", + "t_h1 = 75.; \t\t\t#0C\n", + "t_h2 = 45.; \t\t\t#0C\n", + "t_c1 = 20.; \t\t\t#0C\n", + "hi = 650.; \t\t\t#W/m**2 0C\n", + "h0 = hi;\n", + "cph = 4.187;\n", + "cpc = cph\n", + "\n", + "# Calculations\n", + "Q = m_h*cph*(t_h1-t_h2);\n", + "t_c2 = m_h*cph/cpc*(t_h1-t_h2)/m_c+t_c1;\n", + "theta = ((t_h1-t_c1)- (t_h2-t_c2))/math.log((t_h1-t_c1)/(t_h2-t_c2)); \t\t\t#Logarithmic mean temperature difference\n", + "U = hi*h0/(hi+h0);\n", + "A = Q*10**3/U/theta;\n", + "\n", + "# Results\n", + "print (\"The area of heat exchanger = %.3f\")% (A), (\"m**2\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The area of heat exchanger = 2.655 m**2\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.19 page no : 827" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Find-\n", + "(i) The rate of heat transfer,\n", + "(ii) The mass flow rate of water, and\n", + "(iii) The surface area of the heat exchanger.\n", + "'''\n", + "\n", + "# Variables\n", + "t_c1 = 25.; \t\t\t#0C\n", + "t_c2 = 65.; \t\t\t#0C\n", + "cph = 1.45; \t\t\t#kJ/kg K\n", + "m_h = 0.9; \t\t \t#kg/s\n", + "t_h1 = 230.; \t\t\t#0C\n", + "t_h2 = 160.; \t\t\t#0C\n", + "U = 420.; \t \t\t#W/m**2 0C\n", + "cpc = 4.187; \t\t\t#kJ/kg K\n", + "\n", + "# Calculations and Results\n", + "Q = m_h*cph*(t_h1-t_h2);\n", + "print (\"(i) The rate of heat transfer = \"), (Q), (\"kJ/s\")\n", + "\n", + "m_c = Q/cpc/(t_c2-t_c1);\n", + "print (\"(ii) The mass flow rate of water = %.3f\")% (m_c), (\"kg/s\")\n", + "\n", + "\n", + "print (\"(iii) The surface area of heat exchanger = \")\n", + "LMTD = ((t_h1-t_c2)- (t_h2-t_c1))/math.log((t_h1-t_c2)/(t_h2-t_c1)); \t\t\t#math.logarithmic mean temperature difference\n", + "A = Q*10**3/U/LMTD;\n", + "print (\"A = %.3f\")% (A), (\"m**2\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) The rate of heat transfer = 91.35 kJ/s\n", + "(ii) The mass flow rate of water = 0.545 kg/s\n", + "(iii) The surface area of heat exchanger = \n", + "A = 1.455 m**2\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.20 page no : 828" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "determine the number of tubes required. \n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "m_s = 800./60; \t\t\t#kg/s\n", + "m_c = m_s;\n", + "m_g = 1350./60; \t\t\t#kg/s\n", + "m_h = m_g;\n", + "t_h1 = 650.; \t\t\t#0C\n", + "t_c1 = 180.; \t\t\t#0C\n", + "t_c2 = 350.; \t\t\t#0C\n", + "d = 0.03; \t\t\t#m\n", + "L = 3.; \t\t\t#m\n", + "cph = 1.; \t\t\t#kJ/kg K\n", + "cpc = 2.71; \t\t\t#kJ/kg K\n", + "h_g = 250.;\n", + "h_s = 600.;\n", + "\n", + "# Calculations\n", + "t_h2 = round(t_h1-(m_c*cpc*(t_c2-t_c1)/cph/m_h));\n", + "U = round(h_g*h_s/(h_g+h_s),1);\n", + "Q = round(m_h*(cph*10**3)*(t_h1-t_h2),1);\n", + "theta = ((t_h1-t_c2)- (t_h2-t_c1))/math.log((t_h1-t_c2)/(t_h2-t_c1));# logarithmic mean temperature differenceN = Q/U/theta/(math.pi*d*L);\n", + "\n", + "\n", + "# Results\n", + "print (\"%.1f\")% (theta), (\"C\")\n", + "\n", + "N = (6142.5 * 10**3)/(U * 0.2827* theta)\n", + "print \"number of tubes required = %.0f\" % N\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "244.9 C\n", + "number of tubes required = 503\n" + ] + } + ], + "prompt_number": 31 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.21 page no : 829" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "calculate-\n", + "(i) Mass of cooling water circulated in kg/min,\n", + "(ii) Condenser surface area,\n", + "(iii) Number of tubes required per pass, and\n", + "(iv) Tube length.\n", + "'''\n", + "\n", + "\n", + "# Variables\n", + "di = 0.0296; \t\t\t#m\n", + "d0 = 0.0384; \t\t\t#m\n", + "U = 4000.; \t\t\t#W/m**2 0C\n", + "V = 3.; \t\t\t#m/s\n", + "t_c1 = 24.; \t\t\t#0C\n", + "x = 0.9;\n", + "ps = (760-660.)/760*1.0133; \t\t\t#bar\n", + "t_h1 = 51.; \t\t\t#0C\n", + "t_h2 = 51.; \t\t\t#0C\n", + "h_fg = 2592.; \t\t\t#kJ/kg\n", + "t_c2 = 47.; \t\t\t#0C\n", + "P = 15.; \t\t\t#MW\n", + "ssc = 5.; \t\t\t#specific steam consumption in kg/kWh\n", + "cpc = 4.187; \t\t\t#kJ?kg K\n", + "rho = 1000.;\n", + "\n", + "# Calculations and Results\n", + "m_s = P*10.**3*ssc/60; \t\t\t#kg/min\n", + "m_w = m_s*x*h_fg/cpc/(t_c2-t_c1);\n", + "print (\"(i) Mass of cooling water circulated per minute = %.3f\")% (m_w), (\"kg/min\")\n", + "\n", + "\n", + "Q = m_s*x*h_fg*10**3/60.;\n", + "\n", + "theta = ((t_h1-t_c1)- (t_h2-t_c2))/math.log((t_h1-t_c1)/(t_h2-t_c2)); \t\t\t#Logarithmic mean temperature difference\n", + "A = Q/U/theta;\n", + "print (\"(ii) Condenser surface area %.3f\")% (A), (\"m**2\")\n", + "\n", + "Np = m_w/60*4/math.pi/di**2/V/rho;\n", + "print (\"(iii) Number of tubes required per pass = %.3f\")% (Np)\n", + "\n", + "L = A/math.pi/d0/(2*Np);\n", + "print (\"(iv) Tube length = %.3f\")% (L), (\"m\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) Mass of cooling water circulated per minute = 30280.059 kg/min\n", + "(ii) Condenser surface area 1008.737 m**2\n", + "(iii) Number of tubes required per pass = 244.462\n", + "(iv) Tube length = 17.102 m\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.22 page no : 831" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Determine the water exit temperature\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "cp = 4.187; \t\t\t#kJ/kg \u00b0C\n", + "u = 0.596*10**(-3); \t#Ns/m**2\n", + "k = 0.635; \t\t\t #W/m \u00b0C\n", + "Pr = 3.93; \n", + "d = 0.020; \t\t\t#m\n", + "l = 2.; \t\t\t#m\n", + "m_c = 10.; \t\t\t#kg/s\n", + "t_c1 = 17.; \t\t#/\u00b0C\n", + "t_h1 = 100.; \t\t#\u00b0C\n", + "t_h2 = 100.; \t\t#\u00b0C\n", + "rho = 1000.;\n", + "N = 200.;\n", + "Np = N/l;\n", + "h0 = 10.*10**3;\n", + "\n", + "# Calculations\n", + "V = m_c*4/math.pi/d**2/rho/Np;\n", + "Re = rho*V*d/u;\n", + "hi = k/d*0.023*(Re)**0.8*(Pr)**0.33;\n", + "U = hi*h0/(hi+h0);\n", + "t_c2 = (U*math.pi*d*l*N*91.5 + m_c*cp*10**3*t_c1)/(m_c*cp*10**3 + U*math.pi*d*l*N*0.5);\n", + "\n", + "# Results\n", + "print (\"water exit temperature = %.3f\")% (t_c2), (\"\u00b0C\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "water exit temperature = 71.043 \u00b0C\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.23 page no : 842" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Calculate-\n", + "(i) The total rate of energy emission.\n", + "(ii) The intensity of normal radiation, and\n", + "(iii) The wavelength of maximum monochromatic emissive power.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "A = 0.12; \t\t\t#m**2\n", + "T = 800.; \t\t\t#K\n", + "a = 5.67*10**(-8);\n", + "\n", + "# Calculations and Results\n", + "Eb = a*A*T**4;\n", + "print (\"(i) The total rate of energy emission = %.3f\")% (Eb),(\"W\")\n", + "\n", + "Ibn = a*T**4/math.pi;\n", + "print (\"(ii) The intensity of normal radiation = %.3f\")% (Ibn), (\"W/m**2.sr\")\n", + "\n", + "wavelength = 2898/T;\n", + "print (\"(iii) The wavelength of maximum monochromatic emissive power = %.3f\")%(wavelength), (\"\u03bcm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) The total rate of energy emission = 2786.918 W\n", + "(ii) The intensity of normal radiation = 7392.531 W/m**2.sr\n", + "(iii) The wavelength of maximum monochromatic emissive power = 3.623 \u03bcm\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.24 page no : 842" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Calculate-\n", + "(i) The surface temperature of the sun, and\n", + "(ii) The heat flux at surface of the sun.\n", + "'''\n", + "\n", + "# Variables\n", + "wavelength = 0.49; \t\t\t#\u03bcm\n", + "a = 5.67*10**(-8);\n", + "\n", + "# Calculations and Results\n", + "T = 2898/wavelength;\n", + "print (\"(i) The surface temperature of the sun %.3f\")% (T), (\"K\")\n", + "\n", + "E_sun = a*T**4;\n", + "print (\"(ii) The heat flux at the surface of the sun = %.3f\")% (E_sun/1E+7), (\"*10^7 W/m**2\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) The surface temperature of the sun 5914.286 K\n", + "(ii) The heat flux at the surface of the sun = 6.937 *10^7 W/m**2\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.25 page no : 843" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Calculate-\n", + "(i) Monochromatic emissive power \n", + "(ii) Wavelength at which the emission is maximum,\n", + "(iii) Maximum emissive power,\n", + "(iv) Total emissive power, and\n", + "(v) Total emissive power of the furnance \n", + "'''\n", + "\n", + "import math\n", + "\n", + "# Variables\n", + "T = 2773. \t\t\t#K\n", + "lambda_ = 1.2 * 10**-6\n", + "e = 0.9;\n", + "a = 5.67*10**(-8);\n", + "\n", + "# Calculations and Results\n", + "print (\"(i) Monochromatic emissive power at 1.2 \u03bcm length\")\n", + "C1 = 0.3742*10**(-15); \t\t\t#W.m**4/m**2\n", + "C2 = 1.4388*10**(-4); \t\t\t#mK\n", + "E_lambda_b = C1*lambda_**(-5)/((math.exp(C2/lambda_/T)-1));\n", + "\n", + "print (\"E_lambda_b = %.2e\")% (E_lambda_b)\n", + "\n", + "lambda_max = 2898/T;\n", + "print (\"(ii) Wavelength at which the emission is maximum = %.3f\")% (lambda_max), (\"\u03bcm\")\n", + "\n", + "E_lambda_b_max = 1.285*10.**(-5)*T**5;\n", + "print (\"(iii) Maximum emissive power = %.3f\")% (E_lambda_b_max/1E+12), (\"*10^12 W/m**2 per metre length\")\n", + "\n", + "Eb = a*T**4;\n", + "print (\"(iv) Total emissive power = %.3f\")% (Eb/1E+6), (\"*10^6 W/m**2\")\n", + "\n", + "E = e*a*T**4;\n", + "print (\"(v) Total emissive power = %.3f\")% (E/1E+6), (\"*10^6 W/m**2\")\n", + "\n", + "# Book answers are wrong. Please calculate them manually." + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) Monochromatic emissive power at 1.2 \u03bcm length\n", + "E_lambda_b = 3.40e+15\n", + "(ii) Wavelength at which the emission is maximum = 1.045 \u03bcm\n", + "(iii) Maximum emissive power = 2.107 *10^12 W/m**2 per metre length\n", + "(iv) Total emissive power = 3.353 *10^6 W/m**2\n", + "(v) Total emissive power = 3.017 *10^6 W/m**2\n" + ] + } + ], + "prompt_number": 40 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.26 page no : 845" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "(i) When the body is assumed to be grey with \u03b5 = 0.42.\n", + "(ii) When the body is not grey.\n", + "'''\n", + "\n", + "# Variables\n", + "T1 = 1273.; \t\t\t#K\n", + "T2 = 773.; \t\t\t#K\n", + "e1 = 0.42;\n", + "e2 = 0.72;\n", + "a = 5.67*10**(-8);\n", + "\n", + "# Calculations and Results\n", + "print (\"(i) When the body is grey with \u03b51 = 0.42\")\n", + "q = e1*a*(T1**4-T2**4)/10**3; \t\t\t#kW\n", + "print (\"Heat loss per m2 by radiation = %.3f\")% (q), (\"kW\")\n", + "print (\"(ii) When the body is not grey\")\n", + "E_emitted = e1*a*T1**4;\n", + "E_absorbed = e2*a*(T2)**4;\n", + "q = (E_emitted-E_absorbed)/10**3;\n", + "print (\"Heat loss per m2 by radiation = %.3f\")% (q), (\"kW\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) When the body is grey with \u03b51 = 0.42\n", + "Heat loss per m2 by radiation = 54.036 kW\n", + "(ii) When the body is not grey\n", + "Heat loss per m2 by radiation = 47.962 kW\n" + ] + } + ], + "prompt_number": 28 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.27 page no : 846" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "find the time required for the heating operation.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "d = 0.022; \t\t\t#m\n", + "di = 0.18; \t\t\t#m\n", + "e1 = 0.62;\n", + "e2 = 0.82;\n", + "rho = 7845.; \t\t\t#kg/m**3\n", + "T1a = 693.; \t\t\t#K; For caseI\n", + "T1b = 813.; \t\t\t#K; For caseII\n", + "T2 = 1373.; \t\t\t#K\n", + "l = 1.; \t\t\t#m\n", + "a = 5.67*10**(-8); \n", + "cp = 0.67; \t\t\t#kJ/kg K\n", + "\n", + "# Calculations\n", + "A1 = math.pi*d*l;\n", + "A2 = math.pi*di*l;\n", + "Qi = A1*a*(T1a**4-T2**4)/(1/e1+A1/A2*(1/e2 - 1));\n", + "Qe = A1*a*(T1b**4-T2**4)/(1/e1+A1/A2*(1/e2 - 1));\n", + "Qav = -(Qi+Qe)/2;\n", + "t_h = math.pi/4*d**2*rho*cp*(T1b-T1a)*10**3/Qav;\n", + "\n", + "# Results\n", + "print (\"Time required for the heating operation %.3f\")% (t_h),(\"s\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Time required for the heating operation 31.157 s\n" + ] + } + ], + "prompt_number": 29 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.28 page no : 847" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Calculate the heat transfer rate \n", + "'''\n", + "\n", + "# Variables\n", + "r1 = 0.05; \t\t\t#m\n", + "r2 = 0.1; \t\t\t#m\n", + "T1 = 400.; \t\t\t#K\n", + "T2 = 300.; \t\t\t#K\n", + "e1 = 0.5;\n", + "e2 = 0.5;\n", + "F_12 = 1.;\n", + "\n", + "# Calculations\n", + "a = 5.67*10**(-8);\n", + "#A1/A2 = r1/r2\n", + "Q = a*(T1**4-T2**4)/((1-e1)/e1+1/F_12+(1-e2)/e2*r1/r2);\n", + "\n", + "# Results\n", + "print (\"heat transfer rate per m2 area by radiation\"), (Q), (\"W/m**2\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "heat transfer rate per m2 area by radiation 396.9 W/m**2\n" + ] + } + ], + "prompt_number": 30 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.29 page no : 847" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "calculate the steady state temperature of cylinder surface \n", + "'''\n", + "\n", + "# Variables\n", + "r1 = 0.05; \t\t\t#m\n", + "r2 = 0.1; \t\t\t#m\n", + "r3 = 0.15; \t\t\t#m\n", + "T1 = 1000.; \t\t\t#K\n", + "T3 = 500.; \t\t\t#K\n", + "e1 = 0.05;\n", + "e2 = e1;\n", + "e3 = e1;\n", + "a = 5.67*10**(-8);\n", + "F_12 = 1.;\n", + "F_23 = 1.;\n", + "T2 = 770.; \t\t\t#K\n", + "\n", + "# Calculations\n", + "Q1 = a*(T1**4-T2**4)/(((1-e1)/e1) + 1/F_12 + ((1-e2)/e2)*r1/r2);\n", + "\n", + "# Results\n", + "print (\"Heat flow per m2 area of cylinder 1 = %.3f\")% (Q1), (\"W\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Heat flow per m2 area of cylinder 1 = 1246.381 W\n" + ] + } + ], + "prompt_number": 31 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.30 page no : 848" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Calculate the rate of evaporation of liquid air.\n", + "'''\n", + "\n", + "# Variables\n", + "r1 = 0.105; \t\t\t#m\n", + "r2 = 0.15; \t\t\t#m\n", + "T1 = 120.; \t\t\t#K\n", + "T2 = 300.; \t\t\t#K\n", + "e1 = 0.03;\n", + "e2 = 0.03;\n", + "h_fg = 209.35; \t\t\t#kJ/kg\n", + "a = 5.67*10**(-8);\n", + "F_12 = 1.;\n", + "\n", + "# Calculations\n", + "Q = 4*math.pi*r1**2*a*(T1**4-T2**4)/( ((1-e1)/e1) + 1./F_12 + ((1-e2)/e2)*r1**2/r2**2);\n", + "rate = -Q*3600./h_fg/1000;\n", + "\n", + "# Results\n", + "print (\"Rate of evaporation = %.3f\")% (rate), (\"kg/h\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rate of evaporation = 0.022 kg/h\n" + ] + } + ], + "prompt_number": 32 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.31 page no : 849" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Estimate the rate of heat flow by radiation to the oxygen in the container.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "T1 = 91.; \t\t\t#K\n", + "T2 = 303.; \t\t\t#K\n", + "e1 = 0.03;\n", + "e2 = 0.03;\n", + "d1 = 0.3; \t\t\t#m\n", + "d2 = 0.45; \t\t\t#m\n", + "a = 5.67*10**(-8);\n", + "F_12 = 1.;\n", + "\n", + "# Calculations\n", + "Q = 4*math.pi*(d1/2)**2*a*(T1**4-T2**4)/( ((1-e1)/e1) + 1/F_12 + ((1-e2)/e2)*d1**2/d2**2);\n", + "\n", + "# Results\n", + "print (\"Rate of heat flow = %.3f\")% (Q), (\"W\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rate of heat flow = -2.810 W\n" + ] + } + ], + "prompt_number": 33 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 15.32 page no : 850" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Find the percentage reduction when a polished aluminium shield\n", + "'''\n", + "\n", + "# Variables\n", + "e1 = 0.3;\n", + "e2 = 0.8;\n", + "e3 = 0.04;\n", + "A1 = 1.; \t\t\t#m**2\n", + "A2 = A1;\n", + "A3 = A1;\n", + "\n", + "# Calculations\n", + "reduction = 1-0.131*0.52;\n", + "\n", + "# Results\n", + "print (\"Percentage reduction in heat flow due to shield = %.3f\")% (reduction), (\"%\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Percentage reduction in heat flow due to shield = 0.932 %\n" + ] + } + ], + "prompt_number": 34 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch16.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch16.ipynb new file mode 100644 index 00000000..32a42e59 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch16.ipynb @@ -0,0 +1,1047 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 16 : Compressible Flow" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.1 page no : 859" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Find the velocity of the gas \n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "V1 = 300.; \t\t\t#m/s\n", + "p1 = 78.; \t\t\t#kN/m**2\n", + "T1 = 313.; \t\t\t#K\n", + "p2 = 117.; \t\t\t#kN/m**2\n", + "R = 287.; \t\t\t#J/kg K\n", + "y = 1.4;\n", + "\n", + "# Calculations\n", + "#Let r1 = p1/rho1\n", + "r1 = R*T1;\n", + "V2 = math.sqrt(2*(y/(y-1)*r1*(1-(p2/p1)**((y-1)/y)) + V1**2/2));\n", + "\n", + "# Results\n", + "print (\"Velocity of gas at section 2 = %.3f\")% (V2), (\"m/s\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Velocity of gas at section 2 = 112.987 m/s\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.2 page no : 861" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "determine the pressure and the temperature \n", + "'''\n", + "\n", + "# Variables\n", + "p1 = 35.; \t\t\t#kN/m**2\n", + "V1 = 30.; \t\t\t#m/s\n", + "T1 = 423.; \t\t\t#K\n", + "V2 = 150.; \t\t\t#m/s\n", + "R = 290.; \t\t\t#J/kg K\n", + "y = 1.4;\n", + "\n", + "# Calculations\n", + "#Let r1 = p2/p1\n", + "r1 = R*T1; \n", + "p2 = p1*(1-((V2**2/2-V1**2/2)*(y-1)/y/r1))**(y/(y-1));\n", + "print (\"pressure = %.3f\")% (p2), (\"kN/m**2\")\n", + "\n", + "T2 = T1*(p2/p1)**((y-1)/y);\n", + "t2 = T2-273;\n", + "print (\"temperature = %.3f\")% (t2), (\"\u00b0C\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "pressure = 32.014 kN/m**2\n", + "temperature = 139.360 \u00b0C\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.3 page no : 866" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Find the sonic velocity of -\n", + "(i) Crude oil \n", + "(ii) Mercury \n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "SG = 0.8;\n", + "rho_oil = 800.; \t\t\t#kg/m**3\n", + "K_oil = 1.5*10**9; \t\t\t#N/m**2; crude oil\n", + "K_Hg = 27*10.**9; \t\t\t#N/m**2; Mercury\n", + "rho_Hg = 13600.; \t\t\t#kg/m**3\n", + "\n", + "# Calculations and Results\n", + "C_oil = math.sqrt(K_oil/rho_oil);\n", + "print (\"Sonic velocity of crude oil = %.3f\")% (C_oil), (\"m/s\")\n", + "\n", + "C_Hg = math.sqrt(K_Hg/rho_Hg)\n", + "print (\"Sonic velocity of Mercury = %.3f\")% (C_Hg), (\"m/s\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sonic velocity of crude oil = 1369.306 m/s\n", + "Sonic velocity of Mercury = 1409.005 m/s\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.4 page no : 866" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Find the speed of the plane\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "T = 228.; \t\t\t#K\n", + "M = 2.;\n", + "R = 287.; \t\t\t#Jkg K\n", + "y = 1.4;\n", + "\n", + "# Calculations\n", + "C = math.sqrt(y*R*T);\n", + "V = M*C*3600./1000;\n", + "\n", + "# Results\n", + "print (\"Velocity of the plane = %.3f\")% (V), (\"km/h\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Velocity of the plane = 2179.239 km/h\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.5 page no : 868" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Find the velocity of a bullet fired \n", + "\n", + "# Variables\n", + "a = 40*math.pi/180; \t\t\t#Mach angle in radians\n", + "y = 1.4;\n", + "R = 287.; \t\t\t#J/kg K\n", + "T = 288.; \t\t\t#K\n", + "\n", + "# Calculations\n", + "C = math.sqrt(y*R*T);\n", + "V = C/math.sin(a);\n", + "\n", + "# Results\n", + "print (\"Velocity of bullet = %.3f\")% (V), (\"m/s\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Velocity of bullet = 529.217 m/s\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.6 page no : 868" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "find the velocity of the projectile. \n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "p = 88.3; \t\t\t#kN/m**2\n", + "T = 271.; \t\t\t#K\n", + "M = 40.*math.pi/180;\n", + "y = 1.4;\n", + "R = 287.; \t\t\t#J/kg K\n", + "\n", + "# Calculations\n", + "C = math.sqrt(y*R*T);\n", + "V = C/math.sin(M);\n", + "\n", + "# Results\n", + "print (\"Velocity of the projectile = %.3f\")% (V), (\"m/s\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Velocity of the projectile = 513.360 m/s\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.7 page no : 868" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Determine the speed of the aircraft \n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "h = 1800.; \t\t\t#m\n", + "T = 277.; \t\t\t#K\n", + "t = 4.; \t\t\t#s\n", + "y = 1.4;\n", + "R = 287.; \t\t\t#J/kg K\n", + "\n", + "# Calculations\n", + "C = math.sqrt(y*R*T);\n", + "a = (math.cos(C/h*t));\n", + "V = C/math.sin(a)*3600/1000;\n", + "\n", + "# Results\n", + "print (\"Speed of the aircraft = %.3f\")% (V), (\"km/h\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Speed of the aircraft = 1785.959 km/h\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.8 page no : 873" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Calculate-\n", + "(i) Stagnation pressure,\n", + "(ii) Stagnation temperature, and\n", + "(iii) Stagnation density.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "R = 287.; \t\t\t#J/kg K\n", + "y = 1.4;\n", + "V0 = 1000.*1000/3600; \t\t\t#m/s\n", + "p0 = 78.5; \t\t\t#kN/m**2\n", + "T0 = 265.; \t\t\t#K\n", + "\n", + "# Calculations and Results\n", + "C0 = math.sqrt(y*R*T0);\n", + "M0 = V0/C0;\n", + "\n", + "ps = p0*(1+((y-1)/2*M0**2))**(y/(y-1));\n", + "print (\"(i) Stagnation pressure = %.3f\")%(ps), (\"kN/m**2\")\n", + "\n", + "Ts = T0*(1+((y-1)/2*M0**2));\n", + "print (\"(ii) Stagnation temperature = %.3f\")% (Ts), (\"K\")\n", + "\n", + "rho_s = ps*10**3/R/Ts;\n", + "print (\"(iii) Stagnation density = %.3f\")%(rho_s), (\"kg/m**3\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) Stagnation pressure = 126.067 kN/m**2\n", + "(ii) Stagnation temperature = 303.407 K\n", + "(iii) Stagnation density = 1.448 kg/m**3\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.9 page no : 874" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Compute stagnation properties and the local Mach number. \n", + "What would be the compressibility correction factor for a pitot-static tube \n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "V0 = 1000.*1000./3600; \t\t\t#m/s\n", + "T0 = 320.; \t\t\t#K\n", + "p_atm = 98.1; \t\t\t#kN/m**2\n", + "p = 9.81; \t\t\t#kN/m**2\n", + "p0 = 98.1-p;\n", + "R = 287.; \t\t\t#J/kg K\n", + "y = 1.4;\n", + "\n", + "# Calculations and Results\n", + "C0 = math.sqrt(y*R*T0);\n", + "M0 = V0/C0;\n", + "\n", + "ps = p0*(1+((y-1)/2*M0**2))**(y/(y-1));\n", + "print (\"Stagnation pressure = %.3f\")% (ps), (\"kN/m**2\")\n", + "\n", + "Ts = T0*(1+((y-1)/2*M0**2));\n", + "print (\"Stagnation temperature = %.1f\")% (Ts), (\"K\")\n", + "\n", + "rho_s = ps*10**3/R/Ts;\n", + "print (\"Stagnation density = %.3f\")% (rho_s), (\"kg/m**3\")\n", + "\n", + "M = 0.8;\n", + "\n", + "CF = 1+(M0**2./4)+((2-y)/24.*M0**4);\n", + "print (\"Compressibility factor %.2f\")% (CF)\n", + "\n", + "#Note : Answers are slightly different because of rounding error." + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Stagnation pressure = 131.282 kN/m**2\n", + "Stagnation temperature = 358.4 K\n", + "Stagnation density = 1.276 kg/m**3\n", + "Compressibility factor 1.16\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.10 page no : 875" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Calculate stagnation pressure if -\n", + "(i) Compressibility is neglected ;\n", + "(ii) Compressibility is accounted for.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "R = 287.; \t\t\t#J/kg K\n", + "y = 1.4;\n", + "p0 = 220.*10**3; \t#N/m**2\n", + "T0 = 300.; \t\t\t#K\n", + "V0 = 200.; \t\t\t#m/s\n", + "\n", + "# Calculations and Results\n", + "C0 = math.sqrt(y*R*T0);\n", + "rho_0 = p0/R/T0;\n", + "print (\"Stagnation pressure = \")\n", + "\n", + "print (\"(i) Compressibility is neglected\")\n", + "ps = (p0+rho_0*V0**2/2)/10**3;\n", + "print (\"ps = %.3f\")% (ps), (\"kN/m**2\")\n", + "\n", + "print (\"(ii) Compressibility is accounted for\")\n", + "M0 = V0/C0;\n", + "\n", + "ps = (p0+rho_0*V0**2/2*(1+M0**2./4+(2-y)/24*M0**4))/10**3;\n", + "print (\"ps = %.3f\")% (ps), (\"kN/m**2\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Stagnation pressure = \n", + "(i) Compressibility is neglected\n", + "ps = 271.103 kN/m**2\n", + "(ii) Compressibility is accounted for\n", + "ps = 275.484 kN/m**2\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.11 page no : 875" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Calculate the speed of the aircraft. \n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "p0 = 35.*10**3; \t\t\t#Pa\n", + "T0 = 235.; \t\t\t#K\n", + "ps = 65.4*10**3; \t\t\t#N/m**2\n", + "R0 = 8314.; \t\t\t#Nm/mole K\n", + "M = 28.;\n", + "\n", + "# Calculations\n", + "R = R0/M;\n", + "rho_0 = p0/R/T0;\n", + "Va = math.sqrt(2*(ps-p0)/rho_0);\n", + "\n", + "# Results\n", + "print (\"Speed of the aircraft = %.3f\")% (Va), (\"m/s\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Speed of the aircraft = 348.159 m/s\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.12 page no : 885" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "What is the true speed of the aircraft ?\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "p0 = 30.*10**3; \t\t\t#N/m**2\n", + "V0 = 152.; \t\t\t#m/s\n", + "y = 1.4;\n", + "rho_0 = 1.224; \t\t\t#kg/m**3\n", + "ps = p0+rho_0*V0**2/2;\n", + "\n", + "# Calculations\n", + "rho_0 = 0.454; \t\t\t#kg/m**3\n", + "V0 = math.sqrt(2*(ps-p0)/rho_0);\n", + "C0 = math.sqrt(y*p0/rho_0);\n", + "M = V0/C0;\n", + "ccf = (1+M**2/4); \t\t\t#Compressibility correction factor\n", + "V = V0/math.sqrt(ccf); \t\t\t#True speed of aircraft\n", + "\n", + "# Results\n", + "print (\"True speed of aircraft = %.3f\")% (V), (\"m/s\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "True speed of aircraft = 230.900 m/s\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.13 page no : 886" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Determine the reservoir pressure, temperature and the throat area.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "M = 3.; \t\t\t#Mach number\n", + "d = 0.2; \t\t\t#m\n", + "p_nozzle = 7.85; \t\t\t#kN/m**2\n", + "T_nozzle = 200.; \t\t\t#K\n", + "y = 1.4;\n", + "\n", + "# Calculations and Results\n", + "A = math.pi/4*d**2;\n", + "p_res = p_nozzle*(1+((y-1)/2*M**2))**(y/(y-1));\n", + "print (\"Reservoir pressure = %.3f\")% (p_res), (\"kN/m**2\")\n", + "\n", + "T_res = T_nozzle*(1+((y-1)/2*M**2));\n", + "print (\"Reservoir temperature = %.3f\")% (T_res), (\"K\")\n", + "\n", + "Ac = A*M/((2+(y-1)*M**2)/(y+1))**((y+1)/2/(y-1));\n", + "print (\"Throat area (critical) = %.5f\")% (Ac), (\"m**2\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reservoir pressure = 288.352 kN/m**2\n", + "Reservoir temperature = 560.000 K\n", + "Throat area (critical) = 0.00742 m**2\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.14 page no : 887" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "find the maximum flow rate of air.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "R = 287.; \t\t\t#J/kg K\n", + "y = 1.4;\n", + "p_atm = 100.; \t\t\t#kN/m**2\n", + "p1 = 284. + p_atm; \t\t\t#kN/m**2\n", + "T1 = 297.; \t \t\t#K\n", + "D = 0.02; \t\t \t#m\n", + "\n", + "# Calculations\n", + "A2 = math.pi/4*D**2;\n", + "rho_1 = p1*10**3/R/T1;\n", + "m_max = 0.685*A2*math.sqrt(p1*10**3*rho_1);\n", + "\n", + "# Results\n", + "print (\"Maximum flow rate = %.3f\")% (m_max), (\"kg/s\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Maximum flow rate = 0.283 kg/s\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.15 page no : 888" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "find the velocity of air flowing at the outlet of the nozzle.\n", + "'''\n", + "\n", + "# Variables\n", + "R = 287.; \t\t\t#J/kg K\n", + "y = 1.4;\n", + "p1 = 2500.*10**3; \t#N/m**2\n", + "T1 = 293.; \t\t\t#K\n", + "p2 = 1750.*10**3; \t#N/m**2\n", + "\n", + "# Calculations\n", + "rho_1 = p1/R/T1;\n", + "V2 = math.sqrt(2*y/(y-1)*p1/rho_1*(1-(p2/p1)**((y-1)/y)));\n", + "\n", + "# Results\n", + "print (\"Velocity of air = %.3f\")% (V2),(\"m/s\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Velocity of air = 238.812 m/s\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.16 page no : 889" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "find the mass rate of flow of air through the nozzle to the atmosphere when the pressure in the tank is-\n", + "(i) 140 kN/m 2 (abs.),\n", + "(ii) 300 kN/m 2\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "R = 287.; \t \t\t#J/kg K\n", + "y = 1.4;\n", + "p_atm = 10.**5; \t\t#N/m**2\n", + "T1 = 293. \t\t\t#K\n", + "D2 = 0.025; \t\t\t#m\n", + "p1 = 140.*10**3; \t\t#N/m**2\n", + "\n", + "# Calculations and Results\n", + "A2 = math.pi/4*D2**2;\n", + "\n", + "print (\"(i) Mass rate of flow of air when pressure in the math.tank is 140 kN/m2 (abs.)\")\n", + "rho_1 = p1/R/T1;\n", + "p2 = 10**5; \t\t\t#N/m**2\n", + "\n", + "m = A2*math.sqrt(2*y/(y-1)*p1*rho_1*((p2/p1)**(2/y) - (p2/p1)**((y+1)/y)));\n", + "print (\"m = %.3f\")% (m), (\"kg/s\")\n", + "\n", + "print (\"(ii) Mass rate of flow of air when pressure in the math.tank is 300 kN/m2 (abs.)\")\n", + "p1 = 300.*10**3; \t\t\t#N/m**2\n", + "p2 = 10.**5; \t\t\t#N/m**2\n", + "rho_1 = p1/R/T1;\n", + "\n", + "print (\"The pressure ratio p2/p1 being less than the critical ratio 0.528, the flow in the nozzle will be sonic\");\n", + "\n", + "m_max = 0.685*A2*math.sqrt(p1*rho_1);\n", + "print (\"m_max = %.3f\")% (m_max), (\"kg/s\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) Mass rate of flow of air when pressure in the math.tank is 140 kN/m2 (abs.)\n", + "m = 0.149 kg/s\n", + "(ii) Mass rate of flow of air when pressure in the math.tank is 300 kN/m2 (abs.)\n", + "The pressure ratio p2/p1 being less than the critical ratio 0.528, the flow in the nozzle will be sonic\n", + "m_max = 0.348 kg/s\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.17 page no : 890" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Determine-\n", + "(i) Stagnation temperature and stagnation pressure,\n", + "(ii) Sonic velocity and Mach number at this section,\n", + "(iii) Velocity, Mach number and flow area at outlet section where pressure is 110 kN/m 2 ,\n", + "(iv) Pressure, temperature, velocity and flow area at throat of the nozzle.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "p1 = 200.; \t\t\t#kN/m**2\n", + "V1 = 170.; \t\t\t#m/s\n", + "T1 = 473.; \t\t\t#K\n", + "A1 = 0.001; \t\t#m**2\n", + "R = 287.; \t\t\t#J/kg K\n", + "cp = 1000.; \t\t#J/kg K\n", + "y = 1.4;\n", + "\n", + "# Calculations and Results\n", + "print (\"(i) Stagnation temperature (Ts) and stagnation pressure (ps)\")\n", + "\n", + "Ts = T1+V1**2/2/cp;\n", + "print (\"Ts = %.3f\")% (Ts), (\"K\")\n", + "\n", + "ps = p1*(Ts/T1)**(y/(y-1));\n", + "print (\"ps = %.3f\")% (ps), (\"kN/m**2\")\n", + "\n", + "print (\"(ii) Sonic velocity and Mach number at this section\")\n", + "\n", + "C1 = math.sqrt(y*R*T1);\n", + "print (\"Sonic velocity = %.3f\")% (C1), (\"m/s\")\n", + "\n", + "M1 = V1/C1;\n", + "print (\"Mach number = %.3f\")% (M1)\n", + "\n", + "print (\"(iii) Velocity, Mach number and flow area at outlet section where pressure is 110 kN/m2\")\n", + "p2 = 110.; \t\t\t#kN/m**2\n", + "M2 = math.sqrt(2/(y-1)*((ps/p2)**((y-1)/y) - 1));\n", + "print (\"M2 = %.3f\")% (M2)\n", + "\n", + "T2 = Ts*(p2/ps)**((y-1)/y);\n", + "C2 = math.sqrt(y*R*T2);\n", + "V2 = M2*C2;\n", + "print (\"V2 = %.3f\")% (V2), (\"m/s\")\n", + "\n", + "A2 = (p1*A1*V1*T2/T1/p2/V2)*10**6;\n", + "print (\"A2 = %.3f\")% (A2), (\"mm**2\")\n", + "\n", + "\n", + "print (\"(iv) Pressure (pt), temperature (Tt), velocity (Vt), and flow area (At) at throat of the nozzle\")\n", + "Mt = 1.;\n", + "Tt = Ts/(1+(y-1)/2*Mt**2);\n", + "print (\"Tt = %.3f\")% (Tt), (\"K\")\n", + "\n", + "pt = ps*(Tt/Ts)**(y/(y-1));\n", + "print (\"pt = %.3f\")% (pt), (\"kN/m**2\")\n", + "\n", + "Ct = math.sqrt(y*R*Tt);\n", + "Vt = Mt*Ct;\n", + "\n", + "At = (p1*A1*V1*Tt/T1/pt/Vt)*10**6;\n", + "print (\"At = %.3f\")% (At), (\"mm**2\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) Stagnation temperature (Ts) and stagnation pressure (ps)\n", + "Ts = 487.450 K\n", + "ps = 222.214 kN/m**2\n", + "(ii) Sonic velocity and Mach number at this section\n", + "Sonic velocity = 435.949 m/s\n", + "Mach number = 0.390\n", + "(iii) Velocity, Mach number and flow area at outlet section where pressure is 110 kN/m2\n", + "M2 = 1.055\n", + "V2 = 422.183 m/s\n", + "A2 = 617.168 mm**2\n", + "(iv) Pressure (pt), temperature (Tt), velocity (Vt), and flow area (At) at throat of the nozzle\n", + "Tt = 406.208 K\n", + "pt = 117.392 kN/m**2\n", + "At = 615.673 mm**2\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.18 page no : 893" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "determine the flow conditions before and after the shock wave.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "y = 1.4;\n", + "p1 = 26.5; \t\t\t#kN/m**2\n", + "rho_1 = 0.413; \t\t\t#kg/m**3\n", + "M1 = 2.;\n", + "R = 287.;\n", + "\n", + "# Calculations and Results\n", + "M2 = math.sqrt(((y-1)*M1**2 + 2)/(2*y*M1**2 - (y-1)));\n", + "print (\"Mach number M2 = %.3f\")% (M2)\n", + "\n", + "p2 = p1*(2*y*M1**2 - (y-1))/(y+1);\n", + "print (\"p2 = %.3f\")% (p2), (\"kN/m**2\")\n", + "\n", + "rho_2 = rho_1*((y+1)*M1**2)/((y-1)*M1**2 + 2);\n", + "print (\"density, rho_2 = %.3f\")% (rho_2), (\"kg/m**3\")\n", + "\n", + "T1 = p1*10**3/rho_1/R;\n", + "print (\"T1 = %.3f\")% (T1), (\"K\")\n", + "\n", + "T2 = T1*((y-1)*M1**2 + 2)*(2*y*M1**2 - (y-1))/((y+1)**2*M1**2);\n", + "print (\"T2 = %.3f\")% (T2), (\"K\")\n", + "\n", + "C1 = math.sqrt(y*R*T1);\n", + "V1 = M1*C1;\n", + "print (\"V1 = %.3f\")% (V1), (\"m/s\")\n", + "\n", + "C2 = math.sqrt(y*R*T2);\n", + "V2 = M2*C2;\n", + "print (\"V2 = %.3f\")% (V2), (\"m/s\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mach number M2 = 0.577\n", + "p2 = 119.250 kN/m**2\n", + "density, rho_2 = 1.101 kg/m**3\n", + "T1 = 223.570 K\n", + "T2 = 377.275 K\n", + "V1 = 599.435 m/s\n", + "V2 = 224.788 m/s\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 16.19 page no : 895" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Determine :\n", + "(i) Pressure, temperature and Mach number downstream of the shock, and\n", + "(ii) Strength of shock.\n", + "'''\n", + "\n", + "import math \n", + "\n", + "# Variables\n", + "M1 = 1.5;\n", + "p1 = 170.; \t\t\t#kN/m**2\n", + "T1 = 296.; \t\t\t#K\n", + "y = 1.4;\n", + "\n", + "# Calculations and Results\n", + "print (\"(i) Pressure, temperature and Mach number downstream of the shock\")\n", + "p2 = p1*(2*y*M1**2 - (y-1))/(y+1);\n", + "print (\"p2 = %.3f\")% (p2), (\"kN/m**2\")\n", + "\n", + "T2 = T1*((y-1)*M1**2 + 2)*(2*y*M1**2 - (y-1))/(y+1)**2/M1**2;\n", + "print (\"T2 = %.3f\")% (T2), (\"K\")\n", + "\n", + "M2 = math.sqrt(((y-1)*M1**2 + 2)/(2*y*M1**2 - (y-1)));\n", + "print (\"M2 = %.3f\")% (M2)\n", + "\n", + "strength = p2/p1 - 1;\n", + "print (\"Strength of stock = %.3f\")% (strength)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) Pressure, temperature and Mach number downstream of the shock\n", + "p2 = 417.917 kN/m**2\n", + "T2 = 390.784 K\n", + "M2 = 0.701\n", + "Strength of stock = 1.458\n" + ] + } + ], + "prompt_number": 21 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch2.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch2.ipynb new file mode 100644 index 00000000..606044d0 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch2.ipynb @@ -0,0 +1,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": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch3.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch3.ipynb new file mode 100644 index 00000000..8a41802e --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch3.ipynb @@ -0,0 +1,606 @@ +{ + "metadata": { + "name": "ch3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.1 Testing for Divisibility\nThis program tests if one positive integer is not divisible by another:\n'''\n\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\nif (n%d):\n print \"%d is not divisible by %d\" %(n,d)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two positive integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "66\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "7\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "66 is not divisible by 7\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.2 Testing for Divisibility Again\nThis program is the same as the program in Example 3.1 except that the if statement has been replaced\nby an if..else statement:\n'''\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\nif (n%d):\n print \"%d is not divisible by %d\" %(n,d)\nelse:\n print \"%d is divisible by %d\" %(n,d)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two positive integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "56\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "7\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "56 is divisible by 7\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.3 The Minimum of Two Integers\nThis program prints the minimum of the two integers entered:\n'''\n\nprint \"Enter two integers: \"\nm = int(raw_input())\nn = int(raw_input())\n\nif (m < n):\n print \"%d is the minimum.\" %m\nelse:\n print \"%d is the minimum.\" %n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "55 is the minimum.\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.4 A Common Programming Error\nThis program is erroneous:\n'''\nprint \"Enter an integer: \"\nn = int(raw_input())\nif (n = 22):\n print \"%d = 22\" %n\nelse: \n print \"%d != 22\" %n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (<ipython-input-4-d2cee3e182b1>, line 7)", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"<ipython-input-4-d2cee3e182b1>\"\u001b[1;36m, line \u001b[1;32m7\u001b[0m\n\u001b[1;33m if (n = 22):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.5 The Minimum of Three Integers\nThis program is similar to the one in Example 3.3 except that it applies to three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\n\nm=n1\n# now min <= n1\nif (n2 < m):\n m = n2 # now min <= n1 and min <= n2\nif (n3 < m):\n m = n3 # now min <= n1, min <= n2, and min <= n3\nprint \"Their minimum is %d\" % m", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter three integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Their minimum is 33\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.6 A Statement Block within an if Statement\nThis program inputs two integers and then outputs them in increasing order:\n'''\n\nprint \"Enter two integers: \"\nx = int(raw_input())\ny = int(raw_input())\n\nif (x > y):\n temp=x\n x = y\n y = temp\n \n\nprint \"%d <= %d\" %(x,y)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "66\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "44 <= 66\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.7 Using Blocks to Limit Scope\nThis program uses the same name n for three different variables:\n'''\nn=44\nprint \"n = %d\" % n \nif True:\n # scope extends over 4 lines\n print \"Enter an integer: \"\n n = int(raw_input())\n print \"n = %d\" % n \n\nif True:\n print \"n = %d\" % n \n # the n that was declared first\nif True:\n print \"n = %d\" % n \n\nprint \"n = %d\" % n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 44\nEnter an integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 77\nn = 77\nn = 77\nn = 77\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.8 Using Compound Conditions\nThis program has the same effect as the one in Example 3.5 on page 39. This version uses compound\nconditions to find the minimum of three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\nif (n1 <= n2 and n1 <= n3):\n print \"Their minimum is %d\" % n1\n \nif (n2 <= n1 and n2 <= n3):\n print \"Their minimum is %d \" % n2 \nif (n3 <= n1 and n3 <= n2):\n print \"Their minimum is %d\" % n3 \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter three integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Their minimum is 33 \n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.9 User-Friendly Input\nThis program allows the user to input either a 'Y' or a 'y' for 'yes':\n'''\n\nprint \"Are you enrolled (y/n): \"\nans = raw_input()\nif (ans == 'Y' or ans == 'y'):\n print \"You are enrolled.\\n\"\nelse: \n print \"You are not enrolled.\\n\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Are you enrolled (y/n): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "y\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "You are enrolled.\n\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.10 Short-Circuiting\nThis program tests integer divisibility:\n'''\n\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\n\nif (d != 0 and n%d == 0): \n print \"%d divides %d\" %(d,n)\nelse:\n print \"%d does not divide %d\"% (d,n)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two positive integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "6\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "6 does not divide 33\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.11 Another Logical Error\nThis program is erroneous:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\n\nif (n1 >= n2 >= n3):\n print \"max = x\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter three integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.12 Nesting Selection Statements\nThis program has the same effect as the one in Example 3.10 on page 42:\n'''\nprint \"Enter two positive integers: \"\nn = int(raw_input())\nd = int(raw_input())\n\nif (d != 0):\n if (n%d == 0):\n print d,\n print \" divides %d\" % n \n else:\n print \"%d does not divide %d\" %(d,n)\nelse:\n print '%d does not divide %d '%(d,n)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two positive integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "44 does not divide 55\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.13 Using Nested Selection Statements\nThis program has the same effect as those in Example 3.5 on page 39 and Example 3.8 on page 41.\nThis version uses nested if..else statements to find the minimum of three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\nif (n1 < n2):\n if (n1 < n3):\n print \"Their minimum is : %d\" % n1\n else:\n print \"Their minimum is : %d\" % n3\nelse: # n1 >= n2\n if (n2 < n3):\n print \"Their minimum is : %d\" % n2\n else:\n print \"Their minimum is %d\" % n3", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter three integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Their minimum is : 33\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.14 A Guessing Game\nThis program finds a number that the user selects from 1 to 8:\n'''\n\nprint \"Pick a number from 1 to 8.\" \nanswer = int(raw_input())\nprint \"Is it less than 5? (y|n): \"\nanswer = raw_input()\nif (answer == 'y'): # 1 <= n <= 4\n print \"Is it less than 3? (y|n): \"\n answer = raw_input() \n if (answer == 'y'): # 1 <= n <= 2\n print \"Is it less than 2? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 1.\"\n else:\n print \"Your number is 2.\"\n else: # 3 <= n <= 4\n print \"Is it less than 4? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 3.\"\n else:\n print \"Your number is 4.\"\nelse: # 5 <= n <= 8\n print \"Is it less than 7? (y|n): \"\n answer = raw_input()\n if (answer == 'y'): # 5 <= n <= 6\n print \"Is it less than 6? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 5.\"\n else:\n print \"Your number is 6.\" \n else: # 7 <= n <= 8\n print \"Is it less than 8? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 7.\" \n else:\n print \"Your number is 8.\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Pick a number from 1 to 8.\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "6\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Is it less than 5? (y|n): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "n\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Is it less than 7? (y|n): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "y\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Is it less than 6? (y|n): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "n\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Your number is 6.\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.15 Using the else if Construct for Parallel Alternatives\nThis program requests the users language and then prints a greeting in that language:\n'''\nlanguage = raw_input(\"Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): \")\n\nif (language == 'e'): \n print \"Welcome to ProjectEuclid.\"\nelif (language == 'f'):\n print \"Bon jour, ProjectEuclid.\"\nelif (language == 'g'):\n print \"Guten tag, ProjectEuclid.\"\nelif (language == 'i'):\n print \"Bon giorno, ProjectEuclid.\"\nelif (language == 'r'):\n print \"Dobre utre, ProjectEuclid.\"\nelse:\n print \"Sorry; we don't speak your language.\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): i\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Bon giorno, ProjectEuclid.\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.16 Using the else if Construct to Select a Range of Scores\nThis program converts a test score into its equivalent letter grade:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter your test score: 83\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Your grade is a B.\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.17 Using a switch Statement to Select a Range of Scores\nThis program has the same effect as the one in Example 3.16:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n\nprint \"Goodbye.\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter your test score: 83\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Your grade is a B.\nGoodbye.\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.18 An Erroneous Fall-through in a switch Statement\nThis program was intended to have the same effect as the one in Example 3.17. But with\nstatements, the program execution falls through all the case statements it encounters:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n\nprint \"Goodbye.\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter your test score: 83\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Your grade is a B.\nGoodbye.\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.19 Finding the Minimum Again\n'''\n\nprint \"Enter two integers: \"\nm = int(raw_input())\nn = int(raw_input())\nprint min(m,n),\nprint 'is the minimum'", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "33 is the minimum\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb new file mode 100644 index 00000000..d137f53e --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb @@ -0,0 +1,839 @@ +{ + "metadata": { + "name": "ch4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.1 Using a while Loop to Compute a Sum of Consecutive Integers\nThis program computes the sum 1 + 2 + 3 + + n for an input integer n:\n'''\ni=1\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile (i <= n):\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(i,s)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 6 integers is 15\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.2 Using a while Loop to Compute a Sum of Reciprocals\nThis program computes the sum of reciprocals s = 1 + 1/2 + 1/3 + 1/n where n is\ninteger for which n is greater than s\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\ns=0.0\ni=0\nwhile (s < bound):\n i += 1\n s += 1.0/i\n\nprint \"The sum of the first %d reciprocals is %f\" %(i,s)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 83 reciprocals is 5.002068\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.3 Using a while Loop to Repeat a Computation\nThis program prints the square root of each number input by the user. It uses a while loop to allow any\nnumber of computations in a single run of the program:\n'''\nimport math\nprint \"Enter a positive number: \"\nx = float(raw_input())\nwhile (x > 0):\n print \"sqrt(%d) = %f \"%(x,math.sqrt(x))\n print \"Enter another positive number (or 0 to quit): \"\n x = float(raw_input())\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive number: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "sqrt(5) = 2.236068 \nEnter another positive number (or 0 to quit): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "3\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "sqrt(3) = 1.732051 \nEnter another positive number (or 0 to quit): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.4 Using a break Statement to Terminate a Loop\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\ni=1\nprint \"Enter a positive integer: \";\nn = int(raw_input())\ns=0\nwhile(True):\n if (i > n):\n break # terminates the loop immediately\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(n,s)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 5 integers is 15\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.5 The Fibonacci Numbers\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\nwhile (True):\n f2 = f0 + f1\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2\n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.6 Using the exit(0) Function\nThe exit() function provides another way to terminate a loop. When it executes, it terminates the\nprogram itself:\n'''\nimport sys\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\nwhile (True):\n f2 = f0 + f1\n if (f2 > bound):\n sys.exit(0)\n print \", %d\" % f2,\n f0 = f1\n f1 = f2\n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "ename": "SystemExit", + "evalue": "0", + "output_type": "pyerr", + "traceback": [ + "An exception has occurred, use %tb to see the full traceback.\n", + "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8" + }, + { + "output_type": "stream", + "stream": "stderr", + "text": "To exit: use 'exit', 'quit', or Ctrl-D.\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.7 Aborting Infinite Loop\nWithout some termination mechanism, the loop will run forever. To abort its execution after it starts,\npress <Ctrl>+C (i.e., hold the Ctrl key down and press the C key on your keyboard):\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\n# Error : infinite loop !\nwhile (True):\n f2 = f0 + f1\n # By commenting the below if statement, it goes to infinite.\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.7 Aborting Infinite Loop\nWithout some termination mechanism, the loop will run forever. To abort its execution after it starts,\npress <Ctrl>+C (i.e., hold the Ctrl key down and press the C key on your keyboard):\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\n# Error : infinite loop !\nwhile (True):\n f2 = f0 + f1\n # By commenting the below if statement, it goes to infinite.\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \nFibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.8 Using a do..while Loop to Compute a Sum of Consecutive Integers\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\ni=0\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile i<=n:\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(n,s)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 10 integers is 55\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.9 The Factorial Numbers\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Factorial numbers < %d:\\n1, 1\" %bound,\nf=1\ni=1\nwhile f < bound:\n i += 1\n f *= i\n print \", %d\" %f,\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Factorial numbers < 10:\n1, 1 , 2 , 6 , 24\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.10 Using a for Loop to Compute a Sum of Consecutive Integers\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\n\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0;\nfor i in range(0,n+1):\n s += i\nprint \"The sum of the first %d integers is %d\" %(n,s)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 10 integers is 55\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.11 Reusing for Loop Control Variable Names\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nfor i in range(1,n/2): # the scope of this i is this loop\n s += i\n\nfor i in range(n/2,n+1): # the scope of this i is this loop\n s += i\nprint \"The sum of the first %d integers is %d\" % (n,s)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 10 integers is 55\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.12 The Factorial Numbers Again\nThis program has the same effect as the one in Example 4.9 on page 65:\n'''\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\n\nprint \"Factorial numbers that are <= %d:\\n1, 1\" %bound,\nf=1\nfor i in range(2,bound+1):\n f *= i\n print \", %d\" % f,\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Factorial numbers that are <= 10:\n1, 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 , 3628800\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.13 Using a Descending for Loop\nThis program prints the first ten positive integers in reverse order:\n'''\n\nfor i in range(10,0,-1):\n print i,\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "10 9 8 7 6 5 4 3 2 1\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.14 Using a for Loop with a Step Greater than One\nThis program determines whether an input number is prime:\n'''\nprime = True\nprint \"Enter a positive integer: \"\nn = int(raw_input())\nif (n < 2):\n print \"%d is not prime.\" %n\n prime = False\nelif (n < 4):\n print \"%d is prime.\" %n\n prime = False\nelif (n%2 == 0):\n print \"%d = 2* %d\" %(n,n/2)\n prime = False\nelse:\n for d in range(3,n/2+1):\n if (n%d == 0):\n print \"%d = %d * %d\" %(n,d,n/d)\n prime = False\nif prime: \n print \"%d is prime.\"%n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "11\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "11 is prime.\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.15 Using a Sentinel to Control a for Loop\nThis program finds the maximum of a sequence of input numbers:\n'''\nprint \"Enter positive integers (0 to quit): \";\nn = int(raw_input())\nm = n\nwhile n > 0:\n n = int(raw_input())\n if n > m :\n m = n\n\nprint \"max = %d\" % m", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter positive integers (0 to quit): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "19\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "42\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "max = 42\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.16 Using a Loop Invariant to Prove that a for Loop is Correct\nThis program finds the minimum of a sequence of input numbers. It is similar to the program in\nExample 4.15:\n'''\n\nprint \"Enter positive integers (0 to quit): \";\nn = int(raw_input())\nm = n\nwhile n > 0: \n if n < m :\n m = n\n n = int(raw_input())\n\nprint \"min = %d\" % m\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter positive integers (0 to quit): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "19\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "42\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "min = 1\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.17\n'''\n\nm = 95\nn = 11\nwhile m%n > 0:\n print \"%d modulo %d = %d\" %(m,n,m%n)\n m -= 3\n n += 1\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "95 modulo 11 = 7\n92 modulo 12 = 8\n89 modulo 13 = 11\n86 modulo 14 = 2\n83 modulo 15 = 8\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.18 Nesting for Loops\nThis program prints a multiplication table:\n'''\n\nfor x in range(1,13):\n for y in range(1,13):\n print \"%4d\" % (x*y),\n print \"\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": " 1 2 3 4 5 6 7 8 9 10 11 12 \n 2 4 6 8 10 12 14 16 18 20 22 24 \n 3 6 9 12 15 18 21 24 27 30 33 36 \n 4 8 12 16 20 24 28 32 36 40 44 48 \n 5 10 15 20 25 30 35 40 45 50 55 60 \n 6 12 18 24 30 36 42 48 54 60 66 72 \n 7 14 21 28 35 42 49 56 63 70 77 84 \n 8 16 24 32 40 48 56 64 72 80 88 96 \n 9 18 27 36 45 54 63 72 81 90 99 108 \n 10 20 30 40 50 60 70 80 90 100 110 120 \n 11 22 33 44 55 66 77 88 99 110 121 132 \n 12 24 36 48 60 72 84 96 108 120 132 144 \n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.19 Testing a Loop Invariant\n'''\nimport math\n# defines pow() and log()\n\nprint \"Enter a positive integer: \"\nn = int(raw_input())\nd=0 # the discrete binary logarithm of n\np2d=1 # = 2^d\ni = n\nwhile i > 1:\n # INVARIANT: 2^d <= n/i < 2*2^d\n p2d=math.pow(2,d) # = 2^d\n print \"%2d <= %2d\" %(p2d,2*p2d)\n i /= 2\n d += 1\n\np2d=math.pow(2,d) # = 2^d\nprint \"%2d <= %2d < %2d\" %(p2d,n,2*p2d)\nprint \" The discrete binary logarithm of is %d\" % d \nlgn = math.log(n)/math.log(2) # base 2 logarithm\nprint \"The continuous binary logarithm of is %f\" % lgn", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "17\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " 1 <= 2\n 2 <= 4\n 4 <= 8\n 8 <= 16\n16 <= 17 < 32\n The discrete binary logarithm of is 4\nThe continuous binary logarithm of is 4.087463\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.20 Using a break Statement to Terminate a Loop\nThis program has the same effect as the one in Example 4.1 on page 60. It uses a break statement to\ncontrol the loop:\n'''\ni=1\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile (True):\n if (i > n):\n break\n s += i\n i += 1\n\nprint \"The sum of the first %d integers is %d\" %(i,s)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 11 integers is 55\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.21 Controlling Input with a Sentinel\nThis program reads a sequence of positive integers, terminated by 0, and prints their average:\n'''\ncount=0\ns=0\nprint \"Enter positive integers (0 to quit):\" \nwhile True: # \"forever\"\n print \"\\t %d :\" %(count + 1),\n n = int(raw_input())\n if (n <= 0):\n break\n count += 1\n s += n\n\nprint \"The average of those %d positive numbers is \" %count,\nprint float(s)/count\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter positive integers (0 to quit):\n\t 1 :" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "12\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t 2 :" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "32\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t 3 :" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "11\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t 4 :" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " The average of those 3 positive numbers is 18.3333333333\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.22 Using a break Statement with Nested Loops\n'''\n\nfor x in range(1,13):\n for y in range(1,13):\n if y>x:\n break\n else:\n print '%4d' %(x*y),\n print ''\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": " 1 \n 2 4 \n 3 6 9 \n 4 8 12 16 \n 5 10 15 20 25 \n 6 12 18 24 30 36 \n 7 14 21 28 35 42 49 \n 8 16 24 32 40 48 56 64 \n 9 18 27 36 45 54 63 72 81 \n 10 20 30 40 50 60 70 80 90 100 \n 11 22 33 44 55 66 77 88 99 110 121 \n 12 24 36 48 60 72 84 96 108 120 132 144 \n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.23 Using continue and break Statements\nThis little program illustrates the continue and break statements:\n'''\nwhile True:\n n = int(raw_input('Enter int : '))\n if (n%2 == 0):\n continue\n if (n%3 == 0):\n break\n print \"\\tBottom of loop.\\n\"\nprint \"\\tOutside of loop.\\n\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter int : 5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tBottom of loop.\n\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter int : 4\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter int : 6\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter int : 9\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tOutside of loop.\n\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.24 Using a goto Statement to Break Out of a Nest of Loops\nNote : Python has no goto facility.\n'''\nN=5\ndone=False\nfor i in range(N):\n for j in range(N):\n if done:\n break\n for k in range(N):\n if done:\n break\n if (i+j+k>N):\n done = True\n else:\n print i+j+k,\n print \" \",\n print \"* \"\n print \".\" \n done = False\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "0 1 2 3 4 * \n1 2 3 4 5 * \n2 3 4 5 * \n.\n1 2 3 4 5 * \n2 3 4 5 * \n.\n2 3 4 5 * \n.\n3 4 5 * \n.\n4 5 * \n.\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.25 Using a Flag to Break Out of a Nest of Loops\nThis program has the same output as that in Example 4.24:\n'''\nN=5\ndone=False\nfor i in range(N):\n for j in range(N):\n if done:\n break\n for k in range(N):\n if done:\n break\n if (i+j+k>N):\n done = True\n else:\n print i+j+k,\n print \" \",\n print \"* \"\n print \".\" \n done = False\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "0 1 2 3 4 * \n1 2 3 4 5 * \n2 3 4 5 * \n.\n1 2 3 4 5 * \n2 3 4 5 * \n.\n2 3 4 5 * \n.\n3 4 5 * \n.\n4 5 * \n.\n" + } + ], + "prompt_number": 20 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.26 Generating Pseudo-Random Numbers\nThis program uses the rand() function to generate pseudo-random numbers:\n'''\nimport random\n\n# prints pseudo-random numbers:\n\nfor i in range(0,8):\n print random.random()\n\n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "0.702115758628\n0.969460447904\n0.409934401112\n0.700339443791\n0.093528851602\n0.132172955687\n0.0162887279366\n0.943010713478\n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.27 Setting the Seed Interactively\nThis program is the same as the one in Example 4.26 except that it allows the pseudo-random number\ngenerator's seed to be set interactively:\n'''\nimport random\n# prints pseudo-random numbers:\nprint \"Enter seed: \"\nseed = int(raw_input())\nrandom.seed(seed);\nfor i in range(0,8):\n print random.random()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter seed: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "0.62290169489\n0.741786989261\n0.795193565566\n0.942450283777\n0.73989857474\n0.922324996665\n0.0290052282836\n0.465622654378\n" + } + ], + "prompt_number": 22 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.28 Setting the Seed from the System Clock\nThis program is the same as the one in Example 4.27 except that it sets the pseudo-random number\ngenerator's seed from the system clock.\n'''\nimport random\nfor i in range(0,8):\n print random.random()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "0.943356716998\n0.648974553137\n0.900900491751\n0.113205964653\n0.469069047782\n0.24657283262\n0.543760859236\n0.573941187928\n" + } + ], + "prompt_number": 23 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.29 Generating Pseudo-Random Numbers in Given Range\nThis program is the same as the one in Example 4.28 except that the pseudo-random numbers that it\ngenerates are restricted to given range:\n'''\nimport random\nprint \"Enter minimum and maximum: \"\nm = int(raw_input())\nn = int(raw_input())\n# lowest and highest numbers\nr = n - m + 1\n# number of numbers in range\nfor i in range(0,20):\n j = int(random.random()*100 % r + m)\n print j,\n print \" \",\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter minimum and maximum: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "15\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "6 15 10 8 15 9 7 7 11 6 5 15 14 15 15 15 11 13 14 6 \n" + } + ], + "prompt_number": 24 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file 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 diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch6.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch6.ipynb new file mode 100644 index 00000000..a355a8d2 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch6.ipynb @@ -0,0 +1,648 @@ +{ + "metadata": { + "name": "ch6" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.1 Using Direct Access on Arrays\n'''\n\na = [0, 0, 0]\na[2] = 55.55\na[0] = 11.11\na[1] = 33.33\nprint \"a[0] = \" , a[0] \nprint \"a[1] = \" , a[1] \nprint \"a[2] = \" , a[2] \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a[0] = 11.11\na[1] = 33.33\na[2] = 55.55\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.2 Printing a Sequence in Order\n'''\n\nSIZE=5 # defines the size N for 5 elements\na = []\n# declares the array's elements as type double\nprint \"Enter \" , SIZE , \" numbers:\\t\"\nfor i in range(SIZE):\n a.append(float(raw_input()))\n \nprint \"In reverse order: \"\nfor i in range(SIZE-1,-1,-1):\n print \"\\t\" , a[i]\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 5 numbers:\t\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "3.3\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4.4\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5.5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "In reverse order: \n\t5.5\n\t4.4\n\t3.3\n\t2.0\n\t1.0\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.3 Initializing an Array\nThis program initializes the array a and then prints its values:\n'''\n\na = [ 22.2, 44.4, 66.6 ]\n\nsize = len(a)\nfor i in range(size):\n print \"\\ta[\" , i , \"] = \" , a[i]\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "\ta[ 0 ] = 22.2\n\ta[ 1 ] = 44.4\n\ta[ 2 ] = 66.6\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.4 Initializing an Array with Trailing Zeros\n'''\n\na = [ 22.2, 44.4, 66.6 , 0 ,0,0,0]\nsize = len(a)\nfor i in range(size):\n print \"\\ta[\" , i , \"] = \" , a[i] \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "\ta[ 0 ] = 22.2\n\ta[ 1 ] = 44.4\n\ta[ 2 ] = 66.6\n\ta[ 3 ] = 0\n\ta[ 4 ] = 0\n\ta[ 5 ] = 0\n\ta[ 6 ] = 0\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.5 An Uninitialized Array\nNOte : In PYthon , we have to initialize array. Without initializing it, it doesn't work.\nArray size is automatically incremented when we push data into it.\n'''\nimport numpy\nSIZE = 4\na = numpy.zeros(4)\n# declares the array's elements as type float\nfor i in range(SIZE):\n print \"\\ta[\" , i , \"] = \" , a[i]\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "\ta[ 0 ] = 0.0\n\ta[ 1 ] = 0.0\n\ta[ 2 ] = 0.0\n\ta[ 3 ] = 0.0\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.6 Allowing an Array Index to Exceed its Bounds\nNOte : Python gives error when this kind of error occurs. so It wont print garbage values after bound exceeds.\n'''\nSIZE=4\na = [ 33.3, 44.4, 55.5, 66.6 ]\nfor i in range(7): # ERROR: index is out of bounds!\n print \"\\ta[\" , i , \"] = \" , a[i] \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-6-248fbb50a5b0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m \u001b[1;36m33.3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m44.4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m55.5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m66.6\u001b[0m \u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# ERROR: index is out of bounds!\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"\\ta[\"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;34m\"] = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mIndexError\u001b[0m: list index out of range" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\ta[ 0 ] = 33.3\n\ta[ 1 ] = 44.4\n\ta[ 2 ] = 55.5\n\ta[ 3 ] = 66.6\n\ta[ 4 ] = " + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.7 Causing Side Effects\nThis program inadvertently changes the value of a variable when it accesses a nonexistent element of\nan array:\nNote : Python automatically increase size of array/list. so it wont give error and output would be differ than c gives.\n'''\n\na = [ 22.2, 44.4, 66.6 ]\nx=11.1\nprint \"x = \" , x \na.append(88.8) # ERROR: index is out of bounds!\nprint \"x = \" , x \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": " x = 11.1\nx = 11.1\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.8 Causing Unhandled Exceptions\nThis program crashes because the array index gets too big:\n'''\na = [ 22.2, 44.4, 66.6 ]\nx=11.1\nprint \"x = \" , x \na[3333] = 88.8 # ERROR: index is out of bounds!\nprint \"x = \" , x \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "list assignment index out of range", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-8-63b6c8e60294>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m11.1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"x = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3333\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m88.8\u001b[0m \u001b[1;31m# ERROR: index is out of bounds!\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 9\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"x = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mIndexError\u001b[0m: list assignment index out of range" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 11.1\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.9 Passing an Array to a Function that Returns its Sum\n'''\n\ndef sum_(a):\n s = 0\n for i in a:\n s += i\n return s\n \na = [ 11, 33, 55, 77 ]\nprint \"sum(a) = \" , sum_(a) \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "sum(a) = 176\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.10 Input and Output Functions for an Array\nThis program uses a read() function to input values into the array a interactively. Then it uses a\nprint() function to print the array:\n'''\ndef read(a):\n print \"Enter integers. Terminate with 0:\\n\"\n n = 1\n while True:\n n = int(raw_input(\"a[\" + str(len(a)) + \"]: \"))\n if n == 0:\n break\n a.append(n)\n \n\ndef print_(a):\n for i in a:\n print i ,\n\n\na = []\nread(a)\nprint \"The array has \" , len(a) , \" elements: \"\nprint_(a)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter integers. Terminate with 0:\n\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "a[0]: 11\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "a[1]: 22\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "a[2]: 33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "a[3]: 44\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "a[4]: 0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The array has 4 elements: \n11 22 33 44\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.11 Printing the Memory Location of an Array\nThis program prints the value of the address stored in an array name.\n'''\nimport sys\na = [ 22, 44, 66, 88 ]\nprint \"a = \" , id(a) # the address of a[0]\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a = 169156908\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.12 The Linear Search\nThis program tests a function that implements the Linear Search algorithm:\n'''\ndef index(x,a,n):\n for i in range(len(a)):\n if (a[i] == x):\n return i\n return n # x not found\n\na = [ 22, 44, 66, 88, 44, 66, 55 ]\nprint \"index(44,a,7) = \" , index(44,a,7)\nprint \"index(50,a,7) = \" , index(50,a,7) \n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "index(44,a,7) = 1\nindex(50,a,7) = 7\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.13 The Bubble Sort\nThis program tests a function that implements the Bubble Sort algorithm.\n'''\ndef sort(a,n):\n # bubble sort:\n n = len(a)\n for i in range(n):\n # bubble up max{a[0..n-i]}:\n for j in range(n-i-1):\n if (a[j] > a[j+1]):\n a[j],a[j+1] = a[j+1],a[j]\n\ndef print_(a):\n for i in range(len(a)):\n print a[i],\n print ''\n \na = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\n\nprint_(a)\nsort(a,8)\nprint_(a)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7 \n22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.14 The Binary Search Algorithm\n'''\n\ndef index(x,a,n):\n # PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\n # binary search:\n lo=0\n hi=n-1\n while (lo <= hi):\n i = (lo + hi)/2 # the average of lo and hi\n if (a[i] == x):\n return i\n if (a[i] < x):\n lo = i+1 # continue search in a[i+1..hi]\n else:\n hi = i-1 # continue search in a[lo..i-1]\n return n # x was not found in a[0..n-1]\n\na = [ 22, 33, 44, 55, 66, 77, 88 ]\nprint \"index(44,a,7) = \" , index(44,a,7)\nprint \"index(60,a,7) = \" , index(60,a,7) \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "index(44,a,7) = 2\nindex(60,a,7) = 7\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.15 Determining whether an Array is Sorted\nThis program tests a boolean function that determines whether a given array is nondecreasing.\n'''\n\ndef isNondecreasing(a,n):\n # returns true iff a[0] <= a[1] <= ... <= a[n-1]:\n for i in range(1,n):\n if (a[i]<a[i-1]):\n return False\n return True\n\na = [ 22, 44, 66, 88, 44, 66, 55 ]\nprint \"isNondecreasing(a,4) = \" , isNondecreasing(a,4)\nprint \"isNondecreasing(a,7) = \" , isNondecreasing(a,7)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "isNondecreasing(a,4) = True\nisNondecreasing(a,7) = False\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.16 Using the assert() Function to Enforce a Precondition\nThis program tests an improved version of the search() function from Example 6.14. This version\nuses the isNondecreasing() function from Example 6.15 to determine whether the array is sorted. It\npasses the resulting boolean return value to the assert() function so that the search will not be carried\nout if the array is not sorted:\n'''\n\ndef isNondecreasing(a,n):\n # returns true iff a[0] <= a[1] <= ... <= a[n-1]:\n for i in range(1,n):\n if (a[i]<a[i-1]):\n return False\n return True\n\n\ndef index(x,a,n):\n # PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\n # binary search:\n assert(isNondecreasing(a,n)) \n \n lo=0\n hi=n-1\n while (lo <= hi):\n i = (lo + hi)/2\n if (a[i] == x):\n return i\n if (a[i] < x):\n lo = i+1 # continue search in a[i+1..hi]\n else:\n hi = i-1 # continue search in a[lo..i-1]\n return n # x was not found in a[0..n-1]\n\na = [ 22, 33, 44, 55, 66, 77, 88, 60 ]\nprint \"index(44,a,7) = \" , index(44,a,7) \nprint \"index(44,a,8.py) = \" , index(44,a,8) \nprint \"index(60,a,7) = \" , index(60,a,8)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "AssertionError", + "evalue": "", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-18-3a784dd6b25e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 34\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m \u001b[1;36m22\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m33\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m44\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m55\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m66\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m77\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m88\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m60\u001b[0m \u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 35\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"index(44,a,7) = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m44\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 36\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"index(44,a,7) = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m44\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 37\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"index(60,a,7) = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m60\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m<ipython-input-18-3a784dd6b25e>\u001b[0m in \u001b[0;36mindex\u001b[1;34m(x, a, n)\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[1;31m# PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;31m# binary search:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0misNondecreasing\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 21\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 22\u001b[0m \u001b[0mlo\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mAssertionError\u001b[0m: " + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "index(44,a,7) = 2\nindex(44,a,7) = " + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.17 Enumerating the Days of the Week\nThis program defines an array high[] of seven floats, representing the high temperatures for\nthe seven days of a week: 138\n'''\nDay = [ 0, 1, 2, 3, 4, 5, 6 ]\nhigh = [ 88.3, 95.0, 91.2, 89.9, 91.4, 92.5, 86.7]\n\nfor i in Day:\n print \"The high temperature for day \" , i , \" was \" , high[i] \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": " The high temperature for day 0 was 88.3\nThe high temperature for day 1 was 95.0\nThe high temperature for day 2 was 91.2\nThe high temperature for day 3 was 89.9\nThe high temperature for day 4 was 91.4\nThe high temperature for day 5 was 92.5\nThe high temperature for day 6 was 86.7\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.18 The Bubble Sort Again\n'''\n\ndef sort(a,n):\n a.sort()\n\ndef print_(a,n):\n for i in a:\n print i,\n print ''\na = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\nprint_(a,8);\nsort(a,8)\nprint_(a,8)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7 \n22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n" + } + ], + "prompt_number": 20 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.19 Reading and Printing a Two-Dimensional Array\nThis program shows how a two-dimensional array can be processed:\n'''\n\ndef read(a):\n print \"Enter 15 integers, 5 per row:\\n\"\n for i in range(3):\n ar = []\n print \"Row \" , i , \": \",\n for j in range(5):\n ar.append(int(raw_input()))\n a.append(ar)\n\ndef print_(a):\n for i in range(3):\n for j in range(5):\n print a[i][j],\n print ''\na = []\nread(a)\nprint_(a)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 15 integers, 5 per row:\n\nRow 0 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "11\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " Row 1 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "60\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "50\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "30\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "90\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "70\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " Row 2 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "85\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "25\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "45\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "45\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " 44 77 33 11 44 \n60 50 30 90 70 \n85 25 45 45 55 \n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.20 Processing a Two-Dimensional Array of Quiz Scores\n'''\ndef read(score):\n for s in range(3):\n print \"Student \" , s , \": \",\n st = []\n for q in range(5):\n st.append(int(raw_input()))\n score.append(st)\n\ndef printQuizAverages(score):\n for s in range(3):\n sm = 0\n for q in range(5):\n sm += score[s][q]\n print \"\\tStudent \" , s , \": \" , sm/5.0\n\ndef printClassAverages(score):\n for q in range(5):\n sm = 0\n for s in range(3):\n sm += score[s][q]\n print \"\\tQuiz \" , q , \": \" , sm/3.0\n\n\n\nNUM_STUDENTS = 3\nNUM_QUIZZES = 5\n\n\nscore = []\nprint \"Enter \" , NUM_QUIZZES , \" scores for each student: \"\nread(score)\nprint \"The quiz averages are:\"\nprintQuizAverages(score)\nprint \"The class averages are: \"\nprintClassAverages(score)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 5 scores for each student: \nStudent 0 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "7\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " Student 1 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " Student 2 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "6\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "7\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " The quiz averages are:\n\tStudent 0 : 8.2\n\tStudent 1 : 8.8\n\tStudent 2 : 7.0\nThe class averages are: \n\tQuiz 0 : 7.33333333333\n\tQuiz 1 : 7.33333333333\n\tQuiz 2 : 8.33333333333\n\tQuiz 3 : 8.33333333333\n\tQuiz 4 : 8.66666666667\n" + } + ], + "prompt_number": 24 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.21 Processing a Three-Dimensional Array\nThis program simply counts the number of zeros in a three-dimensional array:\n'''\n\ndef numZeros(a,n1,n2,n3):\n count = 0\n for i in range(n1):\n for j in range(n2):\n for k in range(n3):\n if (a[i][j][k] == 0):\n count += 1\n return count\n\n\na = [ [ [5,0,2], [0,0,9], [4,1,0], [7,7,7] ],[ [3,0,0], [8,5,0], [0,0,0], [2,0,9] ]]\nprint \"This array has \" , numZeros(a,2,4,3) , \" zeros\"\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "This array has 11 zeros\n" + } + ], + "prompt_number": 25 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb new file mode 100644 index 00000000..f480fc86 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb @@ -0,0 +1,329 @@ +{ + "metadata": { + "name": "ch7" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.1 Printing Pointer Values\n'''\n\nn=44\nprint \"n = \" , n \n# prints the value of n\nprint \"&n = \" , hex(id(n)) # prints the address of n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 44\n&n = 0x8fc0eec\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.2\nNote : Python doesn't support reference/pointer variable. But can be achieved by using mutable datatypes i.e. list.\n'''\n\n\nn = [44]\nrn=n # r is a synonym for n\nprint \"n = \" , n , \", rn = \" , rn \nn[0] -= 1\nprint \"n = \" , n , \", rn = \" , rn \nrn[0] *= 2\nprint \"n = \" , n , \", rn = \" , rn \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , rn = [44]\nn = [43] , rn = [43]\nn = [86] , rn = [86]\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.3 References Are Not Separate Variables\n'''\n\n\nn = [44]\nrn=n # r is a synonym for n\nprint \"&n = \" , hex(id(n)) , \", rn = \" , hex(id(rn ))\nrn2 = n\nrn3 = rn\nprint \"&rn2 = \" , hex(id(rn2)) , \", rn = \" , hex(id(rn ))\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "&n = 0x9c6228c , rn = 0x9c6228c\n&rn2 = 0x9c6228c , rn = 0x9c6228c\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.4 Using Pointer Variables\n\n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"pn = \" , hex(id(pn)) , \", &pn = \" , hex(id(hex(id(pn))))\n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , &n = 0x9c624ec\npn = 0x9c624ec , &pn = 0x9c6aa60\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.5\n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , &n = 0x9c58d6c\n\tpn = 0x9c58d6c ,\n &pn = 0x9c6ab20\n*pn = [44]\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.6 \n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\nppn = pn\n\nprint \" ppn = \" , hex(id(hex(id(ppn)))) \nprint \" &ppn = \" , hex(id(hex(id(hex(id(ppn))))))\nprint \" *ppn = \" , hex(id(ppn)) \nprint \"**ppn = \" , ppn \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , &n = 0x9bf05ac\n\tpn = 0x9bf05ac ,\n &pn = 0x9c58160\n*pn = [44]\n ppn = 0x9c58680\n &ppn = 0x9c58160\n *ppn = 0x9bf05ac\n**ppn = [44]\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.7\n'''\n\nn = [44]\nprint \"n = \" , n , \"\\n &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\nnn = pn\nprint \" ppn = \" , hex(id(nn))\nprint \" &ppn = \" , hex(id(hex(id(nn))))\nrpn = pn\nprint \" ppn = \" , hex(id(rpn))\nprint \" &ppn = \" , hex(id(hex(id(rpn))))\n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] \n &n = 0x9bf60ec\n\tpn = 0x9bf60ec ,\n &pn = 0x9bf0e40\n*pn = [44]\n ppn = 0x9bf60ec\n &ppn = 0x9bf0e40\n ppn = 0x9bf60ec\n &ppn = 0x9bf0f20\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.8\n'''\n\ndef max_(m,n):\n if m>n:\n return m\n else:\n return n\n\nm = 44\nn = 22\nprint m , \", \" , n , \", \" , max_(m,n)\nm = max_(m,n) \nm = 55\n# changes the value of m from 44 to 55\nprint m , \", \" , n , \", \" , max_(m,n) \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "44 , 22 , 44\n55 , 22 , 55\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.9 \n'''\n\nv = []\nfor k in range(1,5):\n v.append(1.0/k)\n\nfor i in range(4):\n print \"v[\" , i , \"] = \" , v[i]\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "v[ 0 ] = 1.0\nv[ 1 ] = 0.5\nv[ 2 ] = 0.333333333333\nv[ 3 ] = 0.25\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.10 \n'''\nimport sys\na = [22, 33, 44]\n\nprint \"a = \" , hex(id(a))\nprint \"sizeof(int) = \" , sys.getsizeof(1) \ns = 0\nfor i in a:\n s += i\n print \"\\t i = \" , hex(id(i)),\n print \"\\t *i = \" , i,\n print \"\\t sum = \" , s\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a = 0x9bf688c\nsizeof(int) = 12\n\t i = 0x8fc0ff4 \t *i = 22 \t sum = 22\n\t i = 0x8fc0f70 \t *i = 33 \t sum = 55\n\t i = 0x8fc0eec \t *i = 44 \t sum = 99\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.11 Examining the Addresses of Array Elements\n'''\n\na = [22, 33, 44, 55, 66]\nprint \"a = \" , hex(id(a)) , \", *a = \" , a[0] \nfor p in a:\n print \"p = \" , hex(id(p)) , \", *p = \" , p \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a = 0x9c6526c , *a = 22\np = 0x8fc0ff4 , *p = 22\np = 0x8fc0f70 , *p = 33\np = 0x8fc0eec , *p = 44\np = 0x8fc0e68 , *p = 55\np = 0x8fc0de4 , *p = 66\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.12 Pattern Matching\n'''\n\ndef loc(a1,a2,n1,n2):\n p = []\n for element in a2:\n if element in a1:\n p.append(element)\n return p\n\na1 = [11, 11, 11, 11, 11, 22, 33, 44, 55]\na2 = [11, 11, 11, 22, 33]\nprint \"Array a1 begins at location\\t\" , hex(id(a1 ))\nprint \"Array a2 begins at location\\t\" , hex(id(a2)) \np = loc(a1, a2, 9, 5)\nif (p):\n print \"Array a2 found at location\\t\" , hex(id(p))\n for i in range(len(p)):\n print \"\\t\" , hex(id(p[i])) , \": \" , p[i], \"\\t\" , hex(id(a2[i])) , \": \" , a2[i] \nelse:\n print \"Not found.\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Array a1 begins at location\t0x9bea56c\nArray a2 begins at location\t0x9bea62c\nArray a2 found at location\t0x9bea6cc\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc0ff4 : 22 \t0x8fc0ff4 : 22\n\t0x8fc0f70 : 33 \t0x8fc0f70 : 33\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.15 Using Dynamic Arrays\n'''\n\ndef get(a):\n print \"Enter number of items: \"\n n = int(raw_input())\n print \"Enter \" , n , \" items, one per line:\"\n for i in range(n):\n print \"\\t\" , i+1 , \": \",\n a.append(float(raw_input()))\n\ndef print_(a):\n for i in range(len(a)):\n print a[i] ,\n print ''\n\na = []\nget(a)\nprint_(a)\na = []\nget(a)\nprint_(a)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter number of items: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 4 items, one per line:\n\t1 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44.4\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t2 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77.7\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t3 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "22.2\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t4 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "88.8\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " 44.4 77.7 22.2 88.8 \nEnter number of items: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 2 items, one per line:\n\t1 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "3.33\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t2 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9.99\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " 3.33 9.99 \n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.17 Indirect Bubble Sort\n'''\n\ndef sort(p, n):\n for i in range(1,n):\n for j in range(n-i):\n if (p[j] > p[j+1]):\n p[j],p[j+1] = p[j+1],p[j]\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.18 The Sum of a Function\n'''\n\ndef sum_(k,n):\n # returns the sum f(0) + f(1) + f(2) + . . . + f(n-1):\n s = 0\n for i in range(1,n+1):\n s += k(i)\n return s\n\ndef square(k):\n return k*k\n\ndef cube(k):\n return k*k*k\n\n\nprint sum_(square,4) # 1 + 4 + 9 + 16\nprint sum_(cube,4) # 1 + 8 + 27 + 64\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "30\n100\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch8.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch8.ipynb new file mode 100644 index 00000000..033f6e3f --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch8.ipynb @@ -0,0 +1,492 @@ +{ + "metadata": { + "name": "ch8" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "\"\"\"\nEXAMPLE 8.1\n\"\"\"\n\nn= [44] # n holds the int 44\nprint \"int n=44; // n holds the int 44:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\npn = n \nprint \"int* pn=&n; // pn holds the address of n:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\nprint \"\\t\\t pn = \" , hex(id(pn)) \nprint \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\nprint \"\\t\\t *pn = \" , pn\n\npn[0] = 77 # changes the value of n to 77\nprint \"*pn = 77; // changes the value of n to 77:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\nprint \"\\t\\t pn = \" , hex(id(pn)) \nprint \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\nprint \"\\t\\t *pn = \" , pn\n\nq = n \nprint \"int* q=&n; // q also holds the address of n:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\nprint \"\\t\\t pn = \" , hex(id(pn)) \nprint \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\nprint \"\\t\\t *pn = \" , pn\nprint \"\\t\\t q = \" , hex(id(q))\nprint \"\\t\\t &q = \" , hex(id(hex(id(hex(id(pn))))))\nprint \"\\t\\t *q = \" , q \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "int n=44; // n holds the int 44:\n\n\t\t n = [44]\n\t\t &n = 0x9bfb92c\nint* pn=&n; // pn holds the address of n:\n\n\t\t n = [44]\n\t\t &n = 0x9bfb92c\n\t\t pn = 0x9bfb92c\n\t\t &pn = 0x9bf5aa0\n\t\t *pn = [44]\n*pn = 77; // changes the value of n to 77:\n\n\t\t n = [77]\n\t\t &n = 0x9bfb92c\n\t\t pn = 0x9bfb92c\n\t\t &pn = 0x9c6a760\n\t\t *pn = [77]\nint* q=&n; // q also holds the address of n:\n\n\t\t n = [77]\n\t\t &n = 0x9bfb92c\n\t\t pn = 0x9bfb92c\n\t\t &pn = 0x9bf5c80\n\t\t *pn = [77]\n\t\t q = 0x9bfb92c\n\t\t &q = 0x9c6a760\n\t\t *q = [77]\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.2\nNote : Python does not append '\\0' at the end of string.\n'''\n\ns = \"ABCD\"\nfor i in range(4):\n print \"s[\" , i , \"] = '\" , s[i] , \"'\\n\";\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "s[ 0 ] = ' A '\n\ns[ 1 ] = ' B '\n\ns[ 2 ] = ' C '\n\ns[ 3 ] = ' D '\n\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.3\n'''\n\nwhile True:\n word = raw_input()\n if len(word) < 2:\n break\n l = word.split(' ')\n for i in l:\n print '\\t\"' , i , '\"'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Today's date is March 12, 2000.\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t\" Today's \"\n\t\" date \"\n\t\" is \"\n\t\" March \"\n\t\" 12, \"\n\t\" 2000. \"\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Tomorrow is Monday.\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t\" Tomorrow \"\n\t\" is \"\n\t\" Monday. \"\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.4\nThis program echoes the input, line by line:\n'''\n\nwhile True:\n line = raw_input()\n if len(line) < 2:\n break\n print \"\\t[\" , line , \"]\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t[ Once upon a midnight dreary, while I pondered, weak and weary, ]\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Over a many quaint and curious volume of forgotten lore,\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t[ Over a many quaint and curious volume of forgotten lore, ]\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.5\n'''\n\n\nwhile True:\n word = raw_input()\n if len(word) < 2:\n break\n l = word.split(',')\n for i in range(len(l)-1):\n print '\\t[' , l[i] , ']'\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t[ Once upon a midnight dreary ]\n\t[ while I pondered ]\n\t[ weak and weary ]\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Over a many quaint and curious volume of forgotten lore,\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t[ Over a many quaint and curious volume of forgotten lore ]\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.6 \nThis program counts the number of occurrences of the letter 'e' in the input stream. \n'''\n\ncount = 0\nwhile True:\n a = raw_input()\n if len(a) < 1:\n break\n for ch in a:\n if (ch == 'e'): count+=1\n \nprint count , \" e's were counted.\\n\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Over many a quaint and curious volume of forgotten lore,\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "11 e's were counted.\n\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.7 \nThis program echoes the input stream, capitalizing each word:\n'''\n\nwhile True:\n a = raw_input()\n if len(a) < 1:\n break\n print a.title()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Fourscore and seven years ago our fathers\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Fourscore And Seven Years Ago Our Fathers\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "brought forth upon this continent a new nation,\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Brought Forth Upon This Continent A New Nation,\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.8\nThis tests a function that extracts the integers from the input stream:\n'''\n\na = raw_input()\nl = a.split(' ')\nnos = []\nfor i in l:\n try:\n i = int(i)\n nos.append(i)\n except:\n continue\nm = nos[0]\nn = nos[1] \nprint m , \" + \" , n , \" = \" , m+n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "what is 305 plus 9416 ?\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "305 + 9416 = 9721\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.10 An Array of Strings\n'''\n\nname = []\ncount=0\n\nprint \"Enter at most 4 names with at most 19 characters:\\n\";\nwhile (True):\n n = raw_input()\n if len(n) < 1:\n break\n name.append(n)\n count += 1\n \nprint \"The names are:\\n\"\nfor i in range(count):\n print \"\\t\" , i , \". [\" , name[i] , \"]\" \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter at most 4 names with at most 19 characters:\n\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "George Washington\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "John Adams\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Thomas Jefferson\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The names are:\n\n\t0 . [ George Washington ]\n\t1 . [ John Adams ]\n\t2 . [ Thomas Jefferson ]\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.11 A String Array\n'''\n\nname = []\ncount=0\n\nprint \"Enter at most 4 names with at most 19 characters:\\n\";\nwhile (True):\n n = raw_input()\n if len(n) < 1:\n break\n name.append(n)\n count += 1\n \nprint \"The names are:\\n\"\nfor i in range(count):\n print \"\\t\" , i , \". [\" , name[i] , \"]\" ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter at most 4 names with at most 19 characters:\n\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "George Washington\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "John Adams\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Thomas Jefferson\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The names are:\n\n\t0 . [ George Washington ]\n\t1 . [ John Adams ]\n\t2 . [ Thomas Jefferson ]\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.12 Initializing a String Array\nThis program is nearly equivalent to those in the previous two examples. It initializes the array/list\nname and then prints its contents:\n'''\n\nname = [ \"George Washington\", \"John Adams\", \"Thomas Jefferson\"]\nprint \"The names are:\\n\"\nfor i in range(3):\n print \"\\t\" , i , \". [\" , name[i] , \"]\"\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The names are:\n\n\t0 . [ George Washington ]\n\t1 . [ John Adams ]\n\t2 . [ Thomas Jefferson ]\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.13 The len() Function\n'''\n\ns = \"ABCDEFG\"\nprint \"len(\" , s , \") = \" , len(s) \nprint \"len(\\\"\\\") = \" , len(\"\")\nprint \"Enter string: \"\nb = raw_input()\nprint \"len(\" , b , \") = \" , len(b)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "len( ABCDEFG ) = 7\nlen(\"\") = 0\nEnter string: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "hello how are you !!!\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "len( hello how are you !!! ) = 21\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.14 \ngives index of substring.\n'''\ns = \"The Mississippi is a long river.\"\nprint 's = \"' , s , '\"'\np = s.find(' ')\nprint \"find(s, ' ') points to s[\" , p , \"].\"\np = s.find('s')\nprint \"find(s, 's') points to s[\" , p , \"].\"\np = s.rfind('s')\nprint \"reverse find(s, 's') points to s[\" , p , \"].\"\np = s.find(\"is\")\nprint \"strstr(s, \\\"is\\\") points to s[\" , p , \"].\"\np = s.find(\"isi\")\nif p== -1:\n print 's.find(\"isi\") returns NULL'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "s = \" The Mississippi is a long river. \"\nfind(s, ' ') points to s[ 3 ].\nfind(s, 's') points to s[ 6 ].\nreverse find(s, 's') points to s[ 17 ].\nstrstr(s, \"is\") points to s[ 5 ].\ns.find(\"isi\") returns NULL\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.15 string copy\n'''\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before strcpy(s1,s2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 = s2\nprint \"After strcpy(s1,s2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Before strcpy(s1,s2):\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter strcpy(s1,s2):\n\n\ts1 = [ XYZ ], length = 3\n\ts2 = [ XYZ ], length = 3\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.16 string copy\n'''\n\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before strcpy(s1,s2,2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 = s2[:2] + s1[2:]\nprint \"After strcpy(s1,s2,2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Before strcpy(s1,s2,2):\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter strcpy(s1,s2,2):\n\n\ts1 = [ XYCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.17 The String Concatenation\n'''\n\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before string concatination :\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 += s2\nprint \"After string concatination :\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Before string concatination :\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter string concatination :\n\ts1 = [ ABCDEFGXYZ ], length = 10\n\ts2 = [ XYZ ], length = 3\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.18 The Second String Concatenation (no. of characters)\n'''\n\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before string concatination :\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 += s2[:2]\nprint \"After string concatination :\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Before string concatination :\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter string concatination :\n\ts1 = [ ABCDEFGXY ], length = 9\n\ts2 = [ XYZ ], length = 3\n" + } + ], + "prompt_number": 20 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.19 The String Tokenize \n'''\ns = \"Today's date is March 12, 2000.\"\n\nprint \"The string is: [\" , s , \"] \\nIts tokens are: \"\np = s.split(\" \")\n\nfor i in p:\n print \"\\t[\" , i , \"] \"\n\nprint \"Now the string is: [\" , p[0] , \"] \";\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The string is: [ Today's date is March 12, 2000. ] \nIts tokens are: \n\t[ Today's ] \n\t[ date ] \n\t[ is ] \n\t[ March ] \n\t[ 12, ] \n\t[ 2000. ] \nNow the string is: [ Today's ] \n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.20 The strpbrk() Function\nC provides inbuilt strpbrk(), Here i have written mine.\n'''\n\ndef strpbrk(s,s1):\n found = []\n for i in range(len(s1)):\n if s1[i] in s:\n index = s.find(s1[i])\n found.append(index)\n if found:\n return min(found)\n return None\n \n\ns = \"The Mississippi is a long river.\"\nprint 's = \"' , s , '\"'\np = strpbrk(s, \"nopqr\")\nprint 'strpbrk(s, \"nopqr\") points to s[' , p , \"].\"\np = strpbrk(s, \"NOPQR\")\nif (p == None):\n print 'strpbrk(s, \"NOPQR\") returns NULL.\\n'", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "s = \" The Mississippi is a long river. \"\nstrpbrk(s, \"nopqr\") points to s[ 12 ].\nstrpbrk(s, \"NOPQR\") returns NULL.\n\n" + } + ], + "prompt_number": 22 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch9.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch9.ipynb new file mode 100644 index 00000000..fabb9a56 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch9.ipynb @@ -0,0 +1,232 @@ +{ + "metadata": { + "name": "ch9" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.2 Using the Extraction Operation to Control a Loop\n'''\n\n\nwhile True:\n try:\n n = int(raw_input())\n print \"n = \" , n \n except:\n break", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "46\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 46\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "22\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 22\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 44\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "66\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 66\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "88\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 88\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33,\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.4 Inputting Strings with the raw_input() Function\nThis program shows how to read text data line-by-line into an array \n'''\n\nking = [] # defines king to be an array \nn=0\nwhile True:\n name = raw_input()\n if len(name) < 1:\n break\n king.append(name)\n n += 1\n# now n == the number of names read\nfor i in range(n):\n print '\\t' , i+1 , \". \" , king[i] ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Kenneth II (971-995)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Constantine III (995-997)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Kenneth III (997-1005)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Malcolm II (1005-1034)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Duncan I (1034-1040)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Macbeth (1040-1057)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Lulach (1057-1058)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Malcolm III (1058-1093)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t1 . Kenneth II (971-995)\n\t2 . Constantine III (995-997)\n\t3 . Kenneth III (997-1005)\n\t4 . Malcolm II (1005-1034)\n\t5 . Duncan I (1034-1040)\n\t6 . Macbeth (1040-1057)\n\t7 . Lulach (1057-1058)\n\t8 . Malcolm III (1058-1093)\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.6 Capitalizing All the Words in a Text File\nHere is a complete program that reads words from the external file named input.txt, capitalizes\nthem, and then writes them to the external file named output.txt:\nNote : Working good , you need input.txt in same directory.\n'''\n\ninfile = open(\"input.txt\",\"r\")\noutfile = open(\"output.txt\",\"w\")\n\nfor i in infile:\n s = i.title()\n outfile.write(s)\n\ninfile.close()\noutfile.close()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "IOError", + "evalue": "[Errno 2] No such file or directory: 'input.txt'", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIOError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-3-60ee428ecf4c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m '''\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0minfile\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"input.txt\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[0moutfile\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"output.txt\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"w\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'input.txt'" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.7 Merging Two Sorted Data Files\n'''\n\nfin1 = open(\"north.dat\",\"r\")\nfin2 = open(\"south.dat\",\"r\")\nfout = open(\"combined.dat\",\"w\")\n\nfile1 = []\nfile2 = []\nfor i in fin1:\n try:\n s = i.split(\" \")\n for j in s:\n file1.append(int(j))\n except:\n continue\n \nfor i in fin2:\n try:\n s = i.split(\" \")\n for j in s:\n file2.append(int(j))\n except:\n continue\n\n\nfor i in sorted(file1 + file2):\n fout.write(str(i) + \" \")\n\nfin1.close()\nfin2.close()\nfout.close()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "IOError", + "evalue": "[Errno 2] No such file or directory: 'north.dat'", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIOError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-4-e14718f30215>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 3\u001b[0m '''\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mfin1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"north.dat\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[0mfin2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"south.dat\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mfout\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"combined.dat\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"w\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'north.dat'" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.8 Using an Output String Stream\nThis program creates four objects: a character string s,\nan integer n, a floating-point number x, and an output string stream oss:\n'''\n\ndef print_(oss):\n print 'oss.str() = \"' , str(oss) , '\"'\n\ns=\"ABCDEFG\"\nn=33\nx=2.718\nl = ''\nprint_(l)\nl += s\nprint_(l)\nl += ( \" \" + str(n) )\nprint_(l)\nl += ( \" \" + str(x) )\nprint_(l)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "oss.str() = \" \"\noss.str() = \" ABCDEFG \"\noss.str() = \" ABCDEFG 33 \"\noss.str() = \" ABCDEFG 33 2.718 \"\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.9 Using an Input String Stream iss ABCDEFG 44 3.14 istringstream\nThis program is similar to the one in Example 9.8 \nexcept that it reads from an input string stream iss\ninstead of writing to an output string stream.:\n'''\n'''\nEXAMPLE 9.8 Using an Output String Stream\nThis program creates four objects: a character string s,\nan integer n, a floating-point number x, and an output string stream oss:\n'''\n\ndef print_(iss,s='',n=0,x=0.0):\n print 's = \"' , s , '\", n = ' , n , \", x = \" , x, ', iss.str() = \"' \\\n , iss , '\"' \n\ns=\"\"\nn=0\nx=0.0\nl = ''\niss = \"ABCDEFG 44 3.14\"\nprint_(iss)\ns = \"ABCDEFG\"\nprint_(iss,s)\nn = 44\nprint_(iss,s,n)\nx = 3.14\nprint_(iss,s,n,x)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "s = \" \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\ns = \" ABCDEFG \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\ns = \" ABCDEFG \", n = 44 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\ns = \" ABCDEFG \", n = 44 , x = 3.14 , iss.str() = \" ABCDEFG 44 3.14 \"\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashu1.png b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashu1.png Binary files differnew file mode 100644 index 00000000..13e13f29 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashu1.png diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashu2.png b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashu2.png Binary files differnew file mode 100644 index 00000000..e1bc353c --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashu2.png diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashu3.png b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashu3.png Binary files differnew file mode 100644 index 00000000..60dd1e4b --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashu3.png diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashutosh-3-1_1.png b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashutosh-3-1_1.png Binary files differnew file mode 100644 index 00000000..13e13f29 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashutosh-3-1_1.png diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashutosh-3-2_1.png b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashutosh-3-2_1.png Binary files differnew file mode 100644 index 00000000..e1bc353c --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashutosh-3-2_1.png diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashutosh-3-3_1.png b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashutosh-3-3_1.png Binary files differnew file mode 100644 index 00000000..60dd1e4b --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/screenshots/ashutosh-3-3_1.png |