summaryrefslogtreecommitdiff
path: root/Schaum's_Outlines_-_Programming_with_C++/ch2.ipynb
diff options
context:
space:
mode:
authordebashisdeb2014-06-20 15:42:42 +0530
committerdebashisdeb2014-06-20 15:42:42 +0530
commit83c1bfceb1b681b4bb7253b47491be2d8b2014a1 (patch)
treef54eab21dd3d725d64a495fcd47c00d37abed004 /Schaum's_Outlines_-_Programming_with_C++/ch2.ipynb
parenta78126bbe4443e9526a64df9d8245c4af8843044 (diff)
downloadPython-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++/ch2.ipynb')
-rw-r--r--Schaum's_Outlines_-_Programming_with_C++/ch2.ipynb390
1 files changed, 349 insertions, 41 deletions
diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch2.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch2.ipynb
index 606044d0..52984e73 100644
--- a/Schaum's_Outlines_-_Programming_with_C++/ch2.ipynb
+++ b/Schaum's_Outlines_-_Programming_with_C++/ch2.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "CH2"
+ "name": "",
+ "signature": "sha256:43f850ff4e8965af5c5db1b79b7b4f60c3104eca56084bd90dc829928ca10593"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,14 +11,24 @@
{
"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",
+ "input": [
+ "\n",
+ "# prints the value of a boolean variable:\n",
+ "flag=False\n",
+ "print \"flag = %r\" % flag\n",
+ "flag = True\n",
+ "print \"flag = %r\" % flag"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "flag = False\nflag = True\n"
+ "text": [
+ "flag = False\n",
+ "flag = True\n"
+ ]
}
],
"prompt_number": 1
@@ -25,14 +36,31 @@
{
"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",
+ "input": [
+ "\n",
+ "\n",
+ "# prints the character and its internally stored\n",
+ "c='A'\n",
+ "print \"c = \" + c + \", int(c) = %d\" % ord(c)\n",
+ "c='t'\n",
+ "print \"c = \" + c + \", int(c) = %d\" % ord(c)\n",
+ "c='\\t' # the tab character\n",
+ "print \"c = \" + c + \", int(c) = %d\" % ord(c)\n",
+ "c='!'\n",
+ "print \"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"
+ "text": [
+ "c = A, int(c) = 65\n",
+ "c = t, int(c) = 116\n",
+ "c = \t, int(c) = 9\n",
+ "c = !, int(c) = 33\n"
+ ]
}
],
"prompt_number": 2
@@ -40,14 +68,26 @@
{
"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",
+ "input": [
+ "\n",
+ "import sys\n",
+ "# defines the constants SHRT_MIN, etc.\n",
+ "print 'maximum limit int : ',\n",
+ "print sys.maxint\n",
+ "print 'float info'\n",
+ "print 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"
+ "text": [
+ "maximum limit int : 2147483647\n",
+ "float info\n",
+ "sys.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
@@ -55,14 +95,32 @@
{
"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",
+ "input": [
+ "\n",
+ "# tests operators +, -, *, /, and %:\n",
+ "m=54\n",
+ "n=20\n",
+ "print \"m = %d and n = %d\" %(m,n)\n",
+ "print \"m+n = %d\" % (m+n) # 54+20 = 74\n",
+ "print \"m-n = %d\" % (m-n) # 54-20 = 34\n",
+ "print \"m*n = %d\" % (m*n)# 54*20 = 1080\n",
+ "print \"m/n = %d\" % (m/n) # 54/20 = 2\n",
+ "print \"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"
+ "text": [
+ "m = 54 and n = 20\n",
+ "m+n = 74\n",
+ "m-n = 34\n",
+ "m*n = 1080\n",
+ "m/n = 2\n",
+ "m modulo by n = 14\n"
+ ]
}
],
"prompt_number": 4
@@ -70,14 +128,29 @@
{
"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",
+ "input": [
+ "\n",
+ "\n",
+ "# shows the difference between m++ and ++m:\n",
+ "m = 44\n",
+ "m += 1\n",
+ "n = m\n",
+ "print \"m = %d , n = %d\" %(m,n)\n",
+ "m = 44\n",
+ "n = m # the post-increment operator is applied to m\n",
+ "m += 1\n",
+ "print \"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"
+ "text": [
+ "m = 45 , n = 45\n",
+ "m = 45 , n = 44\n"
+ ]
}
],
"prompt_number": 5
@@ -85,14 +158,37 @@
{
"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",
+ "input": [
+ "\n",
+ "\n",
+ "# tests arithmetic assignment operators:\n",
+ "n=22\n",
+ "print \"n = %d\" % n\n",
+ "n += 9 # adds 9 to n\n",
+ "print \"After n += 9, n = %d\" % n\n",
+ "n -= 5 # subtracts 5 from n\n",
+ "print \"After n -= 5, n = %d\" % n\n",
+ "n *= 2 # multiplies n by 3\n",
+ "print \"After n *= 2, n = %d\" % n \n",
+ "n /= 3 # divides n by 9\n",
+ "print \"After n /= 3, n = %d\" % n \n",
+ "n %= 7 # reduces n to the remainder from dividing by 4\n",
+ "print '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"
+ "text": [
+ "n = 22\n",
+ "After n += 9, n = 31\n",
+ "After n -= 5, n = 26\n",
+ "After n *= 2, n = 52\n",
+ "After n /= 3, n = 17\n",
+ "After n modulo by 7 n = 3\n"
+ ]
}
],
"prompt_number": 6
@@ -100,14 +196,30 @@
{
"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",
+ "input": [
+ "\n",
+ "# tests the floating-point operators +, -, *, and /:\n",
+ "x=54.0\n",
+ "y=20.0\n",
+ "print \"x = %f and y = %f\" %(x,y)\n",
+ "print \"x+y = %f\" %(x+y) # 54.0+20.0 = 74.0\n",
+ "print \"x-y = %f\" % (x-y) # 54.0-20.0 = 34.0\n",
+ "print \"x*y = %f\" %( x*y) # 54.0*20.0 = 1080.0\n",
+ "print \"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"
+ "text": [
+ "x = 54.000000 and y = 20.000000\n",
+ "x+y = 74.000000\n",
+ "x-y = 34.000000\n",
+ "x*y = 1080.000000\n",
+ "x/y = 2.700000\n"
+ ]
}
],
"prompt_number": 7
@@ -115,14 +227,31 @@
{
"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))",
+ "input": [
+ "\n",
+ "import sys\n",
+ "# prints the storage sizes of the fundamental types:\n",
+ "print \"Number of bytes used:\\n\"\n",
+ "\n",
+ "print \" char: %d \" % sys.getsizeof('a')\n",
+ "print \" int : %d \" % sys.getsizeof(int(1))\n",
+ "print \" string : %d \" % sys.getsizeof(str('hellololdei'))\n",
+ "print \"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"
+ "text": [
+ "Number of bytes used:\n",
+ "\n",
+ " char: 22 \n",
+ " int : 12 \n",
+ " string : 32 \n",
+ "float : 16\n"
+ ]
}
],
"prompt_number": 8
@@ -130,14 +259,25 @@
{
"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",
+ "input": [
+ "\n",
+ "import sys\n",
+ "# prints the storage sizes of the fundamental types:\n",
+ "fbits = 8*sys.getsizeof(float(123))\n",
+ "\n",
+ "# each byte contains 8 bits\n",
+ "print \"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"
+ "text": [
+ "float uses : 128 bits:\n",
+ "\t\n"
+ ]
}
],
"prompt_number": 9
@@ -145,14 +285,22 @@
{
"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)",
+ "input": [
+ "\n",
+ "# casts a double value as an int:\n",
+ "v = 1234.56789\n",
+ "n = int(v);\n",
+ "print \"v = %f, n = %d\" %(v,n)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "v = 1234.567890, n = 1234\n"
+ "text": [
+ "v = 1234.567890, n = 1234\n"
+ ]
}
],
"prompt_number": 10
@@ -160,14 +308,36 @@
{
"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",
+ "input": [
+ "\n",
+ "# prints promoted vales of 65 from char to double:\n",
+ "c='A'\n",
+ "print \"char c = \" + c\n",
+ "k=c;\n",
+ "print \"k = \" + k \n",
+ "m=k;\n",
+ "print \"m = \" + m \n",
+ "n=m\n",
+ "print \"n = \" + n\n",
+ "x=m\n",
+ "print \"x = \" + x \n",
+ "y=x\n",
+ "print \"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"
+ "text": [
+ "char c = A\n",
+ "k = A\n",
+ "m = A\n",
+ "n = A\n",
+ "x = A\n",
+ "y = A\n"
+ ]
}
],
"prompt_number": 11
@@ -175,14 +345,30 @@
{
"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",
+ "input": [
+ "\n",
+ "# prints n until it overflows:\n",
+ "n=1000\n",
+ "print \"n = %d\" % n\n",
+ "n *= 1000 # multiplies n by 1000\n",
+ "print \"n = %d\" % n\n",
+ "n *= 1000 # multiplies n by 1000\n",
+ "print \"n = %d\" % n\n",
+ "n *= 1000 # multiplies n by 1000\n",
+ "print \"n = %d\" % n\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 1000\nn = 1000000\nn = 1000000000\nn = 1000000000000\n"
+ "text": [
+ "n = 1000\n",
+ "n = 1000000\n",
+ "n = 1000000000\n",
+ "n = 1000000000000\n"
+ ]
}
],
"prompt_number": 12
@@ -190,14 +376,33 @@
{
"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",
+ "input": [
+ "\n",
+ "# prints x until it overflows:\n",
+ "x=1000.0\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"x = %f\" % x\n",
+ "x *= x # multiplies n by itself; i.e., it squares x\n",
+ "print \"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"
+ "text": [
+ "x = 1000.000000\n",
+ "x = 1000000.000000\n",
+ "x = 1000000000000.000000\n",
+ "x = 999999999999999983222784.000000\n",
+ "x = 1000000000000000043845843045076197354634047651840.000000\n"
+ ]
}
],
"prompt_number": 13
@@ -205,14 +410,33 @@
{
"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",
+ "input": [
+ "\n",
+ "# illustrates round-off error::\n",
+ "x = 1000/3.0\n",
+ "print \"x = %f\" %x # x = 1000/3\n",
+ "y = x - 333.0\n",
+ "print \"y = %f\" % y # y = 1/3\n",
+ "z = 3*y - 1.0\n",
+ "print \"z = %f\" %z # z = 3(1/3) - 1\n",
+ "if (z == 0):\n",
+ " print \"z == 0.\\n\"\n",
+ "else:\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"
+ "text": [
+ "x = 333.333333\n",
+ "y = 0.333333\n",
+ "z = -0.000000\n",
+ "z does not equal 0.\n",
+ "\n"
+ ]
}
],
"prompt_number": 14
@@ -220,7 +444,34 @@
{
"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",
+ "input": [
+ "\n",
+ "import math\n",
+ "\n",
+ "# implements the quadratic formula\n",
+ "a = float(raw_input(\"Enter the coefficients of a quadratic equation:\\n a : \"))\n",
+ "b = float(raw_input('b : '))\n",
+ "c = float(raw_input('c : '))\n",
+ "\n",
+ "print \"The equation is: \",\n",
+ "print a,\n",
+ "print \"*x*x + \",\n",
+ "print b,\n",
+ "print \"*x + \" ,\n",
+ "print c,\n",
+ "print \" = 0\" \n",
+ "\n",
+ "d = b*b - 4*a*c # discriminant\n",
+ "sqrtd = math.sqrt(d)\n",
+ "x1 = (-b + sqrtd)/(2*a)\n",
+ "x2 = (-b - sqrtd)/(2*a)\n",
+ "print \"The solutions are:\"\n",
+ "print \"\\tx1 = %f\" % x1\n",
+ "print \"\\tx2 = %f\" % x2\n",
+ "print \"Check:\" \n",
+ "print \"\\ta*x1*x1 + b*x1 + c = %f\" %( a*x1*x1 + b*x1 + c)\n",
+ "print \"\\ta*x2*x2 + b*x2 + c = %f\" %( a*x2*x2 + b*x2 + c)\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -228,24 +479,39 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter the coefficients of a quadratic equation:\n a : 2\n"
+ "text": [
+ "Enter the coefficients of a quadratic equation:\n",
+ " a : 2\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "b : 1\n"
+ "text": [
+ "b : 1\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "c : -3\n"
+ "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"
+ "text": [
+ "The equation is: 2.0 *x*x + 1.0 *x + -3.0 = 0\n",
+ "The solutions are:\n",
+ "\tx1 = 1.000000\n",
+ "\tx2 = -1.500000\n",
+ "Check:\n",
+ "\ta*x1*x1 + b*x1 + c = 0.000000\n",
+ "\ta*x2*x2 + b*x2 + c = 0.000000\n"
+ ]
}
],
"prompt_number": 15
@@ -253,7 +519,14 @@
{
"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 ",
+ "input": [
+ "\n",
+ "\n",
+ "# prints double values in scientific e-format:\n",
+ "x = float(raw_input(\"Enter float: \"))\n",
+ "print \"Its reciprocal is: \",\n",
+ "print 1/x "
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -261,12 +534,16 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter float: 234.567e89\n"
+ "text": [
+ "Enter float: 234.567e89\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Its reciprocal is: 4.2631742743e-92\n"
+ "text": [
+ "Its reciprocal is: 4.2631742743e-92\n"
+ ]
}
],
"prompt_number": 16
@@ -274,7 +551,19 @@
{
"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",
+ "input": [
+ "\n",
+ "# illustrates the scope of variables:\n",
+ "x = 11\n",
+ "# ERROR: this is not in the scope of x\n",
+ "if 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\n",
+ "x = 66 # OK: this is in the scope of x\n",
+ "y = 77 # ERROR: this is not in the scope of y\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -283,14 +572,33 @@
{
"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",
+ "input": [
+ "\n",
+ "# this x is global\n",
+ "x = 11\n",
+ "\n",
+ "if 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\n",
+ "print \"In main(): x = %d\" %x \n",
+ "print \"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"
+ "text": [
+ "In block inside main(): x = 33 \n",
+ "In main(): x = 33\n",
+ "In main(): x = 33 \n"
+ ]
}
],
"prompt_number": 18
@@ -298,7 +606,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []