diff options
Diffstat (limited to 'Schaum's_Outline/ch2.ipynb')
-rwxr-xr-x | Schaum's_Outline/ch2.ipynb | 740 |
1 files changed, 740 insertions, 0 deletions
diff --git a/Schaum's_Outline/ch2.ipynb b/Schaum's_Outline/ch2.ipynb new file mode 100755 index 00000000..8697f28c --- /dev/null +++ b/Schaum's_Outline/ch2.ipynb @@ -0,0 +1,740 @@ +{
+ "metadata": {
+ "name": "ch2"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Chapter 2: Fundamental Types"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.1, page no. 17"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\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\n",
+ "flag = True\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.2, page no. 19"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\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\n",
+ "c = t, int(c) = 116\n",
+ "c = \t, int(c) = 9\n",
+ "c = !, int(c) = 33\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.3, page no. 19"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "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\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
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.4, page no. 21"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\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\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": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.5, page no. 21"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\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\n",
+ "m = 45 , n = 44\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.6, page no. 22"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\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\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
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.7, page no. 23"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\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\n",
+ "x+y = 74.000000\n",
+ "x-y = 34.000000\n",
+ "x*y = 1080.000000\n",
+ "x/y = 2.700000\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.8, page no. 23s"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "\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 \n",
+ "float : 16\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.9, page no. 24"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "\n",
+ "fbits = 8*sys.getsizeof(float(123))\n",
+ "\n",
+ "# each byte contains 8 bits\n",
+ "print \"float uses : %d bits:\\n\\t\" % fbits "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "float uses : 128 bits:\n",
+ "\t\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.10, page no. 25"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\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"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.11, page no. 26"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\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\n",
+ "k = A\n",
+ "m = A\n",
+ "n = A\n",
+ "x = A\n",
+ "y = A\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.12, page no. 27"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\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\n",
+ "n = 1000000\n",
+ "n = 1000000000\n",
+ "n = 1000000000000\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.13, page no. 27"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\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\n",
+ "x = 1000000.000000\n",
+ "x = 1000000000000.000000\n",
+ "x = 999999999999999983222784.000000\n",
+ "x = 1000000000000000043845843045076197354634047651840.000000\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.14, page no. 28"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\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\n",
+ "y = 0.333333\n",
+ "z = -0.000000\n",
+ "z does not equal 0.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.15, page no. 28"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import math\n",
+ "\n",
+ "\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": [
+ {
+ "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\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
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.17, page no. 31"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "x = float(raw_input(\"Enter float: \"))\n",
+ "print \"Its reciprocal is: \",\n",
+ "print 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": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.18, page no. 31"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\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": [],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.19, page no. 32"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# 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 \n",
+ "In main(): x = 33\n",
+ "In main(): x = 33 \n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |