diff options
Diffstat (limited to 'Teach_Yourself_C_in_24_Hours')
28 files changed, 6173 insertions, 0 deletions
diff --git a/Teach_Yourself_C_in_24_Hours/README.txt b/Teach_Yourself_C_in_24_Hours/README.txt new file mode 100755 index 00000000..f474d041 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/README.txt @@ -0,0 +1,10 @@ +Contributed By: Vaibhav Vajani +Course: others +College/Institute/Organization: brahmanand institute of management & science +Department/Designation: Assistant Professor +Book Title: Teach Yourself C in 24 Hours +Author: Tony Zhang +Publisher: SAMS Publication , 201 West 103rd St., Indianapolis, Indiana, 46290 USA +Year of publication: 2000 +Isbn: 0-672-31861-x +Edition: 2nd
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour1.ipynb b/Teach_Yourself_C_in_24_Hours/hour1.ipynb new file mode 100755 index 00000000..40f921ba --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour1.ipynb @@ -0,0 +1,59 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:69047173d3eeef59534ef80d7bcf63a4b6e2f6165dd08ce224ffe931dca31d9d"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 1: Taking the First Step"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1.1, Page No.3"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "ch=raw_input(\"Please type in one character\")\n",
+ "print \"The Character you just entered is: \",ch"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Please type in one characterH\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The Character you just entered is: H\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour10.ipynb b/Teach_Yourself_C_in_24_Hours/hour10.ipynb new file mode 100755 index 00000000..e6bfd5b9 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour10.ipynb @@ -0,0 +1,356 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:cacc571d4837d5307aef4322356742c15639b9ac8aaaa6bc1bc1580c03c1a8fb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 10: Controlling Programming flow"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Exmaple 10.1, Page No.157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print \"Integers that can be divided by both 2 and 3\"\n",
+ "print \"(within the range of 0 to 100):\"\n",
+ "for i in range(0,100):\n",
+ " if i%2==0 and i%3==0:\n",
+ " print \" \",i\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Integers that can be divided by both 2 and 3\n",
+ "(within the range of 0 to 100):\n",
+ " 0\n",
+ " 6\n",
+ " 12\n",
+ " 18\n",
+ " 24\n",
+ " 30\n",
+ " 36\n",
+ " 42\n",
+ " 48\n",
+ " 54\n",
+ " 60\n",
+ " 66\n",
+ " 72\n",
+ " 78\n",
+ " 84\n",
+ " 90\n",
+ " 96\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Exmaple 10.2, Page No.159"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print \"Even Number Odd Number\"\n",
+ "for i in range(0,10):\n",
+ " if(i%2==0):\n",
+ " print i,\n",
+ " else:\n",
+ " print \"{:14d}\".format(i)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Even Number Odd Number\n",
+ "0 1\n",
+ "2 3\n",
+ "4 5\n",
+ "6 7\n",
+ "8 9\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Exmaple 10.3, Page No.160"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "for i in range(-5,6):\n",
+ " if i>0:\n",
+ " if i%2==0:\n",
+ " print \"{:d} is an even number.\".format(i)\n",
+ " else:\n",
+ " print \"{:d} is an odd number.\".format(i)\n",
+ " elif i==0:\n",
+ " print \"The number is zero.\"\n",
+ " else:\n",
+ " print \"Negative number:\",i"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Negative number: -5\n",
+ "Negative number: -4\n",
+ "Negative number: -3\n",
+ "Negative number: -2\n",
+ "Negative number: -1\n",
+ "The number is zero.\n",
+ "1 is an odd number.\n",
+ "2 is an even number.\n",
+ "3 is an odd number.\n",
+ "4 is an even number.\n",
+ "5 is an odd number.\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.4, Page No.162"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "day=raw_input(\"Please enter a sigle digit for a day\\n(within the range of 1 to 3:)\")\n",
+ "if day=='1':\n",
+ " print \"Day 1\"\n",
+ "elif day=='2':\n",
+ " print \"Day 2\"\n",
+ "elif day=='3':\n",
+ " print \"Day 3\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Please enter a sigle digit for a day\n",
+ "(within the range of 1 to 3:)3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.5, Page No.164"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#This program is specially used to understand the \"break\" statement in switch..case. \n",
+ "#But in python we can't use break statement outside of the loop so break is not written with each if block.\n",
+ "day=raw_input(\"Please enter a sigle digit for a day\\n(within the range of 1 to 7:)\")\n",
+ "if day=='1':\n",
+ " print \"Day 1 is Sunday\"\n",
+ " \n",
+ "elif day=='2':\n",
+ " print \"Day 2 is Monday\"\n",
+ " \n",
+ "elif day=='3':\n",
+ " print \"Day 3 is Tuesday\"\n",
+ " \n",
+ "elif day=='4':\n",
+ " print \"Day 4 is Wednesday\"\n",
+ " \n",
+ "elif day=='5':\n",
+ " print \"Day 5 is Thursday\"\n",
+ " \n",
+ "elif day=='6':\n",
+ " print \"Day 6 is Friday\"\n",
+ " \n",
+ "elif day=='7':\n",
+ " print \"Day 7 is Saturday\"\n",
+ " \n",
+ "else:\n",
+ " print \"The digit is not within the range of 1 to 7\"\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Please enter a sigle digit for a day\n",
+ "(within the range of 1 to 7:)1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day 1 is Sunday\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.6, Page No.166"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print \"Enter a character:\\n (enter X to exit)\\n\"\n",
+ "while 1:\n",
+ " c=raw_input()\n",
+ " if c=='x':\n",
+ " break\n",
+ "print \"Break the infinite loop. Bye!\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a character:\n",
+ " (enter X to exit)\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "H\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Break the infinite loop. Bye!\n"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.7, Page No.167"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "sum=0\n",
+ "for i in range(1,8):\n",
+ " if i==3 or i==5:\n",
+ " continue\n",
+ " sum=sum+i\n",
+ "print \"The sum of 1,2,4,6, and 7 is:\",sum"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of 1,2,4,6, and 7 is: 20\n"
+ ]
+ }
+ ],
+ "prompt_number": 26
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour11.ipynb b/Teach_Yourself_C_in_24_Hours/hour11.ipynb new file mode 100755 index 00000000..49cab5a2 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour11.ipynb @@ -0,0 +1,221 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:5ac60189d3032c0d9eeb79f16671ba934dceed86c00074ca1744202c056c8dca"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 11: Understanding Pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.1, Page No 178"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "c=chr\n",
+ "x=int\n",
+ "y=float\n",
+ "\n",
+ "print \"Address=\"+str(id(c))+\" Content=\"+str(c)\n",
+ "print \"Address=\"+str(id(x))+\" Content=\"+str(x)\n",
+ "print \"Address=\"+str(id(y))+\" Content=\"+str(y)\n",
+ "\n",
+ "c='A'\n",
+ "x=7\n",
+ "y=123.45\n",
+ "\n",
+ "print \"Address=\"+str(id(c))+\" Content=\"+str(c)\n",
+ "print \"Address=\"+str(id(x))+\" Content=\"+str(x)\n",
+ "print \"Address=\"+str(id(y))+\" Content=\"+str(y)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address=19935400 Content=<built-in function chr>\n",
+ "Address=505552912 Content=<type 'int'>\n",
+ "Address=505547744 Content=<type 'float'>\n",
+ "Address=20542336 Content=A\n",
+ "Address=20177304 Content=7\n",
+ "Address=41743224 Content=123.45\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.2, Page No 180"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "c=chr\n",
+ "x=int\n",
+ "y=float\n",
+ "prt_c=chr\n",
+ "ptr_x=int\n",
+ "ptr_y=float\n",
+ "\n",
+ "c='A'\n",
+ "x=7\n",
+ "y=123.45\n",
+ "\n",
+ "print \"C: Address=\"+str(id(c))+\" Content=\"+str(c)\n",
+ "print \"X: Address=\"+str(id(x))+\" Content=\"+str(x)\n",
+ "print \"Y: Address=\"+str(id(y))+\" Content=\"+str(y)\n",
+ "\n",
+ "ptr_c=id(c)\n",
+ "print \"ptr_c: Address=\"+str(id(ptr_c))+\" Content=\"+str(ptr_c)\n",
+ "print \"*ptr_c -> \"+str(c)\n",
+ "\n",
+ "ptr_x=id(x)\n",
+ "print \"ptr_x: Address=\"+str(id(ptr_x))+\" Content=\"+str(ptr_x)\n",
+ "print \"*ptr_x -> \"+str(x)\n",
+ "\n",
+ "ptr_y=id(y)\n",
+ "print \"ptr_y: Address=\"+str(id(ptr_y))+\" Content=\"+str(ptr_y)\n",
+ "print \"*ptr_y -> \"+str(y)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C: Address=20542336 Content=A\n",
+ "X: Address=20177304 Content=7\n",
+ "Y: Address=41743208 Content=123.45\n",
+ "ptr_c: Address=42208720 Content=20542336\n",
+ "*ptr_c -> A\n",
+ "ptr_x: Address=42206796 Content=20177304\n",
+ "*ptr_x -> 7\n",
+ "ptr_y: Address=42206784 Content=41743208\n",
+ "*ptr_y -> 123.45\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.3, Page No 183"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "c=chr\n",
+ "ptr_c=chr\n",
+ "\n",
+ "c='A'\n",
+ "print \"C: Address=\"+str(id(c))+\" Content=\"+str(c)\n",
+ "\n",
+ "ptr_c=id(c)\n",
+ "print \"ptr_c: Address=\"+str(id(ptr_c))+\" Content=\"+str(ptr_c)\n",
+ "print \"*ptr_c -> \"+str(c)\n",
+ "\n",
+ "ptr_c='B'\n",
+ "print \"ptr_c: Address=\"+str(id(ptr_c))+\" Content=\"+str(ptr_c)\n",
+ "print \"*ptr_c -> \"+str(ptr_c)\n",
+ "\n",
+ "print \"C: Address=\"+str(id(c))+\" Content=\"+str(c)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C: Address=20542336 Content=A\n",
+ "ptr_c: Address=42206808 Content=20542336\n",
+ "*ptr_c -> A\n",
+ "ptr_c: Address=20342424 Content=B\n",
+ "*ptr_c -> B\n",
+ "C: Address=20542336 Content=A\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.4, Page No 185"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x=1234\n",
+ "print \"X: address=\"+str(id(x))+\" Content=\"+str(x)\n",
+ "\n",
+ "ptr_1=id(x)\n",
+ "print \"ptr_1: Address=\"+str(id(ptr_1))+\" Content=\"+str(ptr_1)\n",
+ "print \"*ptr_1 -> \"+str(ptr_1)\n",
+ "\n",
+ "ptr_2=id(x)\n",
+ "print \"ptr_2: Address=\"+str(id(ptr_2))+\" Content=\"+str(ptr_2)\n",
+ "print \"*ptr_2 -> \"+str(ptr_2)\n",
+ "\n",
+ "ptr_3=ptr_1\n",
+ "print \"ptr_3: Address=\"+str(id(ptr_3))+\" Content=\"+str(ptr_3)\n",
+ "print \"*ptr_3 -> \"+str(ptr_3)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "X: address=42206832 Content=1234\n",
+ "ptr_1: Address=42206928 Content=42206832\n",
+ "*ptr_1 -> 42206832\n",
+ "ptr_2: Address=42206916 Content=42206832\n",
+ "*ptr_2 -> 42206832\n",
+ "ptr_3: Address=42206928 Content=42206832\n",
+ "*ptr_3 -> 42206832\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour12.ipynb b/Teach_Yourself_C_in_24_Hours/hour12.ipynb new file mode 100755 index 00000000..276fbcc6 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour12.ipynb @@ -0,0 +1,311 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:b5e96d8861a1f6c8945a488114c3e560525b59b81ecaf015f068a3722502da99"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 12 : Understnading Arrays"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.1, Page No 191"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "list_int=range(10)\n",
+ "\n",
+ "for i in range(10):\n",
+ " list_int[i]=i+1\n",
+ " print \"list_int[\"+str(i)+\"] is initialized with \"+str(list_int[i])+\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "list_int[0] is initialized with 1\n",
+ "\n",
+ "list_int[1] is initialized with 2\n",
+ "\n",
+ "list_int[2] is initialized with 3\n",
+ "\n",
+ "list_int[3] is initialized with 4\n",
+ "\n",
+ "list_int[4] is initialized with 5\n",
+ "\n",
+ "list_int[5] is initialized with 6\n",
+ "\n",
+ "list_int[6] is initialized with 7\n",
+ "\n",
+ "list_int[7] is initialized with 8\n",
+ "\n",
+ "list_int[8] is initialized with 9\n",
+ "\n",
+ "list_int[9] is initialized with 10\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.2, Page No 193"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "list_int=range(10)\n",
+ "\n",
+ "total_byte= sys.getsizeof(int)*10\n",
+ "\n",
+ "print \"The size of int is \"+str(sys.getsizeof(int))+\" byte long\"\n",
+ "print \"The array of 10 ints has total \"+str(total_byte)+\" bytes\"\n",
+ "print \"The address of the first element: \"+str(id(list_int[0]))\n",
+ "print \"The address of the last element: \"+str(id(list_int[9]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The size of int is 436 byte long\n",
+ "The array of 10 ints has total 4360 bytes\n",
+ "The address of the first element: 21357036\n",
+ "The address of the last element: 21356928\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.3, Page No 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "list_int=range(10)\n",
+ "\n",
+ "for i in range(10):\n",
+ " list_int[i]=i+1\n",
+ "\n",
+ "ptr_int=list_int\n",
+ "print \"The start address of the array: \"+str(id(ptr_int[0]))\n",
+ "print \"The value of the first element: \"+str(ptr_int[0])\n",
+ "ptr_int=id(list_int[0])\n",
+ "print \"The address of the first element: \"+str(ptr_int)\n",
+ "print \"The value of the first element: \"+str(list_int[0])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The start address of the array: 20177376\n",
+ "The value of the first element: 1\n",
+ "The address of the first element: 20177376\n",
+ "The value of the first element: 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.4, Page No 196"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "array_ch=['H','e','l','l','o','!','\\0']\n",
+ "\n",
+ "for i in range(7):\n",
+ " print \"array_ch[\"+str(i)+\"] contains: \"+str(array_ch[i])\n",
+ " \n",
+ "'''Method I'''\n",
+ "print \"Put all elements together(Method I):\"\n",
+ "for ch in array_ch:\n",
+ " print ch,\n",
+ "\n",
+ "''' Method II'''\n",
+ "print \"\\nPut all elements together(Method II):\"\n",
+ "print \"\".join(array_ch)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "array_ch[0] contains: H\n",
+ "array_ch[1] contains: e\n",
+ "array_ch[2] contains: l\n",
+ "array_ch[3] contains: l\n",
+ "array_ch[4] contains: o\n",
+ "array_ch[5] contains: !\n",
+ "array_ch[6] contains: \u0000\n",
+ "Put all elements together(Method I):\n",
+ "H e l l o ! \u0000 \n",
+ "Put all elements together(Method II):\n",
+ "Hello!\u0000\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.5, Page No 198"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "array_ch=['C',' ','i','s',' ','p','o','w','e','r','f','u','l','!','\\0']\n",
+ "\n",
+ "i=0\n",
+ "while(array_ch[i]!='\\0'):\n",
+ " print \"\"+array_ch[i],\n",
+ " i=i+1\n",
+ "\n",
+ "print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C i s p o w e r f u l ! \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.6, Page No 200"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "two_dim=[[1,2,3,4,5],[10,20,30,40,50],[100,200,300,400,500]]\n",
+ "\n",
+ "for i in range(3):\n",
+ " print \"\\n\"\n",
+ " for j in range(5):\n",
+ " print \"%6u\" % two_dim[i][j],\n",
+ "print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ " 1 2 3 4 5 \n",
+ "\n",
+ " 10 20 30 40 50 \n",
+ "\n",
+ " 100 200 300 400 500 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.7, Page No 202"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "array_ch=['C',' ','i','s',' ','p','o','w','e','r','f','u','l','!','\\0']\n",
+ "\n",
+ "list_int=[[1,1,1],[2,2,8],[3,9,27],[4,16,64],[5,25,125],[6,36,216],[7,49,343]]\n",
+ "\n",
+ "print \"The size of array_ch[] is %d bytes.\\n\" %sys.getsizeof(array_ch)\n",
+ "print \"The size of list_int[][] is %d bytes.\\n\" %sys.getsizeof(list_int)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The size of array_ch[] is 96 bytes.\n",
+ "\n",
+ "The size of list_int[][] is 64 bytes.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour13.ipynb b/Teach_Yourself_C_in_24_Hours/hour13.ipynb new file mode 100755 index 00000000..6535b7e8 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour13.ipynb @@ -0,0 +1,261 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:cebcc94856d8ecdf1ae320585ba1942c4e6ccd608bed15be183214bd1392f3a2"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 13: Manipulating String"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 13.1, Page No 210"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "str1=['A',' ','s','t','r','i','n','g',' ','c','o','n','s','t','a','n','t']\n",
+ "str2= \"Another string constant\"\n",
+ "\n",
+ "i=0\n",
+ "for ch in str1:\n",
+ " print ch,\n",
+ "print \"\"\n",
+ "\n",
+ "for ch in str2:\n",
+ " print ch,\n",
+ "\n",
+ "print \"\"\n",
+ "\n",
+ "#there is no concept of pointer in Python\n",
+ "ptr_str=\"Assign a string to a pointer\"\n",
+ "for ele in ptr_str:\n",
+ " print ele,"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " A s t r i n g c o n s t a n t \n",
+ "A n o t h e r s t r i n g c o n s t a n t \n",
+ "A s s i g n a s t r i n g t o a p o i n t e r\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 13.2, Page No 212"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "str1=['A',' ','s','t','r','i','n','g',' ','c','o','n','s','t','a','n','t']\n",
+ "str2= \"Another string constant\"\n",
+ "ptr_str=\"Assign a string to a pointer\"\n",
+ "\n",
+ "print \"The length of str1 is: %d bytes\\n\" % len(str1)\n",
+ "print \"The length of str2 is: %d bytes\\n\" % len(str2)\n",
+ "print \"The length of the string assigned to a pointer is: %d bytes\" % len(ptr_str)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The length of str1 is: 17 bytes\n",
+ "\n",
+ "The length of str2 is: 23 bytes\n",
+ "\n",
+ "The length of the string assigned to a pointer is: 28 bytes\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 13.3, Page No 213"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "str1=\"Copy a string\"\n",
+ "str2=str1\n",
+ "str3=range(len(str1))\n",
+ "\n",
+ "for i in range(len(str1)):\n",
+ " str3[i]=str1[i]\n",
+ " \n",
+ "str3[i]='\\0'\n",
+ "print \"The content of str2 using strcpy: %s\" % str2\n",
+ "print \"The contect of str3 without using strcpy: %s\" % str3"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The content of str2 using strcpy: Copy a string\n",
+ "The contect of str3 without using strcpy: ['C', 'o', 'p', 'y', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', '\\x00']\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 13.4, Page No 216"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "str1 = range(80)\n",
+ "delt=ord('a')-ord('A')\n",
+ "\n",
+ "str1 = raw_input(\"Enter a string less than 80 characters: \\n\")\n",
+ "str1 = list(str1)\n",
+ "i=0\n",
+ "while(i!=len(str1)):\n",
+ " if((str1[i]>='a') and (str1[i]<='z')):\n",
+ " str1[i] = chr(ord(str1[i])-delt)\n",
+ " i=i+1\n",
+ "print \"The entered string is (in uppercase)\\n\"\n",
+ "print \"\".join(str1)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a string less than 80 characters: \n",
+ "This is a test.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The entered string is (in uppercase)\n",
+ "\n",
+ "THIS IS A TEST.\n"
+ ]
+ }
+ ],
+ "prompt_number": 60
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 13.5, Page No 218"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x=int(raw_input(\"Enter integer:\"))\n",
+ "y=int(raw_input(\"Enter integer:\"))\n",
+ "z=float(raw_input(\"Enter a floating-point number:\"))\n",
+ "str1=raw_input(\"Enter a string:\")\n",
+ "print \"Here are what you've entered\"\n",
+ "print \"%d \" % x,\n",
+ "print \"%d \" % y\n",
+ "print \"%f \" % z\n",
+ "print \"%s \" % str1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer:10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer:12345\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a floating-point number:1.234567\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a string:Test\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Here are what you've entered\n",
+ "10 12345 \n",
+ "1.234567 \n",
+ "Test \n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour14.ipynb b/Teach_Yourself_C_in_24_Hours/hour14.ipynb new file mode 100755 index 00000000..907bcb04 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour14.ipynb @@ -0,0 +1,169 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:dc372127172fa28ae076732c5c6607c2a4f0be3eed0d850c34c59df8b47971c6"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 14: Uderstanding Scope and Storage Classes"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.1, Page No 225"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "i=32\n",
+ "print \"Within the outer block: i=%d\" % i\n",
+ "#There are no direct blocks in Python. We will do it in different way\n",
+ "print \"Within the inner block\"\n",
+ "def fu():\n",
+ " j = 10\n",
+ " for i in range(11):\n",
+ " print \"i=\", i, \"j=\",j\n",
+ " j -= 1\n",
+ "fu()\n",
+ "print \"Within the outer block: i=%d\" % i"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Within the outer block: i=32\n",
+ "Within the inner block\n",
+ "i= 0 j= 10\n",
+ "i= 1 j= 9\n",
+ "i= 2 j= 8\n",
+ "i= 3 j= 7\n",
+ "i= 4 j= 6\n",
+ "i= 5 j= 5\n",
+ "i= 6 j= 4\n",
+ "i= 7 j= 3\n",
+ "i= 8 j= 2\n",
+ "i= 9 j= 1\n",
+ "i= 10 j= 0\n",
+ "Within the outer block: i=32\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.2, Page No 227"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Python does'nt provide block creating facility like using just {} with out any name hence i have implemented in this way\n",
+ "\n",
+ "def function_1():\n",
+ " x = 1234\n",
+ " y = 1.234567\n",
+ " print \"From fuction_1:\"\n",
+ " print \"x=\", x, \", y=\", y\n",
+ "\n",
+ "x = 4321\n",
+ "function_1()\n",
+ "print \"Within the main block:\" \n",
+ "print \"x=%d, y=%f\" %(x, y)\n",
+ "\n",
+ "y = 7.654321\n",
+ "function_1()\n",
+ "print \"Within the nested block:\"\n",
+ "print \"x=%d, y=%f\" %(x, y)\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "From fuction_1:\n",
+ "x= 1234 , y= 1.234567\n",
+ "Within the main block:\n",
+ "x=4321, y=7.654321\n",
+ "From fuction_1:\n",
+ "x= 1234 , y= 1.234567\n",
+ "Within the nested block:\n",
+ "x=4321, y=7.654321\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14.3, Page No 230"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "counter =0\n",
+ "def add_two(x,y):\n",
+ " global counter\n",
+ " counter = counter+1\n",
+ " print \"This is the function call of %d,\" % counter\n",
+ " return (x+y)\n",
+ "j = 5\n",
+ "for i in range(5):\n",
+ " print \"The addition of \"+ str(i) +\" and \"+str(j)+\" is \"+str(add_two(i,j))\n",
+ " j=j-1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "This is the function call of 1,\n",
+ "The addition of 0 and 5 is 5\n",
+ "This is the function call of 2,\n",
+ "The addition of 1 and 4 is 5\n",
+ "This is the function call of 3,\n",
+ "The addition of 2 and 3 is 5\n",
+ "This is the function call of 4,\n",
+ "The addition of 3 and 2 is 5\n",
+ "This is the function call of 5,\n",
+ "The addition of 4 and 1 is 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour15.ipynb b/Teach_Yourself_C_in_24_Hours/hour15.ipynb new file mode 100755 index 00000000..f05e986b --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour15.ipynb @@ -0,0 +1,185 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:6797b9444a439c70039a1e66c231ad4d494ff12505798711ee0fd43c452323b7"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 15: Working with Functions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 15.1, Page No 245"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def function_1(x,y):\n",
+ " print \"Within Function_1.\"\n",
+ " return (x+y)\n",
+ "def function_2(x,y):\n",
+ " print \"Within Function_2.\"\n",
+ " return (x+y)\n",
+ "\n",
+ "x1=80\n",
+ "y1=10\n",
+ "x2=float\n",
+ "y2=float\n",
+ "x2=100.123456\n",
+ "y2=10.123456\n",
+ "print \"Pass function_1 %d\" % x1,\n",
+ "print \" and %d\" % y1\n",
+ "print \"Function_1 returns %d\" % function_1(x1,y1)\n",
+ "print \"Pass function_2 %f\" % x2,\n",
+ "print \" and %f\" % y2\n",
+ "print \"Function_1 returns %f\" % function_2(x2,y2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Pass function_1 80 and 10\n",
+ "Within Function_1.\n",
+ "Function_1 returns 90\n",
+ "Pass function_2 100.123456 and 10.123456\n",
+ "Within Function_2.\n",
+ "Function_1 returns 110.246912\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 15.2, Page No 248"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import time\n",
+ "def GetDateTime():\n",
+ " print \"Within GetDateTime().\"\n",
+ " print \"Current date and time is %s\" % str(time.strftime(\"%a %b %d %X %Y\"))\n",
+ "\n",
+ "print \"Before the GetDateTime() function is called.\"\n",
+ "GetDateTime()\n",
+ "print \"After The GetDateTime() function is called.\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Before the GetDateTime() function is called.\n",
+ "Within GetDateTime().\n",
+ "Current date and time is Mon Nov 10 11:08:33 2014\n",
+ "After The GetDateTime() function is called.\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 15.3, Page No 253"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def AddDouble(*args):\n",
+ " result=float\n",
+ " result=0.0\n",
+ " print \"The number of arguments is \"+ str(len(args))\n",
+ " for i in range(len(args)):\n",
+ " result= result + args[i]\n",
+ " return result\n",
+ "\n",
+ "d1=float\n",
+ "d2=float\n",
+ "d3=float\n",
+ "d4=float\n",
+ "d1=1.5\n",
+ "d2=2.5\n",
+ "d3=3.5\n",
+ "d4=4.5\n",
+ "\n",
+ "print \"Given an argument: %2.1f\" % d1\n",
+ "print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1)\n",
+ "\n",
+ "print \"Given an argument: %2.1f\" % d1,\n",
+ "print \" and %2.1f\" % d2\n",
+ "print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1,d2)\n",
+ "\n",
+ "print \"Given an argument: %2.1f\" % d1,\n",
+ "print \", %2.1f\" % d2,\n",
+ "print \" and %2.1f\" % d3\n",
+ "print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1,d2,d3)\n",
+ "\n",
+ "print \"Given an argument: %2.1f\" % d1,\n",
+ "print \", %2.1f\" % d2,\n",
+ "print \", %2.1f\" % d3,\n",
+ "print \" and %2.1f\" % d4\n",
+ "print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1,d2,d3,d4)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Given an argument: 1.5\n",
+ "The number of arguments is 1\n",
+ "The result returned by AddDouble() is: 1.5 \n",
+ "\n",
+ "Given an argument: 1.5 and 2.5\n",
+ "The number of arguments is 2\n",
+ "The result returned by AddDouble() is: 4.0 \n",
+ "\n",
+ "Given an argument: 1.5 , 2.5 and 3.5\n",
+ "The number of arguments is 3\n",
+ "The result returned by AddDouble() is: 7.5 \n",
+ "\n",
+ "Given an argument: 1.5 , 2.5 , 3.5 and 4.5\n",
+ "The number of arguments is 4\n",
+ "The result returned by AddDouble() is: 12.0 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour16.ipynb b/Teach_Yourself_C_in_24_Hours/hour16.ipynb new file mode 100755 index 00000000..c9dab487 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour16.ipynb @@ -0,0 +1,426 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:f0f6a2ccafa5bf612c316f32caa438a56abd55c7d615f06f2f9b34b2ed02f28d"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 16: Applying Pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.1, Page No 260"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "ptr_ch=chr\n",
+ "ptr_int=int\n",
+ "ptr_db=float\n",
+ "\n",
+ "#Char pointer ptr_ch\n",
+ "print \" Corrent position of ptr_ch: %#x \" % id(ptr_ch)\n",
+ "print \" The position after ptr_ch+1 : %#x\" % (id(ptr_ch)+1)\n",
+ "print \" The position after ptr_ch+2 : %#x\" % (id(ptr_ch)+2)\n",
+ "print \" The position after ptr_ch+1 : %#x\" % (id(ptr_ch)+1)\n",
+ "print \" The position after ptr_ch+2 : %#x\\n\" % (id(ptr_ch)+2)\n",
+ "\n",
+ "print \" Corrent position of ptr_int: %#x \" % id(ptr_int)\n",
+ "print \" The position after ptr_int+1 : %#x\" % (id(ptr_int)+1)\n",
+ "print \" The position after ptr_int+2 : %#x\" % (id(ptr_int)+2)\n",
+ "print \" The position after ptr_int+1 : %#x\" % (id(ptr_int)+1)\n",
+ "print \" The position after ptr_int+2 : %#x\\n\" % (id(ptr_int)+2)\n",
+ "\n",
+ "print \" Corrent position of ptr_db: %#x \" % id(ptr_db)\n",
+ "print \" The position after ptr_db+1 : %#x\" % (id(ptr_db)+1)\n",
+ "print \" The position after ptr_db+2 : %#x\" % (id(ptr_db)+2)\n",
+ "print \" The position after ptr_db+1 : %#x\" % (id(ptr_db)+1)\n",
+ "print \" The position after ptr_db+2 : %#x\\n\" % (id(ptr_db)+2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Corrent position of ptr_ch: 0x12a30a8 \n",
+ " The position after ptr_ch+1 : 0x12a30a9\n",
+ " The position after ptr_ch+2 : 0x12a30aa\n",
+ " The position after ptr_ch+1 : 0x12a30a9\n",
+ " The position after ptr_ch+2 : 0x12a30aa\n",
+ "\n",
+ " Corrent position of ptr_int: 0x1e222010 \n",
+ " The position after ptr_int+1 : 0x1e222011\n",
+ " The position after ptr_int+2 : 0x1e222012\n",
+ " The position after ptr_int+1 : 0x1e222011\n",
+ " The position after ptr_int+2 : 0x1e222012\n",
+ "\n",
+ " Corrent position of ptr_db: 0x1e220be0 \n",
+ " The position after ptr_db+1 : 0x1e220be1\n",
+ " The position after ptr_db+2 : 0x1e220be2\n",
+ " The position after ptr_db+1 : 0x1e220be1\n",
+ " The position after ptr_db+2 : 0x1e220be2\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.2, Page No 263"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "ptr_int1=int\n",
+ "ptr_int2=int\n",
+ "\n",
+ "print \"The position of ptr_int1: %#x\" % id(ptr_int1)\n",
+ "ptr_int2=id(ptr_int1)+5\n",
+ "print \"The position of ptr_int2=id(ptr_int1)+5 :%#x\" % ptr_int2\n",
+ "print \"The subtraction of ptr_int2-ptr_int1 :%#x\" % (ptr_int2 - id(ptr_int1))\n",
+ "\n",
+ "ptr_int2=id(ptr_int1)-5\n",
+ "print \"The position of ptr_int2=id(ptr_int1)-5 :%#x\" % ptr_int2\n",
+ "print \"The subtraction of ptr_int2-ptr_int1 :%#x\" % (ptr_int2 - id(ptr_int1))\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The position of ptr_int1: 0x1e222010\n",
+ "The position of ptr_int2=id(ptr_int1)+5 :0x1e222015\n",
+ "The subtraction of ptr_int2-ptr_int1 :0x5\n",
+ "The position of ptr_int2=id(ptr_int1)-5 :0x1e22200b\n",
+ "The subtraction of ptr_int2-ptr_int1 :-0x5\n"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.3, Page No 265"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "str1=\"It's a string!\"\n",
+ "ptr_str=chr\n",
+ "list1=[1,2,3,4,5]\n",
+ "ptr_int=int\n",
+ "str1=list(str1)\n",
+ "ptr_str=str1\n",
+ "print \"Before the change, str1 contains: \",\n",
+ "print \"\".join(str1)\n",
+ "print \"Before the change, str1[5] contains : %c\" % str1[5]\n",
+ "str1[5]='A'\n",
+ "print \"After The change, str1[5] contains : %c\" % str1[5]\n",
+ "print \"After The change, str1 contains: \",\n",
+ "print \"\".join(str1)\n",
+ "\n",
+ "ptr_int=list1\n",
+ "print \"Before The change, list1[2] contains: %d\" % list1[2]\n",
+ "list1[2]=-3\n",
+ "print \"After the change, list1[2] contains : %d\" % list1[2]\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Before the change, str1 contains: It's a string!\n",
+ "Before the change, str1[5] contains : a\n",
+ "After The change, str1[5] contains : A\n",
+ "After The change, str1 contains: It's A string!\n",
+ "Before The change, list1[2] contains: 3\n",
+ "After the change, list1[2] contains : -3\n"
+ ]
+ }
+ ],
+ "prompt_number": 32
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.4, Page No 266"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def AddThree(list1):\n",
+ " result=0\n",
+ " for i in range(len(list1)):\n",
+ " result=result+list1[i]\n",
+ " return result\n",
+ "\n",
+ "sum1=0\n",
+ "listt=[x for x in range(3)]\n",
+ "\n",
+ "listt[0]=int(raw_input(\"Enter integer1 :\"))\n",
+ "listt[1]=int(raw_input(\"Enter integer2 :\"))\n",
+ "listt[2]=int(raw_input(\"Enter integer3 :\"))\n",
+ "sum1=AddThree(listt)\n",
+ "print \"The sum of three integers is: %d\" % sum1\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer1 :10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer2 :20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer3 :30\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of three integers is: 60\n"
+ ]
+ }
+ ],
+ "prompt_number": 42
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.5, Page No 268"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "def ChPrint(c):\n",
+ " print \"%s\" % c\n",
+ "def DataAdd(list1,max1):\n",
+ " sum1=0\n",
+ " for i in range(max1):\n",
+ " sum1 += list1[i]\n",
+ " return sum1\n",
+ "\n",
+ "str1=\"It's a string!\"\n",
+ "ptr_str=chr\n",
+ "listt=[1,2,3,4,5]\n",
+ "ptr_int=int\n",
+ "\n",
+ "ptr_str=str1\n",
+ "ChPrint(ptr_str)\n",
+ "ChPrint(str1)\n",
+ "\n",
+ "ptr_int=listt\n",
+ "\n",
+ "print \"The sum returned by DataAdd() : %d\" % DataAdd(ptr_int,5)\n",
+ "print \"The sum returned by DataAdd() : %d\" % DataAdd(listt,5)\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "It's a string!\n",
+ "It's a string!\n",
+ "The sum returned by DataAdd() : 15\n",
+ "The sum returned by DataAdd() : 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 47
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.6, Page No 270"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "def DataAdd(list1,max1,max2):\n",
+ " sum1=0\n",
+ " for i in range(max1):\n",
+ " for j in range(max2):\n",
+ " sum1 = sum1 + list1[i][j]\n",
+ " return sum1\n",
+ "def DataAdd2(list2,max1,max2):\n",
+ " sum2=0\n",
+ " for i in range(max1):\n",
+ " for j in range(max2):\n",
+ " sum2 = sum2 + list2[i][j]\n",
+ " return sum2\n",
+ "\n",
+ "listt=[[1,2],[3,4],[5,5],[4,3],[2,1]]\n",
+ "ptr_int=int\n",
+ "print \"The sum returned by DataAdd() : %d\" % DataAdd(listt,5,2)\n",
+ "ptr_int=listt\n",
+ "print \"The sum returned by DataAdd() : %d\" % DataAdd2(ptr_int,5,2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum returned by DataAdd() : 30\n",
+ "The sum returned by DataAdd() : 30\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.7, Page No 272"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def StrPrint1(str1,size):\n",
+ " for i in range(size):\n",
+ " print \"%s\" % str1[i]\n",
+ "def StrPrint2(str2):\n",
+ " print \"%s\" % str2\n",
+ " \n",
+ "str1=[\"There's music in the sighing of a reed;\",\"There's music in the gushing of a rill;\",\"There's music in all things if men had ears;\",\"There earth is but an echo of the spheres.\\n\"]\n",
+ "size=4\n",
+ "\n",
+ "StrPrint1(str1,size)\n",
+ "for i in range(size):\n",
+ " StrPrint2(str1[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "There's music in the sighing of a reed;\n",
+ "There's music in the gushing of a rill;\n",
+ "There's music in all things if men had ears;\n",
+ "There earth is but an echo of the spheres.\n",
+ "\n",
+ "There's music in the sighing of a reed;\n",
+ "There's music in the gushing of a rill;\n",
+ "There's music in all things if men had ears;\n",
+ "There earth is but an echo of the spheres.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.8, Page No 274"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "def StrPrint(str1):\n",
+ " print \"%s\" % str1\n",
+ "\n",
+ "str1=\"Pointing to a function\"\n",
+ "ptr=StrPrint\n",
+ "if(not(ptr(str1))):\n",
+ " print \"Done.\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Pointing to a function\n",
+ "Done.\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour17.ipynb b/Teach_Yourself_C_in_24_Hours/hour17.ipynb new file mode 100755 index 00000000..94c20ee8 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour17.ipynb @@ -0,0 +1,267 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:d4eeb6d301f04950d37e1192961f4665d398ce20c6e4b34ea5228762dfddaffa"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 17: Allocating Memory"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 17.1, Page No 281"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner\n",
+ "def StrCopy(str1,str2):\n",
+ " str1=list(str1)\n",
+ " str2=list(str2)\n",
+ " for i in range(len(str1)):\n",
+ " str2[i]=str1[i]\n",
+ " str2[i]='\\0'\n",
+ "\n",
+ "str1=\"Use malloc() to allocate memory\"\n",
+ "ptr_str=str\n",
+ "ptr_str= str1\n",
+ "\n",
+ "if(ptr_str!= None):\n",
+ " StrCopy(str1,ptr_str)\n",
+ " print \"The string pointed to by ptr_str is : %s\" % ptr_str\n",
+ "else:\n",
+ " print \"malloc() function failed.\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The string pointed to by ptr_str is : Use malloc() to allocate memory\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 17.2, Page No 283"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner\n",
+ "def DataMultiply(max1,ptr_int):\n",
+ " for i in range(max1):\n",
+ " for j in range(max1):\n",
+ " ptr_int=(i+1)*(j+1)\n",
+ " return ptr_int\n",
+ "def TablePrint(max1,ptr_int):\n",
+ " print \"The Multiplication table of %d is:\" % max1\n",
+ " print \"\"\n",
+ " for i in range(max1):\n",
+ " print \"%4d\" % (i+1),\n",
+ " print \"\"\n",
+ " for i in range(max1):\n",
+ " print \"----\",\n",
+ " print\"\"\n",
+ " for i in range(max1):\n",
+ " print \"%d|\" % (i+1),\n",
+ " for j in range(max1):\n",
+ " print \"%3d\" % ptr_int,\n",
+ " print\"\"\n",
+ "\n",
+ "ptr_int=int\n",
+ "ptr_int=0\n",
+ "key='c'\n",
+ "max1=0\n",
+ "termination=0\n",
+ "while(key != 'x'):\n",
+ " max1=int(raw_input(\"Enter a single digit number: \"))\n",
+ " if(ptr_int != None):\n",
+ " print ptr_int\n",
+ " ptr_int=DataMultiply(max1,ptr_int)\n",
+ " print ptr_int\n",
+ " TablePrint(max1,ptr_int)\n",
+ " else:\n",
+ " print \"Malloc() function failed.\"\n",
+ " termination=1\n",
+ " key='x'\n",
+ " key=raw_input(\"\\n\\nPress x key to quit; other key to continue.\")\n",
+ "print\"\\nBYE\\n\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a single digit number: 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0\n",
+ "3\n",
+ "The Multiplication table of 3 is:\n",
+ "\n",
+ " 1 2 3 \n",
+ "---- ---- ---- \n",
+ "1| 3 3 3 \n",
+ "2| 3 3 3 \n",
+ "3| 3 3 3 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "Press x key to quit; other key to continue.x\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "BYE\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 17.3, Page No 287"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner\n",
+ "ptr1=float\n",
+ "ptr2=float\n",
+ "ptr1=0.0\n",
+ "ptr2=0.0\n",
+ "termination=1\n",
+ "n=5\n",
+ "\n",
+ "if(ptr1 == None):\n",
+ " print \"Malloc() failed.\"\n",
+ "elif(ptr2 == None):\n",
+ " print \"Calloc() failed.\"\n",
+ "else:\n",
+ " for i in range(n):\n",
+ " print \"ptr1[%d]= %5.2f, ptr2[%d]= %5.2f\" %(i,(ptr1+i),i,(ptr2+i))\n",
+ " termination=0"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "ptr1[0]= 0.00, ptr2[0]= 0.00\n",
+ "ptr1[1]= 1.00, ptr2[1]= 1.00\n",
+ "ptr1[2]= 2.00, ptr2[2]= 2.00\n",
+ "ptr1[3]= 3.00, ptr2[3]= 3.00\n",
+ "ptr1[4]= 4.00, ptr2[4]= 4.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 17.4, Page No 289"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner\n",
+ "def StrCopy(str1,str2):\n",
+ " str1=list(str1)\n",
+ " str2=list(str1)\n",
+ " for i in range(len(str1)-1):\n",
+ " str2[i]=str1[i]\n",
+ " str2[i]='\\0'\n",
+ " return str2\n",
+ "\n",
+ "str1=[\"There's music in the sighing of a reed;\",\"There's music in the gushing of a rill;\",\"There's music in all things if men had ears;\",\"There earth is but an echo of the spheres.\\n\"]\n",
+ "termination=0\n",
+ "if(ptr == None):\n",
+ " print \"Malloc() failed.\"\n",
+ " termination=1\n",
+ "else:\n",
+ " ptr1=StrCopy(str1[0],ptr)\n",
+ " print \"\".join(ptr1)\n",
+ " for i in range(1,4):\n",
+ " if (ptr == None):\n",
+ " print \"realloc() failed.\"\n",
+ " termination=1\n",
+ " i=4\n",
+ " else:\n",
+ " ptr1=StrCopy(str1[i],ptr)\n",
+ " print \"\".join(ptr1)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "There's music in the sighing of a ree\u0000;\n",
+ "There's music in the gushing of a ril\u0000;\n",
+ "There's music in all things if men had ear\u0000;\n",
+ "There earth is but an echo of the spheres\u0000\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour18.ipynb b/Teach_Yourself_C_in_24_Hours/hour18.ipynb new file mode 100755 index 00000000..f2eaf316 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour18.ipynb @@ -0,0 +1,287 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:a0a1c93dba0a6936203c3f01d39f2697247a7b54141760d78392371a2c5620f6"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 18: Using Special Data Types and Functions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 18.1, Page No 297"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n",
+ "def enum(*sequential, **named):\n",
+ " enums = dict(zip(sequential, range(len(sequential))), **named)\n",
+ " return type('Enum', (), enums)\n",
+ "\n",
+ "\n",
+ "language = enum(human=100,animal=50,computer=51)\n",
+ "\n",
+ "days = enum('SUN','MON','TUE','WED','THU','FRI','SAT')\n",
+ "\n",
+ "print \"human=%d, animal=%d, computer=%d\" % (language.human,language.animal,language.computer)\n",
+ "print \"SUN: %d\" % days.SUN\n",
+ "print \"MON: %d\" % days.MON\n",
+ "print \"TUE: %d\" % days.TUE\n",
+ "print \"WED: %d\" % days.WED\n",
+ "print \"THU: %d\" % days.THU\n",
+ "print \"FRI: %d\" % days.FRI\n",
+ "print \"SAT: %d\" % days.SAT"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "human=100, animal=50, computer=51\n",
+ "SUN: 0\n",
+ "MON: 1\n",
+ "TUE: 2\n",
+ "WED: 3\n",
+ "THU: 4\n",
+ "FRI: 5\n",
+ "SAT: 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 18.2, Page No 298"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n",
+ "def enum(*sequential, **named):\n",
+ " enums = dict(zip(sequential, range(len(sequential))), **named)\n",
+ " return type('Enum', (), enums)\n",
+ "\n",
+ "units= enum(penny=1,nickel=5,dime=10,quarter=25,dollar=100)\n",
+ "money_unit=[units.dollar,units.quarter,units.dime,units.nickel,units.penny]\n",
+ "unit_name=[\"dollar(s)\",\"quarter(s)\",\"dime(s)\",\"nickel(s)\",\"penny(s)\"]\n",
+ "\n",
+ "cent=int(raw_input(\"Enter a monetary value in cents:\"))\n",
+ "\n",
+ "print \"Which is equivalent to:\"\n",
+ "tmp=0\n",
+ "for i in range(5):\n",
+ " tmp=cent / money_unit[i]\n",
+ " cent = cent-(tmp*money_unit[i])\n",
+ " if(tmp):\n",
+ " print \"%d %s\" % (tmp,unit_name[i]),"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a monetary value in cents:141\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Which is equivalent to:\n",
+ "1 dollar(s) 1 quarter(s) 1 dime(s) 1 nickel(s) 1 penny(s)\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 18.3, Page No 301"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n",
+ "def enum(*sequential, **named):\n",
+ " enums = dict(zip(sequential, range(len(sequential))), **named)\n",
+ " return type('Enum', (), enums)\n",
+ "\n",
+ "constants=enum(ITEM_NUM=3,DELT=(ord('a')-ord('A')))\n",
+ "\n",
+ "def Convert2Upper(str1,str2):\n",
+ " str1=list(str1)\n",
+ " str2=list(i for i in range(len(str1)+1))\n",
+ " for i in range(len(str1)):\n",
+ " if ((ord(str1[i])>=97) and (ord(str1[i])<=122)):\n",
+ " str2[i]=chr(ord(str1[i])-constants.DELT)\n",
+ " else:\n",
+ " str2[i]=str1[i]\n",
+ " str2[i+1]='\\0'\n",
+ " return str2\n",
+ " \n",
+ "moon=[\"Whatever we wear\",\"we become beautiful\",\"moon viewing!\"]\n",
+ "str1=\"\"\n",
+ "term=0\n",
+ "str3=[\"\",\"\",\"\"]\n",
+ "\n",
+ "for i in range(constants.ITEM_NUM):\n",
+ " if(str1==None):\n",
+ " print \"malloc() failed.\"\n",
+ " term=1\n",
+ " i=constants.ITEM_NUM\n",
+ " str1=Convert2Upper(moon[i],str1)\n",
+ " print moon[i]\n",
+ " str3[i]=str1\n",
+ "print \"\"\n",
+ "for i in range(len(str3)):\n",
+ " print \"\".join(str3[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Whatever we wear\n",
+ "we become beautiful\n",
+ "moon viewing!\n",
+ "\n",
+ "WHATEVER WE WEAR\u0000\n",
+ "WE BECOME BEAUTIFUL\u0000\n",
+ "MOON VIEWING!\u0000\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 18.4, Page No 303"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n",
+ "def enum(*sequential, **named):\n",
+ " enums = dict(zip(sequential, range(len(sequential))), **named)\n",
+ " return type('Enum', (), enums)\n",
+ "\n",
+ "con=enum(MIN_NUM=0,MAX_NUM=100)\n",
+ "\n",
+ "def fRecur(n):\n",
+ " if(n==con.MIN_NUM):\n",
+ " return 0\n",
+ " return (fRecur(n-1)+n)\n",
+ "\n",
+ "sum1=sum2=0\n",
+ "for i in range(1,(con.MAX_NUM)+1):\n",
+ " sum1+=i\n",
+ " sum2=fRecur(con.MAX_NUM)\n",
+ "\n",
+ "print \"The value of sum1 is %d.\" % sum1\n",
+ "print \"The value returned by fRecur() is %d.\" % sum2"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The value of sum1 is 5050.\n",
+ "The value returned by fRecur() is 5050.\n"
+ ]
+ }
+ ],
+ "prompt_number": 34
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 18.5, Page No 306"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#The output is correct as it takes the CMD LINES from respective terminal.\n",
+ "import sys\n",
+ "print \"The value received by argc is \" ,len(sys.argv),\".\\n\"\n",
+ "print \"There are \",len(sys.argv), \"command-line arguments passed to main().\\n\"\n",
+ "print \"The first command-line argument is: \",sys.argv[0],\"\\n\"\n",
+ "print \"The rest of the command-line arguments are:\\n\"\n",
+ "for i in range(1,len(sys.argv)):\n",
+ " print sys.argv[i]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The value received by argc is 8 .\n",
+ "\n",
+ "There are 8 command-line arguments passed to main().\n",
+ "\n",
+ "The first command-line argument is: -c \n",
+ "\n",
+ "The rest of the command-line arguments are:\n",
+ "\n",
+ "-f\n",
+ "C:\\Users\\Vaibhav\\.ipython\\profile_default\\security\\kernel-683917c3-368a-425a-93a3-9782f6d369b5.json\n",
+ "--IPKernelApp.parent_appname='ipython-notebook'\n",
+ "--profile-dir\n",
+ "C:\\Users\\Vaibhav\\.ipython\\profile_default\n",
+ "--interrupt=852\n",
+ "--parent=1008\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour19.ipynb b/Teach_Yourself_C_in_24_Hours/hour19.ipynb new file mode 100755 index 00000000..e96f1303 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour19.ipynb @@ -0,0 +1,531 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:054e81c7d22cc52697211ef4bdf39c086b00ba90e5528d8b310b3644dab0674b"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 19 : Understanding Structures"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 19.1, Page No 316"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class computer:\n",
+ " def __init__(self, cost, year, cpu_speed, cpu_type):\n",
+ " self.cost = cost\n",
+ " self.year = year\n",
+ " self.cpu_speed = cpu_speed\n",
+ " self.cpu_type = cpu_type\n",
+ "if __name__ == '__main__':\n",
+ " cpu_type = raw_input(\"The type of CPU inside your computer: \")\n",
+ " cpu_speed = int(raw_input(\"The speed(MHz) of the CPU?: \"))\n",
+ " year = int(raw_input(\"The year your computer was made?: \"))\n",
+ " cost = float(raw_input(\"How much you paid for the computer?: \"))\n",
+ " computer(cost,year,cpu_speed,cpu_type)\n",
+ " print \"Hear are what you have entered\"\n",
+ " print \"Year: \",year,\"\\n\"\n",
+ " print \"Cost: \",cost,\"\\n\"\n",
+ " print \"CPU type: \",cpu_type,\"\\n\"\n",
+ " print \"CPU speed: \",cpu_speed,\"MHz\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The type of CPU inside your computer: Pentium\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The speed(MHz) of the CPU?: 100\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The year your computer was made?: 1996\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How much you paid for the computer?: 1234.56\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hear are what you have entered\n",
+ "Year: 1996 \n",
+ "\n",
+ "Cost: 1234.56 \n",
+ "\n",
+ "CPU type: Pentium \n",
+ "\n",
+ "CPU speed: 100 MHz\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 19.2, Page No 318"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class employee:\n",
+ " def __init__(self,id1,name):\n",
+ " self.id1 = id1\n",
+ " self.name = name\n",
+ "if __name__ == '__main__':\n",
+ " info = employee(1,\"B.Smith\")\n",
+ " print \"Here is a sample: \\n\"\n",
+ " print \"Employee Name : \",info.name,\"\\n\"\n",
+ " print \"Employee ID : \",info.id1\n",
+ " info.name = raw_input(\"What's your name?: \")\n",
+ " info.id1 = int(raw_input(\"What's your ID number: \"))\n",
+ " print \"\\nHere are what you entered: \\n\"\n",
+ " print \"Name: \",info.name,\"\\n\"\n",
+ " print \"Id: \",info.id1,\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Here is a sample: \n",
+ "\n",
+ "Employee Name : B.Smith \n",
+ "\n",
+ "Employee ID : 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What's your name?: T. Zhang\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What's your ID number: 1234\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Here are what you entered: \n",
+ "\n",
+ "Name: T. Zhang \n",
+ "\n",
+ "Id: 1234 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 19.3, Page No 320"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class computer:\n",
+ " def __init__(self, cost, year, cpu_speed, cpu_type):\n",
+ " self.cost = cost\n",
+ " self.year = year\n",
+ " self.cpu_speed = cpu_speed\n",
+ " self.cpu_type = cpu_type\n",
+ "def DataReceiver(s):\n",
+ " cpu_type = raw_input(\"The type of CPU inside your computer: \")\n",
+ " cpu_speed = int(raw_input(\"The speed(MHz) of the CPU?: \"))\n",
+ " year = int(raw_input(\"The year your computer was made?: \"))\n",
+ " cost = float(raw_input(\"How much you paid for the computer?: \"))\n",
+ " s = computer(cost,year,cpu_speed,cpu_type)\n",
+ " return s\n",
+ "if __name__ == '__main__':\n",
+ " model = DataReceiver(model)\n",
+ " print \"Hear are what you have entered\"\n",
+ " print \"Year: \",model.year,\"\\n\"\n",
+ " print \"Cost: \",model.cost,\"\\n\"\n",
+ " print \"CPU type: \",model.cpu_type,\"\\n\"\n",
+ " print \"CPU speed: \",model.cpu_speed ,\"MHz\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The type of CPU inside your computer: Pentium\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The speed(MHz) of the CPU?: 100\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The year your computer was made?: 1996\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How much you paid for the computer?: 1234.56\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hear are what you have entered\n",
+ "Year: 1996 \n",
+ "\n",
+ "Cost: 1234.56 \n",
+ "\n",
+ "CPU type: Pentium \n",
+ "\n",
+ "CPU speed: 100 MHz\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 19.4, Page No 322"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no pointer in python\n",
+ "class computer:\n",
+ " def __init__(self, cost, year, cpu_speed, cpu_type):\n",
+ " self.cost = cost\n",
+ " self.year = year\n",
+ " self.cpu_speed = cpu_speed\n",
+ " self.cpu_type = cpu_type\n",
+ "def DataReceiver(s):\n",
+ " cpu_type = raw_input(\"The type of CPU inside your computer: \")\n",
+ " cpu_speed = int(raw_input(\"The speed(MHz) of the CPU?: \"))\n",
+ " year = int(raw_input(\"The year your computer was made?: \"))\n",
+ " cost = float(raw_input(\"How much you paid for the computer?: \"))\n",
+ " s = computer(cost,year,cpu_speed,cpu_type)\n",
+ " return s\n",
+ "if __name__ == '__main__':\n",
+ " model = DataReceiver(model)\n",
+ " print \"Hear are what you have entered\"\n",
+ " print \"Year: \",model.year,\"\\n\"\n",
+ " print \"Cost: \",model.cost,\"\\n\"\n",
+ " print \"CPU type: \",model.cpu_type,\"\\n\"\n",
+ " print \"CPU speed: \",model.cpu_speed ,\"MHz\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The type of CPU inside your computer: Pentium\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The speed(MHz) of the CPU?: 100\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The year your computer was made?: 1996\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How much you paid for the computer?: 1234.56\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hear are what you have entered\n",
+ "Year: 1996 \n",
+ "\n",
+ "Cost: 1234.56 \n",
+ "\n",
+ "CPU type: Pentium \n",
+ "\n",
+ "CPU speed: 100 MHz\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 19.5, Page No 325"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class haiku:\n",
+ " def __init__(self,start_year,end_year,author,str1,str2,str3):\n",
+ " self.start_year = start_year\n",
+ " self.end_year = end_year\n",
+ " self.author = author\n",
+ " self.str1 = str1\n",
+ " self.str2 = str2\n",
+ " self.str3 = str3\n",
+ "def DataDisplay(ptr_s):\n",
+ " print ptr_s.str1\n",
+ " print ptr_s.str2\n",
+ " print ptr_s.str3\n",
+ " print \" ---\",ptr_s.author\n",
+ " print \"(\",ptr_s.start_year,\",\",ptr_s.end_year,\")\"\n",
+ "if __name__ == '__main__':\n",
+ " poem = []\n",
+ " poem.append(haiku(1641,1716,\"Sodo\",\"Leading me along\",\"my shadow goes back home\",\"from looking at the moon\"))\n",
+ " poem.append(haiku(1729,1781,\"Chora\",\"A strom wind blows\",\"out from among the grasses\",\"the full moon grows\"))\n",
+ " for i in range(len(poem)):\n",
+ " DataDisplay(poem[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Leading me along\n",
+ "my shadow goes back home\n",
+ "from looking at the moon\n",
+ " --- Sodo\n",
+ "( 1641 , 1716 )\n",
+ "A strom wind blows\n",
+ "out from among the grasses\n",
+ "the full moon grows\n",
+ " --- Chora\n",
+ "( 1729 , 1781 )\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 19.6, Page No 327"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class department:\n",
+ " def __init__(self,code,dname,position):\n",
+ " self.code = code\n",
+ " self.dname = dname\n",
+ " self.position = position\n",
+ "class employee:\n",
+ " def __init__(self,code,dname,position,id1,name):\n",
+ " self.DPT = department(code,dname,position)\n",
+ " self.id1 = id1\n",
+ " self.name = name\n",
+ "def InfoDisplay(ptr):\n",
+ " print \"Name: \",ptr.name,\"\\n\"\n",
+ " print \"ID #: \",ptr.id1,\"\\n\"\n",
+ " print \"Dept. name: \",ptr.DPT.dname,\"\\n\"\n",
+ " print \"Dept. code: \",ptr.DPT.code,\"\\n\"\n",
+ " print \"Your Position : \",ptr.DPT.position,\"\\n\"\n",
+ "def InfoEnter(ptr):\n",
+ " print \"Please enter your information:\\n\"\n",
+ " name = raw_input(\"Your name: \")\n",
+ " position = raw_input(\"Your position: \")\n",
+ " name = raw_input(\"Dept. name: \")\n",
+ " code = raw_input(\"Dept. code: \")\n",
+ " id1 = raw_input(\"Your employee ID #: \")\n",
+ "if __name__ == '__main__':\n",
+ " info = employee(\"1\",\"marketing\",\"manager\",1,\"B.Smith\")\n",
+ " print \"\\nHere is a sample\"\n",
+ " InfoDisplay(info)\n",
+ " InfoEnter(info)\n",
+ " print \"\\nHere are what you entered\"\n",
+ " InfoDisplay(info)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " \n",
+ "Here is a sample\n",
+ "Name: B.Smith \n",
+ "\n",
+ "ID #: 1 \n",
+ "\n",
+ "Dept. name: marketing \n",
+ "\n",
+ "Dept. code: 1 \n",
+ "\n",
+ "Your Position : manager \n",
+ "\n",
+ "Please enter your information:\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your name: T. Zhang\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your position: Engineer\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Dept. name: R&D\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Dept. code: 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your employee ID #: 1234\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Here are what you entered\n",
+ "Name: B.Smith \n",
+ "\n",
+ "ID #: 1 \n",
+ "\n",
+ "Dept. name: marketing \n",
+ "\n",
+ "Dept. code: 1 \n",
+ "\n",
+ "Your Position : manager \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour2.ipynb b/Teach_Yourself_C_in_24_Hours/hour2.ipynb new file mode 100755 index 00000000..8c6cef05 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour2.ipynb @@ -0,0 +1,50 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:aa0169689b73d81c82a8f62ed4cf3b03d328c0e235d7ff4fff52cc0bebc646cc"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 2: Writing Your first C program"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.1, Page No.28"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print \"Howdy, neighbout! This is my first C Program.\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Howdy, neighbout! This is my first C Program.\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour20.ipynb b/Teach_Yourself_C_in_24_Hours/hour20.ipynb new file mode 100755 index 00000000..b99451c6 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour20.ipynb @@ -0,0 +1,459 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:d3ec56f0fb1e7a95eb53ee17745721c0f8fa66da6256b85d8eb402b33b736e2d" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Hour 20 : Understanding Unions" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 20.1, Page No 335" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class menu:\n", + " def __init__(self,name,price):\n", + " self.name = name\n", + " self.price = price\n", + "if __name__ == '__main__':\n", + " print \"The content assigned to the union separately:\\n\"\n", + " dish = menu(\"Sweet and Sour Chicken\",9.95)\n", + " print \"Dish Name: \",dish.name\n", + " print \"Dish Price: \",dish.price" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The content assigned to the union separately:\n", + "\n", + "Dish Name: Sweet and Sour Chicken\n", + "Dish Price: 9.95\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 20.2, Page No 338" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class employee:\n", + " def __init__(self,start_year,dpt_code,id_number):\n", + " self.start_year = start_year\n", + " self.dpt_code = dpt_code\n", + " self.id_number = id_number\n", + "if __name__ == '__main__':\n", + " info = employee(1997,8,1234)\n", + " print \"Start Year: \",info.start_year,\"\\n\"\n", + " print \"Dpt. Code: \",info.dpt_code,\"\\n\"\n", + " print \"ID Number: \",info.id_number,\"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Start Year: 1997 \n", + "\n", + "Dpt. Code: 8 \n", + "\n", + "ID Number: 1234 \n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 20.3, page no. 340" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "class u:\n", + " def __init__(self,x,y):\n", + " self.x = x\n", + " self.y = y\n", + "class s:\n", + " def __init__(self,x,y):\n", + " self.x = x\n", + " self.y = y\n", + "if __name__ == '__main__':\n", + " a_union = u(10,20)\n", + " a_struct = s(10,20)\n", + " #print \"The size of double: \",sys.getsizeof(double),\"byte\\n\" Double is not in python\n", + " print \"The size of int: \",sys.getsizeof(int),\"byte\\n\"\n", + " print \"The size of union: \",sys.getsizeof(a_union),\"byte\\n\"\n", + " print \"The size of struct: \",sys.getsizeof(a_struct),\"byte\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The size of int: 872 byte\n", + "\n", + "The size of union: 72 byte\n", + "\n", + "The size of struct: 72 byte\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 20.4, Page No 341" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class u:\n", + " ch = [0, 0]\n", + " num = 0\n", + "\n", + "def UnionInitialize(val):\n", + " val.ch[0] = 'H'\n", + " val.ch[1] = 'I'\n", + " return val.ch\n", + "if __name__ == '__main__':\n", + " val = u()\n", + " x = UnionInitialize(val)\n", + " print \"The two characters held by the union:\\n\"\n", + " print x[0],\"\\n\"\n", + " print x[1],\"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The two characters held by the union:\n", + "\n", + "H \n", + "\n", + "I \n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 20.5, Page No 344" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class survey:\n", + " def __init__(self,name,c_d_p,age,hour_per_week):\n", + " self.name = name\n", + " self.c_d_p = c_d_p\n", + " self.age = age\n", + " self.hour_per_week = hour_per_week\n", + " class provider:\n", + " def __init__(self,cable_company,dish_company):\n", + " self.cable_company = cable_company\n", + " self.dish_company = dish_company\n", + "def DataEnter(ptr):\n", + " is_yes = raw_input(\"Are you using cable at home? (Yes or No): \")\n", + " if is_yes == 'Y' or is_yes == 'y':\n", + " ptr.provider.cable_company = raw_input(\"Enter the cable company name: \")\n", + " ptr.c_d_p = 'c'\n", + " else:\n", + " is_yes = raw_input(\"Are you using a satellite dish? (Yes or No): \")\n", + " if is_yes == 'Y' or is_yes == 'y':\n", + " ptr.provider.dish_company = raw_input(\"Enter the satellite dish company name: \")\n", + " ptr.c_d_p = 'd'\n", + " else:\n", + " ptr.c_d_p = 'p'\n", + " ptr.name = raw_input(\"Please enter your name: \")\n", + " ptr.age = int(raw_input(\"Your age : \"))\n", + " ptr.hour_per_week = int(raw_input(\"How many hours you spend on watching TV per week: \"))\n", + "def DataDisplay(ptr):\n", + " print \"\\nHere\u2019s what you\u2019ve entered:\\n\"\n", + " print \"Name: \",ptr.name,\"\\n\"\n", + " print \"Age: \",ptr.age,\"\\n\"\n", + " print \"Hour per week: \",ptr.hour_per_week,\"\\n\"\n", + " if ptr.c_d_p == 'c':\n", + " print \"Your cable company is: \",ptr.provider.cable_company,\"\\n\"\n", + " elif ptr.c_d_p == 'd':\n", + " print \"Your satellite dish company is: \",ptr.provider.dish_company,\"\\n\"\n", + " else:\n", + " print \"You don\u2019t have cable or a satellite dish.\\n\"\n", + " print \"\\nThanks and Bye!\\n\"\n", + "if __name__ == '__main__':\n", + " tv = survey(\"\",\"\",\"\",\"\")\n", + " DataEnter(tv)\n", + " DataDisplay(tv)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Are you using cable at home? (Yes or No): N\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Are you using a satellite dish? (Yes or No): Y\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the satellite dish company name: ABCD Company\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter your name: Tony Zhang\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your age : 30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many hours you spend on watching TV per week: 8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "Here\u2019s what you\u2019ve entered:\n", + "\n", + "Name: Tony Zhang \n", + "\n", + "Age: 30 \n", + "\n", + "Hour per week: 8 \n", + "\n", + "Your satellite dish company is: ABCD Company \n", + "\n", + "\n", + "Thanks and Bye!\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 20.6, Page No 348" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class bit_field:\n", + " def __init__(self,cable,dish):\n", + " self.cable = cable\n", + " self.dish = dish\n", + " cable = 1\n", + " dish = 1\n", + "class survey:\n", + " def __init__(self,name,age,hour_per_week):\n", + " self.name = name\n", + " self.age = age\n", + " self.hour_per_week = hour_per_week\n", + " c_d = bit_field(\"\",\"\")\n", + " class provider:\n", + " def __init__(self,cable_company,dish_company):\n", + " self.cable_company = cable_company\n", + " self.dish_company = dish_company\n", + "def DataEnter(ptr):\n", + " is_yes = raw_input(\"Are you using cable at home? (Yes or No): \")\n", + " if is_yes == 'Y' or is_yes == 'y':\n", + " ptr.provider.cable_company = raw_input(\"Enter the cable company name: \")\n", + " ptr.c_d.cable = 1\n", + " ptr.c_d.dish = 0\n", + " else:\n", + " is_yes = raw_input(\"Are you using a satellite dish? (Yes or No): \")\n", + " if is_yes == 'Y' or is_yes == 'y':\n", + " ptr.provider.dish_company = raw_input(\"Enter the satellite dish company name: \")\n", + " ptr.c_d.cable = 0\n", + " ptr.c_d.dish = 1\n", + " else:\n", + " ptr.c_d.cable = 0\n", + " ptr.c_d.dish = 0\n", + " ptr.name = raw_input(\"Please enter your name: \")\n", + " ptr.age = int(raw_input(\"Your age : \"))\n", + " ptr.hour_per_week = int(raw_input(\"How many hours you spend on watching TV per week: \"))\n", + "def DataDisplay(ptr):\n", + " print \"\\nHere\u2019s what you\u2019ve entered:\\n\"\n", + " print \"Name: \",ptr.name,\"\\n\"\n", + " print \"Age: \",ptr.age,\"\\n\"\n", + " print \"Hour per week: \",ptr.hour_per_week,\"\\n\"\n", + " if ptr.c_d.cable and not ptr.c_d.dish:\n", + " print \"Your cable company is: \",ptr.provider.cable_company,\"\\n\"\n", + " elif not ptr.c_d.cable and ptr.c_d.dish:\n", + " print \"Your satellite dish company is: \",ptr.provider.dish_company,\"\\n\"\n", + " else:\n", + " print \"You don\u2019t have cable or a satellite dish.\\n\"\n", + " print \"\\nThanks and Bye!\\n\"\n", + "if __name__ == '__main__':\n", + " tv = survey(\"\",\"\",\"\")\n", + " DataEnter(tv)\n", + " DataDisplay(tv)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Are you using cable at home? (Yes or No): N\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Are you using a satellite dish? (Yes or No): Y\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the satellite dish company name: ABCD Company\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter your name: Tony Zhang\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your age : 30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many hours you spend on watching TV per week: 8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Here\u2019s what you\u2019ve entered:\n", + "\n", + "Name: Tony Zhang \n", + "\n", + "Age: 30 \n", + "\n", + "Hour per week: 8 \n", + "\n", + "Your satellite dish company is: ABCD Company \n", + "\n", + "\n", + "Thanks and Bye!\n", + "\n" + ] + } + ], + "prompt_number": 6 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour21.ipynb b/Teach_Yourself_C_in_24_Hours/hour21.ipynb new file mode 100755 index 00000000..54c180de --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour21.ipynb @@ -0,0 +1,277 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:3c3bf16deef4fec8e871033ad86caaf2229bc23c73902bfdcf895c53cd295312"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 21 : Reading and Writing with Files"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 21.1, Page No 359"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "try:\n",
+ " filename = \"haiku.txt\"\n",
+ " reval = \"SUCCESS\"\n",
+ " fptr = open(filename,\"r\")\n",
+ " if(not isinstance(fptr,file)):\n",
+ " print \"\\nCan not open \",filename,\"\\n\"\n",
+ " reval = \"FAIL\"\n",
+ " else:\n",
+ " print \"The value of fptr: \",fptr,\"\\n\"\n",
+ " print \"Ready to close the file.\"\n",
+ " fptr.close()\n",
+ "except IOError:\n",
+ " print \"\\nFile can not be opened\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The value of fptr: <open file 'haiku.txt', mode 'r' at 0x000000000394DB70> \n",
+ "\n",
+ "Ready to close the file.\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 21.2, Page No 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def CharReadWrite(fin,fout):\n",
+ " while(1):\n",
+ " c = fin.readline()\n",
+ " if c == \"\":\n",
+ " break\n",
+ " else:\n",
+ " print c\n",
+ " fout.writelines(c)\n",
+ "try:\n",
+ " filename1 = \"outhaiku.txt\"\n",
+ " filename2 = \"haiku.txt\"\n",
+ " reval = \"SUCCESS\"\n",
+ " fptr1 = open(filename1,\"w\")\n",
+ " fptr2 = open(filename2,\"r\")\n",
+ " if(not isinstance(fptr1,file)):\n",
+ " print \"\\nCan not open \",filename1,\"\\n\"\n",
+ " reval = \"FAIL\"\n",
+ " elif(not isinstance(fptr1,file)):\n",
+ " print \"\\nCan not open \",filename2,\"\\n\"\n",
+ " reval = \"FAIL\"\n",
+ " else:\n",
+ " CharReadWrite(fptr2,fptr1)\n",
+ " fptr1.close()\n",
+ " fptr2.close()\n",
+ "except IOError:\n",
+ " print \"\\nFile can not be opened\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Leading me along\n",
+ "\n",
+ "my shadow goes back home\n",
+ "\n",
+ "from looking at the moon.\n",
+ "\n",
+ "--- Sodo\n",
+ "\n",
+ "(1641-1716)\n",
+ "\n",
+ "A storm wind blows\n",
+ "\n",
+ "out from among the grasses\n",
+ "\n",
+ "the full moon grows.\n",
+ "\n",
+ "--- Chora\n",
+ "\n",
+ "(1729-1781)\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 21.3, Page No 364"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def LineReadWrite(fin,fout):\n",
+ " while(1):\n",
+ " c = fin.readline()\n",
+ " if c == \"\":\n",
+ " break\n",
+ " else:\n",
+ " print c\n",
+ " fout.writelines(c)\n",
+ "try:\n",
+ " filename1 = \"outhaiku.txt\"\n",
+ " filename2 = \"haiku.txt\"\n",
+ " reval = \"SUCCESS\"\n",
+ " fptr1 = open(filename1,\"w\")\n",
+ " fptr2 = open(filename2,\"r\")\n",
+ " if(not isinstance(fptr1,file)):\n",
+ " print \"\\nCan not open \",filename1,\"\\n\"\n",
+ " reval = \"FAIL\"\n",
+ " elif(not isinstance(fptr1,file)):\n",
+ " print \"\\nCan not open \",filename2,\"\\n\"\n",
+ " reval = \"FAIL\"\n",
+ " else:\n",
+ " LineReadWrite(fptr2,fptr1)\n",
+ " fptr1.close()\n",
+ " fptr2.close()\n",
+ "except IOError:\n",
+ " print \"\\nFile can not be opened\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Leading me along\n",
+ "\n",
+ "my shadow goes back home\n",
+ "\n",
+ "from looking at the moon.\n",
+ "\n",
+ "--- Sodo\n",
+ "\n",
+ "(1641-1716)\n",
+ "\n",
+ "A storm wind blows\n",
+ "\n",
+ "out from among the grasses\n",
+ "\n",
+ "the full moon grows.\n",
+ "\n",
+ "--- Chora\n",
+ "\n",
+ "(1729-1781)\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 21.4, Page No 368"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def BlockReadWrite(fin,fout):\n",
+ " while(1):\n",
+ " c = fin.readline()\n",
+ " if c == \"\":\n",
+ " break\n",
+ " else:\n",
+ " print c\n",
+ " fout.writelines(c)\n",
+ "def ErrorMsg(str1):\n",
+ " print \"Cannot open: \",str1,\"\\n\"\n",
+ " return FAIL\n",
+ "try:\n",
+ " filename1 = \"outhaiku.txt\"\n",
+ " filename2 = \"haiku.txt\"\n",
+ " reval = \"SUCCESS\"\n",
+ " fptr1 = open(filename1,\"w\")\n",
+ " fptr2 = open(filename2,\"r\")\n",
+ " if(not isinstance(fptr1,file)):\n",
+ " print \"\\nCan not open \",filename1,\"\\n\"\n",
+ " reval = ErrorMsg(filename1)\n",
+ " elif(not isinstance(fptr1,file)):\n",
+ " print \"\\nCan not open \",filename2,\"\\n\"\n",
+ " reval = ErrorMsg(filename2)\n",
+ " else:\n",
+ " BlockReadWrite(fptr2,fptr1)\n",
+ " fptr1.close()\n",
+ " fptr2.close()\n",
+ "except IOError:\n",
+ " print \"\\nFile can not be opened\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Leading me along\n",
+ "\n",
+ "my shadow goes back home\n",
+ "\n",
+ "from looking at the moon.\n",
+ "\n",
+ "--- Sodo\n",
+ "\n",
+ "(1641-1716)\n",
+ "\n",
+ "A storm wind blows\n",
+ "\n",
+ "out from among the grasses\n",
+ "\n",
+ "the full moon grows.\n",
+ "\n",
+ "--- Chora\n",
+ "\n",
+ "(1729-1781)\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour22.ipynb b/Teach_Yourself_C_in_24_Hours/hour22.ipynb new file mode 100755 index 00000000..1f255fb4 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour22.ipynb @@ -0,0 +1,365 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:619858f96c40102945d24a70a3fd99304f6fb166114144600528626d39e31490"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 22 : Using Special File Functions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 22.1, Page No 375"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def PtrSeek(fptr):\n",
+ " offset1 = PtrTell(fptr)\n",
+ " DataRead(fptr)\n",
+ " offset2 = PtrTell(fptr)\n",
+ " DataRead(fptr)\n",
+ " offset3 = PtrTell(fptr)\n",
+ " DataRead(fptr)\n",
+ " print \"\\nRe-read the haiku:\\n\"\n",
+ " fptr.seek(offset3)\n",
+ " DataRead(fptr)\n",
+ " fptr.seek(offset2)\n",
+ " DataRead(fptr)\n",
+ " fptr.seek(offset1)\n",
+ " DataRead(fptr)\n",
+ "def PtrTell(fprt):\n",
+ " reval = fptr.tell()\n",
+ " print \"The fptr is at: \",reval,\"\\n\"\n",
+ " return reval\n",
+ "def DataRead(fptr):\n",
+ " buff = range(80)\n",
+ " buff = fptr.readline()\n",
+ " print buff\n",
+ "def ErrorMsg(str):\n",
+ " print \"Cannot open\",str,\"\\n\"\n",
+ " return FAIL\n",
+ "MAX_LEN = 80\n",
+ "try:\n",
+ " filename = \"haiku.txt\"\n",
+ " reval = \"SUCCESS\"\n",
+ " fptr = open(filename,\"r\")\n",
+ " if(not isinstance(fptr,file)):\n",
+ " reval = ErrorMsg(filename)\n",
+ " else:\n",
+ " PtrSeek(fptr)\n",
+ " fptr.close()\n",
+ "except IOError:\n",
+ " print \"\\nFile can not be opened\\n\"\n",
+ " \n",
+ " \n",
+ "\"\"\"\n",
+ "haiku.txt (Read file)\n",
+ "Leading me along\n",
+ "my shadow goes back home\n",
+ "from looking at the moon.\n",
+ "--- Sodo\n",
+ "(1641-1716)\n",
+ "A storm wind blows\n",
+ "out from among the grasses\n",
+ "the full moon grows.\n",
+ "--- Chora\n",
+ "(1729-1781)\n",
+ "\"\"\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The fptr is at: 0 \n",
+ "\n",
+ "Leading me along\n",
+ "\n",
+ "The fptr is at: 18 \n",
+ "\n",
+ "my shadow goes back home\n",
+ "\n",
+ "The fptr is at: 44 \n",
+ "\n",
+ "from looking at the moon.\n",
+ "\n",
+ "\n",
+ "Re-read the haiku:\n",
+ "\n",
+ "from looking at the moon.\n",
+ "\n",
+ "my shadow goes back home\n",
+ "\n",
+ "Leading me along\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 22.2, Page No 379"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "MAX_NUM = 3\n",
+ "def DataWrite(fout):\n",
+ " buff = [123.45,567.89,100.11]\n",
+ " print \"The size of buff:\",sys.getsizeof(buff),\"-byte\\n\u201d, sizeof(buff)\"\n",
+ " for i in range(MAX_NUM):\n",
+ " print buff[i],\"\\n\"\n",
+ " fout.write(str(buff[i]) + \"\\n\")\n",
+ "def DataRead(fin):\n",
+ " print \"\\nRead back from the binary file:\\n\"\n",
+ " for i in range(MAX_NUM):\n",
+ " x = fin.readline()\n",
+ " print x,\"\\n\"\n",
+ "def ErrorMsg(str):\n",
+ " print \"Cannot open\",str,\"\\n\"\n",
+ " return FAIL\n",
+ "try:\n",
+ " filename = \"double.bin\"\n",
+ " reval = \"SUCCESS\"\n",
+ " fptr = open(filename,\"wb\")\n",
+ " if(not isinstance(fptr,file)):\n",
+ " reval = ErrorMsg(filename)\n",
+ " else:\n",
+ " DataWrite(fptr)\n",
+ " # rewind(fptr) No Such Function In Python\n",
+ " fptr.close()\n",
+ " fptr = open(filename,\"r\")\n",
+ " DataRead(fptr)\n",
+ " fptr.close()\n",
+ "except IOError:\n",
+ " print \"\\nFile can not be opened\\n\"\n",
+ "\n",
+ "\"\"\"\n",
+ "double.bin (Writing Binary File)\n",
+ "123.45\n",
+ "567.89\n",
+ "100.11\n",
+ "\"\"\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The size of buff: 88 -byte\n",
+ "\u201d, sizeof(buff)\n",
+ "123.45 \n",
+ "\n",
+ "567.89 \n",
+ "\n",
+ "100.11 \n",
+ "\n",
+ "\n",
+ "Read back from the binary file:\n",
+ "\n",
+ "123.45\n",
+ "\n",
+ "\n",
+ "567.89\n",
+ "\n",
+ "\n",
+ "100.11\n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 22.3, Page No 382"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "MAX_NUM = 3\n",
+ "STR_LEN = 23\n",
+ "def DataWrite(fout):\n",
+ " cities = range(MAX_NUM)\n",
+ " cities = [\"St.Louis->Houston:\",\"Houston->Dallas:\",\"Dallas->Philadelphia:\"]\n",
+ " miles = range(MAX_NUM)\n",
+ " miles = [845,243,1459]\n",
+ " print \"The data written:\\n\"\n",
+ " for i in range(MAX_NUM):\n",
+ " print cities[i],miles[i],\"\\n\"\n",
+ " fout.writelines(str(cities[i]) + str(miles[i]) + \"\\n\")\n",
+ "def DataRead(fin):\n",
+ " print \"\\nThe data read:\\n\"\n",
+ " for i in range(MAX_NUM):\n",
+ " cities = fin.read()\n",
+ " miles = fin.read()\n",
+ " print cities,miles\n",
+ "def ErrorMsg(str):\n",
+ " print \"Cannot open\",str,\"\\n\"\n",
+ " return FAIL\n",
+ "try:\n",
+ " filename = \"strnum.mix\"\n",
+ " reval = \"SUCCESS\"\n",
+ " fptr = open(filename,\"w+\")\n",
+ " if(not isinstance(fptr,file)):\n",
+ " reval = ErrorMsg(filename)\n",
+ " else:\n",
+ " DataWrite(fptr)\n",
+ " # rewind(fptr) No Such Function In Python\n",
+ " fptr.close()\n",
+ " fptr = open(filename,\"r\")\n",
+ " DataRead(fptr)\n",
+ " fptr.close()\n",
+ "except IOError:\n",
+ " print \"\\nFile can not be opened\\n\"\n",
+ "\"\"\"\n",
+ "strnum.mix (Writing and Reading File)\n",
+ "St.Louis->Houston:845\n",
+ "Houston->Dallas:243\n",
+ "Dallas->Philadelphia:1459\n",
+ "\"\"\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The data written:\n",
+ "\n",
+ "St.Louis->Houston: 845 \n",
+ "\n",
+ "Houston->Dallas: 243 \n",
+ "\n",
+ "Dallas->Philadelphia: 1459 \n",
+ "\n",
+ "\n",
+ "The data read:\n",
+ "\n",
+ "St.Louis->Houston:845\n",
+ "Houston->Dallas:243\n",
+ "Dallas->Philadelphia:1459\n",
+ "\n",
+ " \n",
+ " \n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 22.4, Page No 385"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "STR_NUM = 4\n",
+ "def ErrorMsg(str1):\n",
+ " print \"Cannot open\",str,\"\\n\"\n",
+ " return FAIL\n",
+ "def StrPrint(str1):\n",
+ " for i in range(STR_NUM):\n",
+ " print str1[i],\"\\n\"\n",
+ " stdout.writelines(str1[i] + \"\\n\")\n",
+ " \n",
+ "try:\n",
+ " str1 = range(STR_NUM)\n",
+ " str1 = [\"Be bent, and you will remain straight.\",\"Be vacant, and you will remain full.\",\"Be worn, and you will remain new.\",\"--- by Lao Tzu\"]\n",
+ " filename = \"LaoTzu.txt\"\n",
+ " reval = \"SUCCESS\"\n",
+ " stdout = open(filename,\"w+\")\n",
+ " StrPrint(str1)\n",
+ " stdout.close()\n",
+ " stdout = open(filename,\"w+\")\n",
+ " if(not isinstance(stdout,file)):\n",
+ " reval = ErrorMsg(filename)\n",
+ " else:\n",
+ " StrPrint(str1)\n",
+ " stdout.close()\n",
+ "except IOError:\n",
+ " print \"\\nFile can not be opened\\n\"\n",
+ "\n",
+ "\"\"\"\n",
+ " Output File : LaoTzu.txt\n",
+ " \n",
+ " Be bent, and you will remain straight.\n",
+ "Be vacant, and you will remain full.\n",
+ "Be worn, and you will remain new.\n",
+ "--- by Lao Tzu\n",
+ "\n",
+ "\n",
+ "\"\"\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Be bent, and you will remain straight. \n",
+ "\n",
+ "Be vacant, and you will remain full. \n",
+ "\n",
+ "Be worn, and you will remain new. \n",
+ "\n",
+ "--- by Lao Tzu \n",
+ "\n",
+ "Be bent, and you will remain straight. \n",
+ "\n",
+ "Be vacant, and you will remain full. \n",
+ "\n",
+ "Be worn, and you will remain new. \n",
+ "\n",
+ "--- by Lao Tzu \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour23.ipynb b/Teach_Yourself_C_in_24_Hours/hour23.ipynb new file mode 100755 index 00000000..ba61a82d --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour23.ipynb @@ -0,0 +1,232 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:95f715496e847b4ccbd328c0da6129f101af5cd4b3c551a2f5644703db14657d"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 23 : Compiling Programs: The C Preprocessor"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 23.1, Page No 394"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "METHOD = \"ABS\"\n",
+ "def ABS(val):\n",
+ " if val < 0:\n",
+ " return -(val)\n",
+ " else:\n",
+ " return (val)\n",
+ "MAX_LEN = 8\n",
+ "NEGATIVE_NUM = -10\n",
+ "array = range(MAX_LEN)\n",
+ "print \"The orignal values in array:\"\n",
+ "for i in range(MAX_LEN):\n",
+ " array[i] = (i + 1) * NEGATIVE_NUM;\n",
+ " print \"array[\",i,\"]:\",array[i]\n",
+ "print \"\\nApplying the \",METHOD,\" macro:\\n\"\n",
+ "for i in range(MAX_LEN):\n",
+ " print \"ABS(%d) : \"%(array[i]), ABS(array[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The orignal values in array:\n",
+ "array[ 0 ]: -10\n",
+ "array[ 1 ]: -20\n",
+ "array[ 2 ]: -30\n",
+ "array[ 3 ]: -40\n",
+ "array[ 4 ]: -50\n",
+ "array[ 5 ]: -60\n",
+ "array[ 6 ]: -70\n",
+ "array[ 7 ]: -80\n",
+ "\n",
+ "Applying the ABS macro:\n",
+ "\n",
+ "ABS(-10) : 10\n",
+ "ABS(-20) : 20\n",
+ "ABS(-30) : 30\n",
+ "ABS(-40) : 40\n",
+ "ABS(-50) : 50\n",
+ "ABS(-60) : 60\n",
+ "ABS(-70) : 70\n",
+ "ABS(-80) : 80\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 23.2, Page No 398"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#There is no concept of the mentioned directives in Python.\n",
+ "UPPER_CASE = True\n",
+ "LOWER_CASE = False\n",
+ "if UPPER_CASE:\n",
+ " print \"THIS LINE IS PRINTED OUT,\\n\"\n",
+ " print \"BECAUSE UPPER_CASE IS DEFINED.\\n\"\n",
+ "if not LOWER_CASE:\n",
+ " print \"\\nThis line is printed out,\\n\"\n",
+ " print \"because LOWER_CASE is not defined.\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "THIS LINE IS PRINTED OUT,\n",
+ "\n",
+ "BECAUSE UPPER_CASE IS DEFINED.\n",
+ "\n",
+ "\n",
+ "This line is printed out,\n",
+ "\n",
+ "because LOWER_CASE is not defined.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 23.3, Page No 401"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "C_LANG = 'C'\n",
+ "B_LANG = 'B'\n",
+ "NO_ERROR = 0\n",
+ "if C_LANG == 'C' and B_LANG == 'B':\n",
+ " del C_LANG\n",
+ " C_LANG = \"I only know C language.\\n\"\n",
+ " print C_LANG\n",
+ " del B_LANG\n",
+ " B_LANG = \"I know BASIC.\\n\"\n",
+ " print C_LANG,B_LANG\n",
+ "elif C_LANG == 'C':\n",
+ " del C_LANG\n",
+ " C_LANG = \"I only know C language.\\n\"\n",
+ " print C_LANG\n",
+ "elif B_LANG == 'B':\n",
+ " del B_LANG\n",
+ " B_LANG = \"I only know BASIC.\\n\"\n",
+ " print B_LANG\n",
+ "else:\n",
+ " print \"\u201cI don\u2019t know C or BASIC.\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I only know C language.\n",
+ "\n",
+ "I only know C language.\n",
+ "I know BASIC.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 23.4, Page No 403"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "ZERO = 0\n",
+ "ONE = 1\n",
+ "TWO = ONE + ONE\n",
+ "THREE = ONE + TWO\n",
+ "TEST_1 = ONE\n",
+ "TEST_2 = TWO\n",
+ "TEST_3 = THREE\n",
+ "MAX_NUM = THREE\n",
+ "NO_ERROR = ZERO\n",
+ "def StrPrint(ptr_s,max1):\n",
+ " for i in range(max1):\n",
+ " print \"Content: \",ptr_s[i],\"\\n\"\n",
+ "\n",
+ "str1 = [\"The choice of a point of view\",\"is the initial act of culture.\",\"--- by O. Gasset\"]\n",
+ "if TEST_1 == 1:\n",
+ " if TEST_2 == 2:\n",
+ " if TEST_3 == 3:\n",
+ " StrPrint(str1,MAX_NUM)\n",
+ " else:\n",
+ " StrPrint(str1,MAX_NUM - ONE)\n",
+ " else:\n",
+ " StrPrint(str1,MAX_NUM - ONE)\n",
+ "else:\n",
+ " \"No TEST macro has been set.\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Content: The choice of a point of view \n",
+ "\n",
+ "Content: is the initial act of culture. \n",
+ "\n",
+ "Content: --- by O. Gasset \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour24.ipynb b/Teach_Yourself_C_in_24_Hours/hour24.ipynb new file mode 100755 index 00000000..0db0f3ec --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour24.ipynb @@ -0,0 +1,80 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:2892cdbf82debfaaf5bd321dc73cce7cbc035bf0ab81bdbe14335d47dded8155"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 24: Where Do you Go from Here?"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 24.1, Page No.410"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Dynamic Memory Allocation is not available in python, so the code for the given programme is skipped."
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 24.2, Page No.415"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Dynamic Memory Allocation is not available in python, so the code for the given programme is skipped."
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 24.3, Page No.416"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#free function is not available in python, so function for freeing memory is not created."
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 3
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour3.ipynb b/Teach_Yourself_C_in_24_Hours/hour3.ipynb new file mode 100755 index 00000000..95b6416b --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour3.ipynb @@ -0,0 +1,103 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:ce290cf2bde2b380a50d312f15b1ca096cf17ef06fb76494e9775a2b07fbccc8"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 3: Learning the Structure of a C program"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.1, Page No.48"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print \"Howdy, neighbour! This is my first C program\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Howdy, neighbour! This is my first C program\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.2, Page No.49"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def integer_add(x,y):\n",
+ " result = x+y\n",
+ " return result\n",
+ "#This program has no output because the function is not called in this program"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.3, Page No.50"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def integer_add(x,y):\n",
+ " result = x+y\n",
+ " return result\n",
+ "sum=integer_add(5,12)\n",
+ "print \"The addition of 5 and 12 is \",sum"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The addition of 5 and 12 is 17\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour4.ipynb b/Teach_Yourself_C_in_24_Hours/hour4.ipynb new file mode 100755 index 00000000..7496626d --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour4.ipynb @@ -0,0 +1,164 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:a2f721b4c266424e2bea1224b09de694c0f19e61e65e603f28f9ade00148c17a"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 4: Understanding Data Types and Keywords"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.1, Page No.60"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "c1='A'\n",
+ "c2='a'\n",
+ "\n",
+ "print \"Convert the value of c1 to character : \",c1\n",
+ "print \"Convert the value of c2 to character : \",c2"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Convert the value of c1 to character : A\n",
+ "Convert the value of c2 to character : a\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.2, Page No.61"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "c1=65\n",
+ "c2=97\n",
+ "\n",
+ "print \"The character that has numeric value of 65 is:\", chr(c1)\n",
+ "print \"The character that has numeric value of 97 is:\", chr(c2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The character that has numeric value of 65 is: A\n",
+ "The character that has numeric value of 97 is: a\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.3, Page No.63"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "c1='A'\n",
+ "c2='a'\n",
+ "print \"The Numeric Value of A is:\",ord(c1)\n",
+ "print \"The Numeric Value of a is:\",ord(c2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The Numeric Value of A is: 65\n",
+ "The Numeric Value of a is: 97\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.4, Page No.65"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "int_num1=int(32/10)\n",
+ "flt_num1=float(32/10)\n",
+ "int_num2=int(32.0/10)\n",
+ "flt_num2=float(32.0/10)\n",
+ "int_num3=int(32/10.0)\n",
+ "flt_num3=float(32/10.0)\n",
+ "\n",
+ "print \"The integer divis. of 32/10 is\",int_num1\n",
+ "print \"The floating-point divis. of 32/10 is\",flt_num1\n",
+ "\n",
+ "print \"The integer divis. of 32.0/10 is\",int_num2\n",
+ "print \"The floating-point divis. of 32.0/10 is\",flt_num2\n",
+ "\n",
+ "print \"The integer divis. of 32/10.0 is\",int_num3\n",
+ "print \"The floating-point divis. of 32/10.0 is\",flt_num3"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The integer divis. of 32/10 is 3\n",
+ "The floating-point divis. of 32/10 is 3.0\n",
+ "The integer divis. of 32.0/10 is 3\n",
+ "The floating-point divis. of 32.0/10 is 3.2\n",
+ "The integer divis. of 32/10.0 is 3\n",
+ "The floating-point divis. of 32/10.0 is 3.2\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour5.ipynb b/Teach_Yourself_C_in_24_Hours/hour5.ipynb new file mode 100755 index 00000000..4926fe29 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour5.ipynb @@ -0,0 +1,346 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:3993f457ea298742521e73510f9498127e21d06e98284c389cdf6b608a1b8c94"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 5: Handling Standard Input and Output"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.1, Page No.73"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "ch=raw_input(\"Please type in one character\")\n",
+ "print \"The Character you just entered is: \", ch"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Please type in one characterH\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The Character you just entered is: H\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.2, Page No.74"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "ch1=raw_input(\"Please type in first character\")\n",
+ "ch2=raw_input(\"Please type in second character\")\n",
+ "\n",
+ "print \"The first character you just entered is: \",ch1\n",
+ "print \"The second character you just entered is: \",ch2"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Please type in first characterH\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Please type in second characteri\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The first character you just entered is: H\n",
+ "The second character you just entered is: i\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.3, Page No.76"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "ch=65\n",
+ "print \"The character that has numeric value of 65 is:\",chr(ch)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The character that has numeric value of 65 is: A\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.4, Page No.77"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print chr(65)\n",
+ "print chr(10)\n",
+ "print chr(66)\n",
+ "print chr(10)\n",
+ "print chr(67)\n",
+ "print chr(10)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A\n",
+ "\n",
+ "\n",
+ "B\n",
+ "\n",
+ "\n",
+ "C\n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.5, Page No.80"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print \"Hex(uppercase) Hex(lowercase) Decimal\"\n",
+ "print \"{:01X} {:01x} {:d}\".format(0,0,0)\n",
+ "print \"{:01X} {:01x} {:d}\".format(1,1,1)\n",
+ "print \"{:01X} {:01x} {:d}\".format(2,2,2)\n",
+ "print \"{:01X} {:01x} {:d}\".format(3,3,3)\n",
+ "print \"{:01X} {:01x} {:d}\".format(4,4,4)\n",
+ "print \"{:01X} {:01x} {:d}\".format(5,5,5)\n",
+ "print \"{:01X} {:01x} {:d}\".format(6,6,6)\n",
+ "print \"{:01X} {:01x} {:d}\".format(7,7,7)\n",
+ "print \"{:01X} {:01x} {:d}\".format(8,8,8)\n",
+ "print \"{:01X} {:01x} {:d}\".format(9,9,9)\n",
+ "print \"{:01X} {:01x} {:d}\".format(10,10,10)\n",
+ "print \"{:01X} {:01x} {:d}\".format(11,11,11)\n",
+ "print \"{:01X} {:01x} {:d}\".format(12,12,12)\n",
+ "print \"{:01X} {:01x} {:d}\".format(13,13,13)\n",
+ "print \"{:01X} {:01x} {:d}\".format(14,14,14)\n",
+ "print \"{:01X} {:01x} {:d}\".format(15,15,15)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hex(uppercase) Hex(lowercase) Decimal\n",
+ "0 0 0\n",
+ "1 1 1\n",
+ "2 2 2\n",
+ "3 3 3\n",
+ "4 4 4\n",
+ "5 5 5\n",
+ "6 6 6\n",
+ "7 7 7\n",
+ "8 8 8\n",
+ "9 9 9\n",
+ "A a 10\n",
+ "B b 11\n",
+ "C c 12\n",
+ "D d 13\n",
+ "E e 14\n",
+ "F f 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 31
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.6, Page No.82"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "num1=12\n",
+ "num2=12345\n",
+ "print num1\n",
+ "print num2\n",
+ "print \"{:5d}\".format(num1)\n",
+ "print \"{:05d}\".format(num1)\n",
+ "print \"{:2d}\".format(num2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "12\n",
+ "12345\n",
+ " 12\n",
+ "00012\n",
+ "12345\n"
+ ]
+ }
+ ],
+ "prompt_number": 32
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.7, Page No.83"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "num1=1\n",
+ "num2=12\n",
+ "num3=123\n",
+ "num4=1234\n",
+ "num5=12345\n",
+ "print \"{:8d}{:-8d}\".format(num1,num1)\n",
+ "print \"{:8d}{:-8d}\".format(num2,num2)\n",
+ "print \"{:8d}{:-8d}\".format(num3,num3)\n",
+ "print \"{:8d}{:-8d}\".format(num4,num4)\n",
+ "print \"{:8d}{:-8d}\".format(num5,num5)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1 1\n",
+ " 12 12\n",
+ " 123 123\n",
+ " 1234 1234\n",
+ " 12345 12345\n"
+ ]
+ }
+ ],
+ "prompt_number": 35
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.8, Page No.84"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "int_num=123\n",
+ "flt_num=123.456789\n",
+ "print \"Default integer format: \",int_num\n",
+ "print \"With Precision Specifier: \",format(int_num,'08d')\n",
+ "print \"Default float format: \",flt_num\n",
+ "print \"With Precision Specifier: \",format(flt_num,'6.2f')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Default integer format: 123\n",
+ "With Precision Specifier: 00000123\n",
+ "Default float format: 123.456789\n",
+ "With Precision Specifier: 123.46\n"
+ ]
+ }
+ ],
+ "prompt_number": 44
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour6.ipynb b/Teach_Yourself_C_in_24_Hours/hour6.ipynb new file mode 100755 index 00000000..a23d8d29 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour6.ipynb @@ -0,0 +1,211 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:e529cfbc85bce112cc77323c9cb2b301f79e8cbceaba58360ccb33809ec1b0e2"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "HOUR 6 : Manipulating Data"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.1, Page No 94"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x = 1\n",
+ "y = 3\n",
+ "z = 10\n",
+ "print \"Given \" ,x,y,z,\"\\n\"\n",
+ "x = x + y\n",
+ "print \"x = x + y assigns \" + str(x) + \"to x\\n\"\n",
+ "x = 1\n",
+ "x += y\n",
+ "print \"x += y assigns \" + str(x) + \"to x\\n\"\n",
+ "x = 1\n",
+ "z = z * x + y\n",
+ "print \"z = z * x + y assigns\" + str(z) + \"to z\\n\"\n",
+ "z = 10\n",
+ "z = z * (x + y)\n",
+ "print \"z = z * (x + y) assigns\" + str(z) + \"to z\\n\"\n",
+ "z = 10\n",
+ "z *= x + y\n",
+ "print \"z *= x + y assigns\" + str(z) + \"to z\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Given 1 3 10 \n",
+ "\n",
+ "x = x + y assigns 4to x\n",
+ "\n",
+ "x += y assigns 4to x\n",
+ "\n",
+ "z = z * x + y assigns13to z\n",
+ "\n",
+ "z = z * (x + y) assigns40to z\n",
+ "\n",
+ "z *= x + y assigns40to z\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.2, Page No 97"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#there is no concept of pre-increment or post-increment in Python.\n",
+ "w=x=y=z=1\n",
+ "print \"Given w = \",w,\"x = \",x,\"y = \",y,\"z = \",z,\"\\n\"\n",
+ "w+=1\n",
+ "result=w\n",
+ "print \"++w evaluates to \" ,result,\"and w is now \",w,\"\\n\"\n",
+ "result = x\n",
+ "x = x + 1\n",
+ "print \"x++ evaluates to \",result,\" and x is now \",x,\"\\n\"\n",
+ "y-=1\n",
+ "result = y\n",
+ "print \"--y evaluates to \",result,\" and y is now \",y,\"\\n\"\n",
+ "result = z\n",
+ "z -= 1\n",
+ "print \"z-- evaluates to \",result,\" and z is now \",z,\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Given w = 1 x = 1 y = 1 z = 1 \n",
+ "\n",
+ "++w evaluates to 2 and w is now 2 \n",
+ "\n",
+ "x++ evaluates to 1 and x is now 2 \n",
+ "\n",
+ "--y evaluates to 0 and y is now 0 \n",
+ "\n",
+ "z-- evaluates to 1 and z is now 0 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.3, Page No 99"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x = 7\n",
+ "y = 25\n",
+ "z = 24.46\n",
+ "print \"Given x = \",x,\"y = \",y,\" and z = \",z,\"\\n\"\n",
+ "print \"x >= y produces: \",x >= y,\"\\n\"\n",
+ "print \"x == y produces: \",x == y,\"\\n\"\n",
+ "print \"x < z produces: \",x < z,\"\\n\"\n",
+ "print \"y > z produces: \",y > z,\"\\n\"\n",
+ "print \"x != y - 18 produces: \",x != y - 18,\"\\n\"\n",
+ "print \"x + y != z produces: \",x + y != z"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Given x = 7 y = 25 and z = 24.46 \n",
+ "\n",
+ "x >= y produces: False \n",
+ "\n",
+ "x == y produces: False \n",
+ "\n",
+ "x < z produces: True \n",
+ "\n",
+ "y > z produces: True \n",
+ "\n",
+ "x != y - 18 produces: False \n",
+ "\n",
+ "x + y != z produces: True\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.4, Page No 101"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x = 7\n",
+ "y = 5\n",
+ "print \"Given x = \",x,\"y = \",y,\"\\n\"\n",
+ "print \"x / y produces: \",x/y,\"\\n\"\n",
+ "print \"(float)x / y produces: \", float(x)/y"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Given x = 7 y = 5 \n",
+ "\n",
+ "x / y produces: 1 \n",
+ "\n",
+ "(float)x / y produces: 1.4\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour7.ipynb b/Teach_Yourself_C_in_24_Hours/hour7.ipynb new file mode 100755 index 00000000..c6333f50 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour7.ipynb @@ -0,0 +1,306 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:7e105f47cd21746fb8dd2ed76992ed00bd717837d537834fb5013cd214e21609"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 7: Working with loops"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.1, Page No.106"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "c=' '\n",
+ "print \"Enter a character:\\n(Eneter X to exit)\"\n",
+ "while (c!='x'):\n",
+ " c=raw_input()\n",
+ " print c\n",
+ "print \"Out of the while loop. Bye\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a character:\n",
+ "(Eneter X to exit)\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "H\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "H\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "i\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "i\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x\n",
+ "Out of the while loop. Bye\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.2, Page No.108"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "i=65\n",
+ "while (i<72):\n",
+ " print \"The numeric value of \",chr(i),\" is \",i\n",
+ " i=i+1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The numeric value of A is 65\n",
+ "The numeric value of B is 66\n",
+ "The numeric value of C is 67\n",
+ "The numeric value of D is 68\n",
+ "The numeric value of E is 69\n",
+ "The numeric value of F is 70\n",
+ "The numeric value of G is 71\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.3, Page No.110"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print \"Hex(uppercase) Hex(lowercase) Decimal\"\n",
+ "for i in range (0,16):\n",
+ " print \"{:01X} {:01x} {:d}\".format(i,i,i)\n",
+ " i=i+1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hex(uppercase) Hex(lowercase) Decimal\n",
+ "0 0 0\n",
+ "1 1 1\n",
+ "2 2 2\n",
+ "3 3 3\n",
+ "4 4 4\n",
+ "5 5 5\n",
+ "6 6 6\n",
+ "7 7 7\n",
+ "8 8 8\n",
+ "9 9 9\n",
+ "A a 10\n",
+ "B b 11\n",
+ "C c 12\n",
+ "D d 13\n",
+ "E e 14\n",
+ "F f 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.4, Page No.114"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "j=8\n",
+ "for i in range(0,8):\n",
+ " print i,\"+\",j,\"=\",i+j\n",
+ " j=j-1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0 + 8 = 8\n",
+ "1 + 7 = 8\n",
+ "2 + 6 = 8\n",
+ "3 + 5 = 8\n",
+ "4 + 4 = 8\n",
+ "5 + 3 = 8\n",
+ "6 + 2 = 8\n",
+ "7 + 1 = 8\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.5, Page No.115"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "j=1\n",
+ "for i in range(0,8):\n",
+ " print j,\"-\",i,\"=\",j-i\n",
+ " j=j+1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 - 0 = 1\n",
+ "2 - 1 = 1\n",
+ "3 - 2 = 1\n",
+ "4 - 3 = 1\n",
+ "5 - 4 = 1\n",
+ "6 - 5 = 1\n",
+ "7 - 6 = 1\n",
+ "8 - 7 = 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.6, Page No.116"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "for i in range(1,4):\n",
+ " print \"The Start of iteration\",i,\"of the outer loop\"\n",
+ " for j in range(1,5):\n",
+ " print \"\\tIteration\",j,\"of the inner loop\"\n",
+ " print \"The end of iteration\",i,\"of the outer loop\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The Start of iteration 1 of the outer loop\n",
+ "\tIteration 1 of the inner loop\n",
+ "\tIteration 2 of the inner loop\n",
+ "\tIteration 3 of the inner loop\n",
+ "\tIteration 4 of the inner loop\n",
+ "The end of iteration 1 of the outer loop\n",
+ "The Start of iteration 2 of the outer loop\n",
+ "\tIteration 1 of the inner loop\n",
+ "\tIteration 2 of the inner loop\n",
+ "\tIteration 3 of the inner loop\n",
+ "\tIteration 4 of the inner loop\n",
+ "The end of iteration 2 of the outer loop\n",
+ "The Start of iteration 3 of the outer loop\n",
+ "\tIteration 1 of the inner loop\n",
+ "\tIteration 2 of the inner loop\n",
+ "\tIteration 3 of the inner loop\n",
+ "\tIteration 4 of the inner loop\n",
+ "The end of iteration 3 of the outer loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour8.ipynb b/Teach_Yourself_C_in_24_Hours/hour8.ipynb new file mode 100755 index 00000000..9918b3d0 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour8.ipynb @@ -0,0 +1,294 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:77914c92baf9b6155be2979d7adfda60e6d3dc335c9ab543c21e730dcfce4fad"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 8: Using Conditional Operators"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.1, Page No.122"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "ch=' '\n",
+ "int_num=0\n",
+ "flt_num=0.0\n",
+ "dbl_num=0.0\n",
+ "print \"The size of char is:\",sys.getsizeof(chr),\".byte\"\n",
+ "print \"The size of ch is:\",sys.getsizeof(ch),\".byte\"\n",
+ "print \"The size of int is:\",sys.getsizeof(int),\".byte\"\n",
+ "print \"The size of int_num is:\",sys.getsizeof(int_num),\".byte\"\n",
+ "print \"The size of float is:\",sys.getsizeof(float),\".byte\"\n",
+ "print \"The size of flt_num is:\",sys.getsizeof(flt_num),\".byte\"\n",
+ "#As python has no double data type, I have used only float data type here."
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The size of char is: 36 .byte\n",
+ "The size of ch is: 22 .byte\n",
+ "The size of int is: 436 .byte\n",
+ "The size of int_num is: 12 .byte\n",
+ "The size of float is: 436 .byte\n",
+ "The size of flt_num is: 16 .byte\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.2, Page No.125"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "num=0\n",
+ "print \"The AND operator yields:\",(num%2==0 and num%3==0)\n",
+ "num=2\n",
+ "print \"The AND operator yields:\",(num%2==0 and num%3==0)\n",
+ "num=3\n",
+ "print \"The AND operator yields:\",(num%2==0 and num%3==0)\n",
+ "num=6\n",
+ "print \"The AND operator yields:\",(num%2==0 and num%3==0)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The AND operator yields: True\n",
+ "The AND operator yields: False\n",
+ "The AND operator yields: False\n",
+ "The AND operator yields: True\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.3, Page No.127"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#It is not possible to use for-loop in Python the way it is given in textbook. Hence, the use of while loop\n",
+ "num=1\n",
+ "print \"Enter a single digit that can be divided \\nby both 2 and 3:\"\n",
+ "for num in range(2,7):\n",
+ " if(num%2!=0 or num%3!=0):\n",
+ " print num\n",
+ "print num\n",
+ "print \"You got such a number:\",num"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a single digit that can be divided \n",
+ "by both 2 and 3:\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "You got such a number: 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.4, Page No.128"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "num=7\n",
+ "print \"!(n<7) yields:\",(not(num<7))\n",
+ "print \"!(n>7) yields:\",(not(num>7))\n",
+ "print \"!(n==7) yields:\",(not(num==7))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "!(n<7) yields: True\n",
+ "!(n>7) yields: True\n",
+ "!(n==7) yields: False\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.5, Page No.132"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x=4321\n",
+ "y=5678\n",
+ "print \"Given x=\",x,\",i.e.,0X{:04X}\".format(x)\n",
+ "print \" y=\",y,\",i.e.,0X{:04X}\".format(y)\n",
+ "z=x&y\n",
+ "print \"X & y returns:{:6d}\".format(z),\",i.e.,0X{:04X}\".format(z)\n",
+ "z=x|y\n",
+ "print \"X | y returns:{:6d}\".format(z),\",i.e.,0X{:04X}\".format(z)\n",
+ "z=x^y\n",
+ "print \"X ^ y returns:{:6d}\".format(z),\",i.e.,0X{:04X}\".format(z)\n",
+ "print \" ~X returns:{:6d}\".format(~x),\",i.e.,0X{:04X}\".format(~x)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Given x= 4321 ,i.e.,0X10E1\n",
+ " y= 5678 ,i.e.,0X162E\n",
+ "X & y returns: 4128 ,i.e.,0X1020\n",
+ "X | y returns: 5871 ,i.e.,0X16EF\n",
+ "X ^ y returns: 1743 ,i.e.,0X06CF\n",
+ " ~X returns: -4322 ,i.e.,0X-10E2\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.6, Page No.134"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x=255\n",
+ "y=5\n",
+ "print \"Given x={:4d}\".format(x),\",i.e.,0X{:04X}\".format(x)\n",
+ "print \" y={:4d}\".format(y),\",i.e.,0X{:04X}\".format(y)\n",
+ "z=x>>y\n",
+ "print \"x >> y yields:{:6d}\".format(z),\",i.e.,0X{:04X}\".format(z)\n",
+ "z=x<<y\n",
+ "print \"x << y yields:{:6d}\".format(z),\",i.e.,0X{:04X}\".format(z)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Given x= 255 ,i.e.,0X00FF\n",
+ " y= 5 ,i.e.,0X0005\n",
+ "x >> y yields: 7 ,i.e.,0X0007\n",
+ "x << y yields: 8160 ,i.e.,0X1FE0\n"
+ ]
+ }
+ ],
+ "prompt_number": 29
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.7, Page No.135"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "x=sys.getsizeof(int)\n",
+ "if x==2:\n",
+ " print \"The int data type has 2 bytes.\"\n",
+ "else:\n",
+ " print \"int doesn\u2019t have 2 bytes.\"\n",
+ "print \"The maximum value of int is: \"\n",
+ "if x!=2:\n",
+ " print ~(1 << x * 8 - 1)\n",
+ "else:\n",
+ " print ~(1 << 15)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "int doesn\u2019t have 2 bytes.\n",
+ "The maximum value of int is: \n",
+ "-491580764103542290389344003363488311062969810712349922950599774733041575563632045350838283083871987455171335931407342682680717201183371703685174955816593532050173778613198750299409984983114585069503870574316115011832273610552315180593120587012451297348595879648367038580742589916924929458075098309119941596150264781422412791167044744644512096092893664088575496131199113239879791394700259830388709521310004694953161827594685985046611263824408277271440340445151941401762175596309439311534804991519681288440882037066695104175933148121875339324910503310089608103880047567773082849318342395952108111196228471393174721112896352782400967939746829014435634293038974558987792428521448420075385030683955244112860897403710788795703260001867255101895634186646659237618203276865199656402546057935845979230559646064581353308805843310002383509210463719836898912777400961330646465730432059223747226730680060410613651809943713333186362580641534580104154278272333152706344741470433854227018577102016581210085064981107523450403116588706337588967284774946664250005782529\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/hour9.ipynb b/Teach_Yourself_C_in_24_Hours/hour9.ipynb new file mode 100755 index 00000000..c52a1ab2 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour9.ipynb @@ -0,0 +1,203 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:9985251f9ed527f5f6157524d1241d5f81fd588714602adf2cf638e46fdde236" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Hour 9: Working with data and Math functions" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.1, Page No.144" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Value in the textbook for 0xFF is wrong\n", + "ch = \"0xFF\"\n", + "x = \"0xFFFF\"\n", + "y = \"0xFFFF\"\n", + "print \"The decimal of signed 0xFF is \", int(ch, 16)\n", + "print \"The decimal of signed 0xFFFF is \", int(x, 16)\n", + "print \"The decimal of unsigned 0xFFFF is \", int(y, 16)\n", + "print \"The hex of decimal 12345 is 0x%x\" %(12345)\n", + "#answer in textbook for -12345 is wrong\n", + "print \"The hex of decimal -12345 is 0x%x\" %(-12345)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The decimal of signed 0xFF is 255\n", + "The decimal of signed 0xFFFF is 65535\n", + "The decimal of unsigned 0xFFFF is 65535\n", + "The hex of decimal 12345 is 0x3039\n", + "The hex of decimal -12345 is 0x-3039\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.2, Page No.146" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "print \"The size of short int is:\",sys.getsizeof(65535)\n", + "print \"The size of long int is:\",sys.getsizeof(65535*65535)\n", + "print \"The size of float is:\",sys.getsizeof(0.0)\n", + "# As python has no datatype like double, so that portion of program has not writeen here." + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The size of short int is: 12\n", + "The size of long int is: 18\n", + "The size of float is: 16\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Exmaple 9.3, Page No.147" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import array\n", + "x=0xFFFF\n", + "y=0xFFFF\n", + "s=0xFFFFFFFFl\n", + "t=0xFFFFFFFFL\n", + "print \"The short int of 0xFFFF is {:d}\".format(x)\n", + "print \"The unsigned int of 0xFFFF is {:d}\".format(y)\n", + "print \"The long int of 0xFFFFFFFFl is {:d}\".format(s)\n", + "print \"The long int of 0xFFFFFFFFl is {:d}\".format(t)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The short int of 0xFFFF is 65535\n", + "The unsigned int of 0xFFFF is 65535\n", + "The long int of 0xFFFFFFFFl is 4294967295\n", + "The long int of 0xFFFFFFFFl is 4294967295\n" + ] + } + ], + "prompt_number": 30 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.4, Page No.150" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "x=45.0\n", + "x=x*3.141593/180.0\n", + "print \"The sine of 45 is: \",math.asin(x)\n", + "print \"The sine of 45 is: \",math.acos(x)\n", + "print \"The tangent of 45 is: \",math.atan(x)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sine of 45 is: 0.903339250676\n", + "The sine of 45 is: 0.667457076119\n", + "The tangent of 45 is: 0.665773803591\n" + ] + } + ], + "prompt_number": 33 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.5, Page No.151" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "x=64.0\n", + "y=3.0\n", + "z=0.5\n", + "print \"pow(64.0,3.0) returns {:7.0f}\".format(math.pow(x,y))\n", + "print \"sqrt(64.0) returns {:2.0f}\".format(math.sqrt(x))\n", + "print \"pow(64.0,0.5) returns {:2.0f}\".format(math.pow(x,z))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "pow(64.0,3.0) returns 262144\n", + "sqrt(64.0) returns 8\n", + "pow(64.0,0.5) returns 8\n" + ] + } + ], + "prompt_number": 36 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Teach_Yourself_C_in_24_Hours/screenshots/1.png b/Teach_Yourself_C_in_24_Hours/screenshots/1.png Binary files differnew file mode 100755 index 00000000..8209b4ff --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/screenshots/1.png diff --git a/Teach_Yourself_C_in_24_Hours/screenshots/2.png b/Teach_Yourself_C_in_24_Hours/screenshots/2.png Binary files differnew file mode 100755 index 00000000..0cb942d5 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/screenshots/2.png diff --git a/Teach_Yourself_C_in_24_Hours/screenshots/3.png b/Teach_Yourself_C_in_24_Hours/screenshots/3.png Binary files differnew file mode 100755 index 00000000..3534e22c --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/screenshots/3.png |