diff options
author | debashisdeb | 2014-06-20 15:42:42 +0530 |
---|---|---|
committer | debashisdeb | 2014-06-20 15:42:42 +0530 |
commit | 83c1bfceb1b681b4bb7253b47491be2d8b2014a1 (patch) | |
tree | f54eab21dd3d725d64a495fcd47c00d37abed004 /Schaum's_Outlines_-_Programming_with_C++/ch5.ipynb | |
parent | a78126bbe4443e9526a64df9d8245c4af8843044 (diff) | |
download | Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.gz Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.bz2 Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.zip |
removing problem statements
Diffstat (limited to 'Schaum's_Outlines_-_Programming_with_C++/ch5.ipynb')
-rw-r--r-- | Schaum's_Outlines_-_Programming_with_C++/ch5.ipynb | 1055 |
1 files changed, 950 insertions, 105 deletions
diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch5.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch5.ipynb index 6ef37c88..e3de2713 100644 --- a/Schaum's_Outlines_-_Programming_with_C++/ch5.ipynb +++ b/Schaum's_Outlines_-_Programming_with_C++/ch5.ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "ch5" + "name": "", + "signature": "sha256:24e76f61ba45b924df39848fa855d4479d9e602e3e0935f26f67699be40eb3ff" }, "nbformat": 3, "nbformat_minor": 0, @@ -10,14 +11,28 @@ { "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))", + "input": [ + "\n", + "import math\n", + "\n", + "# tests the sqrt() function:\n", + "for 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" + "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 @@ -25,14 +40,35 @@ { "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", + "input": [ + "\n", + "import math\n", + "# tests the identity sin 2x = 2 sin x cos x:\n", + "x = 0\n", + "while 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" + "text": [ + "0.000000 \t\t 0.000000 \t 0.000000\n", + "0.200000 \t\t 0.389418 \t 0.389418\n", + "0.400000 \t\t 0.717356 \t 0.717356\n", + "0.600000 \t\t 0.932039 \t 0.932039\n", + "0.800000 \t\t 0.999574 \t 0.999574\n", + "1.000000 \t\t 0.909297 \t 0.909297\n", + "1.200000 \t\t 0.675463 \t 0.675463\n", + "1.400000 \t\t 0.334988 \t 0.334988\n", + "1.600000 \t\t -0.058374 \t -0.058374\n", + "1.800000 \t\t -0.442520 \t -0.442520\n", + "2.000000 \t\t -0.756802 \t -0.756802\n" + ] } ], "prompt_number": 2 @@ -40,7 +76,14 @@ { "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", + "input": [ + "\n", + "\n", + "def cube(x):\n", + " # returns cube of x:\n", + " return x*x*x\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [], @@ -49,7 +92,19 @@ { "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))", + "input": [ + "\n", + "\n", + "def cube(x):\n", + " # returns cube of x:\n", + " return x*x*x\n", + "\n", + "# tests the cube() function:\n", + "n=1\n", + "while (n != 0):\n", + " n = int(raw_input())\n", + " print \"\\tcube( %d ) = %d\" %(n,cube(n))" + ], "language": "python", "metadata": {}, "outputs": [ @@ -57,45 +112,61 @@ "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "4\n" + "text": [ + "4\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tcube( 4 ) = 64\n" + "text": [ + "\tcube( 4 ) = 64\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "2\n" + "text": [ + "2\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tcube( 2 ) = 8\n" + "text": [ + "\tcube( 2 ) = 8\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "9\n" + "text": [ + "9\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tcube( 9 ) = 729\n" + "text": [ + "\tcube( 9 ) = 729\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "0\n" + "text": [ + "0\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tcube( 0 ) = 0\n" + "text": [ + "\tcube( 0 ) = 0\n" + ] } ], "prompt_number": 1 @@ -103,7 +174,24 @@ { "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", + "input": [ + "\n", + "def 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:\n", + "m = 1\n", + "n = 1\n", + "while 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": [ @@ -111,35 +199,47 @@ "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "5\n" + "text": [ + "5\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "2\n" + "text": [ + "2\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tmax( 5 , 2 ) = 5\n" + "text": [ + "\tmax( 5 , 2 ) = 5\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "0\n" + "text": [ + "0\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "3\n" + "text": [ + "3\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tmax( 0 , 3 ) = 3\n" + "text": [ + "\tmax( 0 , 3 ) = 3\n" + ] } ], "prompt_number": 2 @@ -147,7 +247,24 @@ { "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", + "input": [ + "\n", + "def 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:\n", + "m = 1\n", + "n = 1\n", + "while 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": [ @@ -155,35 +272,47 @@ "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "5\n" + "text": [ + "5\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "2\n" + "text": [ + "2\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tmax( 5 , 2 ) = 5\n" + "text": [ + "\tmax( 5 , 2 ) = 5\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "0\n" + "text": [ + "0\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "3\n" + "text": [ + "3\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tmax( 0 , 3 ) = 3\n" + "text": [ + "\tmax( 0 , 3 ) = 3\n" + ] } ], "prompt_number": 3 @@ -191,7 +320,18 @@ { "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", + "input": [ + "\n", + "\n", + "# returns larger of the two given integers:\n", + "\n", + "m = 1\n", + "n = 1\n", + "while 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": [ @@ -199,69 +339,93 @@ "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "5\n" + "text": [ + "5\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "4\n" + "text": [ + "4\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tmax(5,4) = 5\n" + "text": [ + "\tmax(5,4) = 5\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "4\n" + "text": [ + "4\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "3\n" + "text": [ + "3\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tmax(4,3) = 4\n" + "text": [ + "\tmax(4,3) = 4\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "8\n" + "text": [ + "8\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "0\n" + "text": [ + "0\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tmax(8,0) = 8\n" + "text": [ + "\tmax(8,0) = 8\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "0\n" + "text": [ + "0\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "5\n" + "text": [ + "5\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\tmax(0,5) = 5\n" + "text": [ + "\tmax(0,5) = 5\n" + ] } ], "prompt_number": 4 @@ -269,14 +433,29 @@ { "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", + "input": [ + "\n", + "def 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", + "for 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" + "text": [ + "0 1 1 2 6 24 120\n" + ] } ], "prompt_number": 5 @@ -284,14 +463,46 @@ { "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", + "input": [ + "\n", + "def 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", + "\n", + "def 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", + "\n", + "for 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" + "text": [ + "0 0 \n", + "0 1 0 \n", + "0 1 1 0 \n", + "0 1 2 2 0 \n", + "0 1 3 6 6 0 \n", + "0 1 4 12 24 24 0 \n", + "0 1 5 20 60 120 120 0 \n", + "0 1 6 30 120 360 720 720 0 \n", + "0 1 7 42 210 840 2520 5040 5040 0 \n" + ] } ], "prompt_number": 6 @@ -299,7 +510,48 @@ { "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", + "input": [ + "\n", + "\n", + "def 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:\n", + "month = 1\n", + "while 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": [ @@ -307,47 +559,64 @@ "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "9\n" + "text": [ + "9\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "12\n" + "text": [ + "12\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "1989\n" + "text": [ + "1989\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "September 12 , 1989\n" + "text": [ + "September 12 , 1989\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "0\n" + "text": [ + "0\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "5\n" + "text": [ + "5\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "2001\n" + "text": [ + "2001\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "Error: parameter out of range.\n\n" + "text": [ + "Error: parameter out of range.\n", + "\n" + ] } ], "prompt_number": 8 @@ -355,14 +624,305 @@ { "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", + "input": [ + "\n", + "import string\n", + "def ispunct(s):\n", + " return all(c in string.punctuation for c in s)\n", + "def 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:\n", + "for 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" + "text": [ + " The character [\u0000] is a control character.\n", + "\n", + "The character [\u0001] is a Error.\n", + "\n", + "The character [\u0002] is a Error.\n", + "\n", + "The character [\u0003] is a Error.\n", + "\n", + "The character [\u0004] is a Error.\n", + "\n", + "The character [\u0005] is a Error.\n", + "\n", + "The character [\u0006] is a Error.\n", + "\n", + "The character [\u0007] is a Error.\n", + "\n", + "The character [\b] is a Error.\n", + "\n", + "The character [\t] is a white space character.\n", + "\n", + "The character [\n", + "] is a white space character.\n", + "\n", + "The character [\u000b", + "] is a white space character.\n", + "\n", + "The character [\f", + "] is a white space character.\n", + "\n", + "The character [\r", + "] is a white space character.\n", + "\n", + "The character [\u000e] is a control character.\n", + "\n", + "The character [\u000f] is a control character.\n", + "\n", + "The character [\u0010] is a Error.\n", + "\n", + "The character [\u0011] is a Error.\n", + "\n", + "The character [\u0012] is a Error.\n", + "\n", + "The character [\u0013] is a Error.\n", + "\n", + "The character [\u0014] is a Error.\n", + "\n", + "The character [\u0015] is a Error.\n", + "\n", + "The character [\u0016] is a Error.\n", + "\n", + "The character [\u0017] is a Error.\n", + "\n", + "The character [\u0018] is a Error.\n", + "\n", + "The character [\u0019] is a Error.\n", + "\n", + "The character [\u001a] is a Error.\n", + "\n", + "The character [\u001b] is a Error.\n", + "\n", + "The character [\u001c", + "] is a Error.\n", + "\n", + "The character [\u001d", + "] is a Error.\n", + "\n", + "The character [\u001e", + "] is a Error.\n", + "\n", + "The character [\u001f] is a Error.\n", + "\n", + "The character [ ] is a white space character.\n", + "\n", + "The character [!] is a punctuation mark.\n", + "\n", + "The character [\"] is a punctuation mark.\n", + "\n", + "The character [#] is a punctuation mark.\n", + "\n", + "The character [$] is a punctuation mark.\n", + "\n", + "The character [%] is a punctuation mark.\n", + "\n", + "The character [&] is a punctuation mark.\n", + "\n", + "The character ['] is a punctuation mark.\n", + "\n", + "The character [(] is a punctuation mark.\n", + "\n", + "The character [)] is a punctuation mark.\n", + "\n", + "The character [*] is a punctuation mark.\n", + "\n", + "The character [+] is a punctuation mark.\n", + "\n", + "The character [,] is a punctuation mark.\n", + "\n", + "The character [-] is a punctuation mark.\n", + "\n", + "The character [.] is a punctuation mark.\n", + "\n", + "The character [/] is a punctuation mark.\n", + "\n", + "The character [0] is a digit.\n", + "\n", + "The character [1] is a digit.\n", + "\n", + "The character [2] is a digit.\n", + "\n", + "The character [3] is a digit.\n", + "\n", + "The character [4] is a digit.\n", + "\n", + "The character [5] is a digit.\n", + "\n", + "The character [6] is a digit.\n", + "\n", + "The character [7] is a digit.\n", + "\n", + "The character [8] is a digit.\n", + "\n", + "The character [9] is a digit.\n", + "\n", + "The character [:] is a punctuation mark.\n", + "\n", + "The character [;] is a punctuation mark.\n", + "\n", + "The character [<] is a punctuation mark.\n", + "\n", + "The character [=] is a punctuation mark.\n", + "\n", + "The character [>] is a punctuation mark.\n", + "\n", + "The character [?] is a punctuation mark.\n", + "\n", + "The character [@] is a punctuation mark.\n", + "\n", + "The character [A] is a capital letter.\n", + "\n", + "The character [B] is a capital letter.\n", + "\n", + "The character [C] is a capital letter.\n", + "\n", + "The character [D] is a capital letter.\n", + "\n", + "The character [E] is a capital letter.\n", + "\n", + "The character [F] is a capital letter.\n", + "\n", + "The character [G] is a capital letter.\n", + "\n", + "The character [H] is a capital letter.\n", + "\n", + "The character [I] is a capital letter.\n", + "\n", + "The character [J] is a capital letter.\n", + "\n", + "The character [K] is a capital letter.\n", + "\n", + "The character [L] is a capital letter.\n", + "\n", + "The character [M] is a capital letter.\n", + "\n", + "The character [N] is a capital letter.\n", + "\n", + "The character [O] is a capital letter.\n", + "\n", + "The character [P] is a capital letter.\n", + "\n", + "The character [Q] is a capital letter.\n", + "\n", + "The character [R] is a capital letter.\n", + "\n", + "The character [S] is a capital letter.\n", + "\n", + "The character [T] is a capital letter.\n", + "\n", + "The character [U] is a capital letter.\n", + "\n", + "The character [V] is a capital letter.\n", + "\n", + "The character [W] is a capital letter.\n", + "\n", + "The character [X] is a capital letter.\n", + "\n", + "The character [Y] is a capital letter.\n", + "\n", + "The character [Z] is a capital letter.\n", + "\n", + "The character [[] is a punctuation mark.\n", + "\n", + "The character [\\] is a punctuation mark.\n", + "\n", + "The character []] is a punctuation mark.\n", + "\n", + "The character [^] is a punctuation mark.\n", + "\n", + "The character [_] is a punctuation mark.\n", + "\n", + "The character [`] is a punctuation mark.\n", + "\n", + "The character [a] is a lower-case letter.\n", + "\n", + "The character [b] is a lower-case letter.\n", + "\n", + "The character [c] is a lower-case letter.\n", + "\n", + "The character [d] is a lower-case letter.\n", + "\n", + "The character [e] is a lower-case letter.\n", + "\n", + "The character [f] is a lower-case letter.\n", + "\n", + "The character [g] is a lower-case letter.\n", + "\n", + "The character [h] is a lower-case letter.\n", + "\n", + "The character [i] is a lower-case letter.\n", + "\n", + "The character [j] is a lower-case letter.\n", + "\n", + "The character [k] is a lower-case letter.\n", + "\n", + "The character [l] is a lower-case letter.\n", + "\n", + "The character [m] is a lower-case letter.\n", + "\n", + "The character [n] is a lower-case letter.\n", + "\n", + "The character [o] is a lower-case letter.\n", + "\n", + "The character [p] is a lower-case letter.\n", + "\n", + "The character [q] is a lower-case letter.\n", + "\n", + "The character [r] is a lower-case letter.\n", + "\n", + "The character [s] is a lower-case letter.\n", + "\n", + "The character [t] is a lower-case letter.\n", + "\n", + "The character [u] is a lower-case letter.\n", + "\n", + "The character [v] is a lower-case letter.\n", + "\n", + "The character [w] is a lower-case letter.\n", + "\n", + "The character [x] is a lower-case letter.\n", + "\n", + "The character [y] is a lower-case letter.\n", + "\n", + "The character [z] is a lower-case letter.\n", + "\n", + "The character [{] is a punctuation mark.\n", + "\n", + "The character [|] is a punctuation mark.\n", + "\n", + "The character [}] is a punctuation mark.\n", + "\n", + "The character [~] is a punctuation mark.\n", + "\n", + "The character [\u007f] is a Error.\n", + "\n" + ] } ], "prompt_number": 4 @@ -370,14 +930,40 @@ { "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", + "input": [ + "\n", + "import math\n", + "def 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", + "\n", + "for 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" + "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 @@ -385,7 +971,21 @@ { "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", + "input": [ + "\n", + "def 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:\n", + "n = 2\n", + "while 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": [ @@ -393,45 +993,61 @@ "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "2004\n" + "text": [ + "2004\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "2004 is a leap year.\n" + "text": [ + "2004 is a leap year.\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "2006\n" + "text": [ + "2006\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "2006 is not a leap year.\n" + "text": [ + "2006 is not a leap year.\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "2013\n" + "text": [ + "2013\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "2013 is not a leap year.\n" + "text": [ + "2013 is not a leap year.\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "0\n" + "text": [ + "0\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "0 is a leap year.\n" + "text": [ + "0 is a leap year.\n" + ] } ], "prompt_number": 11 @@ -439,47 +1055,88 @@ { "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", + "input": [ + "\n", + "\n", + "def 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", + "\n", + "a = age();\n", + "print \"\\nYou are %d years old.\" %a\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "How old are you: \n" + "text": [ + "How old are you: \n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "-12\n" + "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" + "text": [ + "\u0007\tYour age could not be negative.\n", + "\n", + "\tTry again.\n", + "\n", + "How old are you: \n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "125\n" + "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" + "text": [ + "\u0007\tYou could not be over 120.\n", + "\n", + "\tTry again.\n", + "\n", + "How old are you: \n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "24\n" + "text": [ + "24\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "\nYou are 24 years old.\n" + "text": [ + "\n", + "You are 24 years old.\n" + ] } ], "prompt_number": 14 @@ -487,14 +1144,29 @@ { "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", + "input": [ + "\n", + "\n", + "def swap(x,y):\n", + " # exchanges the values of x and y:\n", + " x[0],y[0] = y[0],x[0]\n", + "\n", + "a = [22.2]\n", + "b = [44.4]\n", + "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n", + "swap(a,b)\n", + "print \"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" + "text": [ + "a = 22.20 , b = 44.40 \n", + "a = 44.40 , b = 22.20 \n" + ] } ], "prompt_number": 15 @@ -502,14 +1174,33 @@ { "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", + "input": [ + "\n", + "\n", + "def f(x,y):\n", + " x[0]= 88\n", + " y[0] = 99\n", + "\n", + "# tests the f() function:\n", + "a = [22]\n", + "b = [44]\n", + "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n", + "f(a,b)\n", + "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n", + "f(2*a,b)\n", + "print \"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" + "text": [ + "a = 22.00 , b = 44.00 \n", + "a = 88.00 , b = 99.00 \n", + "a = 88.00 , b = 99.00 \n" + ] } ], "prompt_number": 16 @@ -517,25 +1208,46 @@ { "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)", + "input": [ + "\n", + "\n", + "def 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:\n", + "print \"Enter radius: \"\n", + "r = int(raw_input())\n", + "a,c = computeCircle(r)\n", + "print \"area = %.2f , circumference = %.2f\" %(a,c)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Enter radius: \n" + "text": [ + "Enter radius: \n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "5\n" + "text": [ + "5\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "area = 78.54 , circumference = 31.42\n" + "text": [ + "area = 78.54 , circumference = 31.42\n" + ] } ], "prompt_number": 17 @@ -543,14 +1255,37 @@ { "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", + "input": [ + "\n", + "def 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", + "\n", + "x = [22]\n", + "y = [33]\n", + "z = [44]\n", + "\n", + "print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n", + "f(x,y,z)\n", + "print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n", + "x[0] = 2*x[0] - 3\n", + "f(x,y,z)\n", + "print \"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" + "text": [ + "x = 22 , y = 33 , z = 44\n", + "x = 66 , y = 77 , z = 44\n", + "x = 66 , y = 77 , z = 44\n", + "x = 173 , y = 121 , z = 44\n", + "x = 173 , y = 121 , z = 44\n" + ] } ], "prompt_number": 5 @@ -558,25 +1293,42 @@ { "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", + "input": [ + "\n", + "def cube(x):\n", + " # returns cube of x:\n", + " return x*x*x\n", + "\n", + "# tests the cube() function:\n", + "print cube(4)\n", + "x = int(raw_input())\n", + "y = cube(2*x-3)\n", + "print y\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "64\n" + "text": [ + "64\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "5\n" + "text": [ + "5\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "343\n" + "text": [ + "343\n" + ] } ], "prompt_number": 19 @@ -584,14 +1336,40 @@ { "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", + "input": [ + "\n", + "x = 11\n", + "\n", + "def f():\n", + " x = 44\n", + " print \"In f(): x = %d\" % x \n", + "\n", + "def g():\n", + " print \"In g(): x = %d\" % x \n", + "\n", + "x = 22\n", + "x = 33\n", + "print \"In block inside main(): x = %d\" % x\n", + "\n", + "\n", + "print \"In main(): x = %d\" % x \n", + "print \"In main(): ::x = %d\" % x \n", + "f()\n", + "g()\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" + "text": [ + "In block inside main(): x = 33\n", + "In main(): x = 33\n", + "In main(): ::x = 33\n", + "In f(): x = 44\n", + "In g(): x = 33\n" + ] } ], "prompt_number": 20 @@ -599,14 +1377,28 @@ { "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", + "input": [ + "\n", + "def 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", + " \n", + "print max(99,77), \" \" , max(55,66,33)\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "99 66\n" + "text": [ + "99 66\n" + ] } ], "prompt_number": 21 @@ -614,31 +1406,51 @@ { "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", + "input": [ + "\n", + "\n", + "# prints the quotient of two input integers:\n", + "print \"Enter two integers: \"\n", + "n = int(raw_input())\n", + "d = int(raw_input())\n", + "if (d == 0):\n", + " import sys\n", + " sys.exit(0)\n", + "print n , \"/\" , d , \" = \" , n/d \n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Enter two integers: \n" + "text": [ + "Enter two integers: \n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "8\n" + "text": [ + "8\n" + ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "2\n" + "text": [ + "2\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "8 / 2 = 4\n" + "text": [ + "8 / 2 = 4\n" + ] } ], "prompt_number": 22 @@ -646,7 +1458,18 @@ { "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", + "input": [ + "\n", + "def 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", + "\n", + "x = float(raw_input())\n", + "print reciprocal(x)\n" + ], "language": "python", "metadata": {}, "outputs": [ @@ -654,12 +1477,16 @@ "name": "stdout", "output_type": "stream", "stream": "stdout", - "text": "25\n" + "text": [ + "25\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "0.04\n" + "text": [ + "0.04\n" + ] } ], "prompt_number": 23 @@ -667,14 +1494,32 @@ { "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", + "input": [ + "\n", + "def 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:\n", + "x = 2.0003\n", + "print \"p(x,7) = %f\" % p(x,7)\n", + "print \"p(x,7,6) = %f\" % p(x,7,6)\n", + "print \"p(x,7,6,5) = %f\" % p(x,7,6,5)\n", + "print \"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" + "text": [ + "p(x,7) = 7.000000\n", + "p(x,7,6) = 19.001800\n", + "p(x,7,6,5) = 39.007800\n", + "p(x,7,6,5,4) = 71.022203\n" + ] } ], "prompt_number": 24 @@ -682,7 +1527,7 @@ { "cell_type": "code", "collapsed": false, - "input": "", + "input": [], "language": "python", "metadata": {}, "outputs": [] |