From 62aa228e2519ac7b7f1aef53001f2f2e988a6eb1 Mon Sep 17 00:00:00 2001 From: hardythe1 Date: Fri, 30 Jan 2015 12:30:05 +0530 Subject: added books --- C++_Programming_In_Easy_Steps/Chapter1.ipynb | 200 ++++++++ C++_Programming_In_Easy_Steps/Chapter10.ipynb | 137 ++++++ C++_Programming_In_Easy_Steps/Chapter2.ipynb | 419 ++++++++++++++++ C++_Programming_In_Easy_Steps/Chapter3.ipynb | 352 ++++++++++++++ C++_Programming_In_Easy_Steps/Chapter4.ipynb | 335 +++++++++++++ C++_Programming_In_Easy_Steps/Chapter5.ipynb | 381 +++++++++++++++ C++_Programming_In_Easy_Steps/Chapter6.ipynb | 342 ++++++++++++++ C++_Programming_In_Easy_Steps/Chapter7.ipynb | 429 +++++++++++++++++ C++_Programming_In_Easy_Steps/Chapter8.ipynb | 603 ++++++++++++++++++++++++ C++_Programming_In_Easy_Steps/Chapter9.ipynb | 316 +++++++++++++ C++_Programming_In_Easy_Steps/README.txt | 10 + C++_Programming_In_Easy_Steps/screenshots/1.png | Bin 0 -> 40907 bytes C++_Programming_In_Easy_Steps/screenshots/2.png | Bin 0 -> 16108 bytes C++_Programming_In_Easy_Steps/screenshots/3.png | Bin 0 -> 13177 bytes Teach_Yourself_C_in_24_Hours/README.txt | 10 + Teach_Yourself_C_in_24_Hours/hour1.ipynb | 59 +++ Teach_Yourself_C_in_24_Hours/hour10.ipynb | 356 ++++++++++++++ Teach_Yourself_C_in_24_Hours/hour11.ipynb | 221 +++++++++ Teach_Yourself_C_in_24_Hours/hour12.ipynb | 311 ++++++++++++ Teach_Yourself_C_in_24_Hours/hour13.ipynb | 261 ++++++++++ Teach_Yourself_C_in_24_Hours/hour14.ipynb | 169 +++++++ Teach_Yourself_C_in_24_Hours/hour15.ipynb | 185 ++++++++ Teach_Yourself_C_in_24_Hours/hour16.ipynb | 426 +++++++++++++++++ Teach_Yourself_C_in_24_Hours/hour17.ipynb | 267 +++++++++++ Teach_Yourself_C_in_24_Hours/hour18.ipynb | 287 +++++++++++ Teach_Yourself_C_in_24_Hours/hour19.ipynb | 531 +++++++++++++++++++++ Teach_Yourself_C_in_24_Hours/hour2.ipynb | 50 ++ Teach_Yourself_C_in_24_Hours/hour20.ipynb | 459 ++++++++++++++++++ Teach_Yourself_C_in_24_Hours/hour21.ipynb | 277 +++++++++++ Teach_Yourself_C_in_24_Hours/hour22.ipynb | 365 ++++++++++++++ Teach_Yourself_C_in_24_Hours/hour23.ipynb | 232 +++++++++ Teach_Yourself_C_in_24_Hours/hour24.ipynb | 80 ++++ Teach_Yourself_C_in_24_Hours/hour3.ipynb | 103 ++++ Teach_Yourself_C_in_24_Hours/hour4.ipynb | 164 +++++++ Teach_Yourself_C_in_24_Hours/hour5.ipynb | 346 ++++++++++++++ Teach_Yourself_C_in_24_Hours/hour6.ipynb | 211 +++++++++ Teach_Yourself_C_in_24_Hours/hour7.ipynb | 306 ++++++++++++ Teach_Yourself_C_in_24_Hours/hour8.ipynb | 294 ++++++++++++ Teach_Yourself_C_in_24_Hours/hour9.ipynb | 203 ++++++++ Teach_Yourself_C_in_24_Hours/screenshots/1.png | Bin 0 -> 21732 bytes Teach_Yourself_C_in_24_Hours/screenshots/2.png | Bin 0 -> 27316 bytes Teach_Yourself_C_in_24_Hours/screenshots/3.png | Bin 0 -> 39379 bytes 42 files changed, 9697 insertions(+) create mode 100755 C++_Programming_In_Easy_Steps/Chapter1.ipynb create mode 100755 C++_Programming_In_Easy_Steps/Chapter10.ipynb create mode 100755 C++_Programming_In_Easy_Steps/Chapter2.ipynb create mode 100755 C++_Programming_In_Easy_Steps/Chapter3.ipynb create mode 100755 C++_Programming_In_Easy_Steps/Chapter4.ipynb create mode 100755 C++_Programming_In_Easy_Steps/Chapter5.ipynb create mode 100755 C++_Programming_In_Easy_Steps/Chapter6.ipynb create mode 100755 C++_Programming_In_Easy_Steps/Chapter7.ipynb create mode 100755 C++_Programming_In_Easy_Steps/Chapter8.ipynb create mode 100755 C++_Programming_In_Easy_Steps/Chapter9.ipynb create mode 100755 C++_Programming_In_Easy_Steps/README.txt create mode 100755 C++_Programming_In_Easy_Steps/screenshots/1.png create mode 100755 C++_Programming_In_Easy_Steps/screenshots/2.png create mode 100755 C++_Programming_In_Easy_Steps/screenshots/3.png create mode 100755 Teach_Yourself_C_in_24_Hours/README.txt create mode 100755 Teach_Yourself_C_in_24_Hours/hour1.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour10.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour11.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour12.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour13.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour14.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour15.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour16.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour17.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour18.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour19.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour2.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour20.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour21.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour22.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour23.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour24.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour3.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour4.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour5.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour6.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour7.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour8.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/hour9.ipynb create mode 100755 Teach_Yourself_C_in_24_Hours/screenshots/1.png create mode 100755 Teach_Yourself_C_in_24_Hours/screenshots/2.png create mode 100755 Teach_Yourself_C_in_24_Hours/screenshots/3.png diff --git a/C++_Programming_In_Easy_Steps/Chapter1.ipynb b/C++_Programming_In_Easy_Steps/Chapter1.ipynb new file mode 100755 index 00000000..ba2c8125 --- /dev/null +++ b/C++_Programming_In_Easy_Steps/Chapter1.ipynb @@ -0,0 +1,200 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:accfa65c82343aeed5f184613989e8f303b6335bf19049dd322738d1012e1aff" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1 : Getting Started" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.1, Page No 12" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Hello World!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello World!\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.2, Page No 17" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "letter = 'A'\n", + "number = 100\n", + "decimal = 7.5\n", + "pi = 3.14159\n", + "isTrue = \"false\"\n", + "print \"char letter: \",letter\n", + "print \"int number: \",number\n", + "print \"float decimal: \",decimal\n", + "print \"double pi: \",pi\n", + "print \"bool isTrue: \",isTrue" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "char letter: A\n", + "int number: 100\n", + "float decimal: 7.5\n", + "double pi: 3.14159\n", + "bool isTrue: false\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.3, Page No 19" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "nums = [1.5,2.75,3.25]\n", + "name = ['m','i','k','e','\\0']\n", + "coords = [(1,2,3),(4,5,6)]\n", + "print \"nums[0]: \",nums[0]\n", + "print \"nums[1]: \",nums[1]\n", + "print \"nums[2]: \",nums[2]\n", + "print \"name[0]: \",name[0]\n", + "print \"Test stirng: \", \"\".join(name)\n", + "print \"coords[0][2]: \",coords[0][2]\n", + "print \"coords[1][2]: \",coords[1][2]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "nums[0]: 1.5\n", + "nums[1]: 2.75\n", + "nums[2]: 3.25\n", + "name[0]: m\n", + "Test stirng: mike\u0000\n", + "coords[0][2]: 3\n", + "coords[1][2]: 6\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.4, Page No 21" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# ipython does not support vector array" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.5, Page No 23" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "PI = 3.1415926536\n", + "print \"6\\\" circle circumference: \" ,(PI * 6)\n", + "#ENUM IS NOT IN PYTHON SO DECLARED DIRECTLY\n", + "RED = 1\n", + "YELLOW = 2\n", + "GREEN = 3\n", + "BROWN = 4\n", + "BLUE = 5\n", + "PINK = 5\n", + "BLACK = 6\n", + "print \"I shot a red worth: \",RED\n", + "print \"Then a blue worth: \",BLUE\n", + "print \"Total scored: \",(RED+BLUE)\n", + "#typedef and enum is not in python so declared directly\n", + "neutral = \"NEGATIVE \"\n", + "live = \"POSITIVE\"\n", + "print \"Neutral wire: \", neutral\n", + "print \"Live wire: \", live" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\" circle circumference: 18.8495559216\n", + "I shot a red worth: 1\n", + "Then a blue worth: 5\n", + "Total scored: 6\n", + "Neutral wire: NEGATIVE \n", + "Live wire: POSITIVE\n" + ] + } + ], + "prompt_number": 19 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/C++_Programming_In_Easy_Steps/Chapter10.ipynb b/C++_Programming_In_Easy_Steps/Chapter10.ipynb new file mode 100755 index 00000000..7e4467fb --- /dev/null +++ b/C++_Programming_In_Easy_Steps/Chapter10.ipynb @@ -0,0 +1,137 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:f8a96fa9f559e8169a19de38e190920c7c87ad5a2ecae3d068468c11c8ecc55d" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 10 : Programming visually" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 10.1, Page No 171" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import random\n", + "nums = range(50)\n", + "for i in range(1,50):\n", + " nums[i] = i\n", + "for i in range(1,50):\n", + " j = random.randrange(1,49, 1)\n", + " k = nums[i]\n", + " nums[i] = nums[j]\n", + " nums[j] = k\n", + "print \"Your six lucky numbers are: \"\n", + "for i in range(1,7):\n", + " print nums[i],\" \",\n", + " \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your six lucky numbers are: \n", + "30 5 38 20 7 29 \n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 10.2, Page No 173" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from Tkinter import *\n", + "from PIL import Image, ImageTk\n", + "import random\n", + "\n", + "def getBtnClick():\n", + " nums = range(50)\n", + " for i in range(1,50):\n", + " nums[i] = i\n", + " for i in range(1,50):\n", + " j = random.randrange(1,49, 1)\n", + " k = nums[i]\n", + " nums[i] = nums[j]\n", + " nums[j] = k\n", + " label1.config(text = nums[0])\n", + " label2.config(text = nums[1])\n", + " label3.config(text = nums[2])\n", + " label4.config(text = nums[3])\n", + " label5.config(text = nums[4])\n", + " label6.config(text = nums[5])\n", + " getBtn.config(state=DISABLED)\n", + " resetBtn.config(state=NORMAL)\n", + "def resetBtnClick():\n", + " label1.config(text = \"...\")\n", + " label2.config(text = \"...\")\n", + " label3.config(text = \"...\")\n", + " label4.config(text = \"...\")\n", + " label5.config(text = \"...\")\n", + " label6.config(text = \"...\")\n", + " resetBtn.config(state=DISABLED)\n", + " getBtn.config(state=NORMAL)\n", + "root = Tk()\n", + "root.title(\"Lotto number generator\")\n", + "image = Image.open(\"lotto.gif\")\n", + "photo = ImageTk.PhotoImage(image)\n", + "label0 = Label(image=photo)\n", + "label0.image = photo\n", + "label0.grid(row=0,column=0,rowspan=2)\n", + "\n", + "label1 = Label(text=\"...\")\n", + "label1.grid(row=0,column=1,padx=5,pady=5)\n", + "label2 = Label(text=\"...\")\n", + "label2.grid(row=0,column=2,padx=5,pady=5)\n", + "label3 = Label(text=\"...\")\n", + "label3.grid(row=0,column=3,padx=5,pady=5)\n", + "label4 = Label(text=\"...\")\n", + "label4.grid(row=0,column=4,padx=5,pady=5)\n", + "label5 = Label(text=\"...\")\n", + "label5.grid(row=0,column=5,padx=5,pady=5)\n", + "label6 = Label(text=\"...\")\n", + "label6.grid(row=0,column=6,padx=5,pady=5)\n", + "\n", + "getBtn = Button(text=\"Get My Lucky Numbers\",command=getBtnClick)\n", + "getBtn.grid(row=1,column=2,columnspan=2)\n", + "resetBtn = Button(text=\"Reset\",state=DISABLED,command=resetBtnClick)\n", + "resetBtn.grid(row=1,column=4,columnspan=3)\n", + "root.mainloop()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/C++_Programming_In_Easy_Steps/Chapter2.ipynb b/C++_Programming_In_Easy_Steps/Chapter2.ipynb new file mode 100755 index 00000000..cd57ef03 --- /dev/null +++ b/C++_Programming_In_Easy_Steps/Chapter2.ipynb @@ -0,0 +1,419 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:1a5f4b4db0e022cc4962fee72ac059eb139d26a2030ea11b6f3c9bb4ceecd474" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 2 : Performing operations" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.1, Page No : 27" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = 8\n", + "b = 4\n", + "print \"Addition result: \",(a+b)\n", + "print \"Substraction result: \",(a-b)\n", + "print \"Multiplication result: \",(a*b)\n", + "print \"Division result: \",(a/b)\n", + "print \"Modulus result: \",(a%b)\n", + "print \"Postfix increment: \",a+1\n", + "print \"Postfix result: \",a\n", + "print \"Prefix increment: \",b+1\n", + "print \"Prefix result: \",b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Addition result: 12\n", + "Substraction result: 4\n", + "Multiplication result: 32\n", + "Division result: 2\n", + "Modulus result: 0\n", + "Postfix increment: 9\n", + "Postfix result: 8\n", + "Prefix increment: 5\n", + "Prefix result: 4\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.2, Page No : 29" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = 8\n", + "b = 4\n", + "print \"Assigned values: \"\n", + "print \"a = \",a\n", + "print \"b = \",b\n", + "print \"Add & assign: \"\n", + "a = a + b\n", + "print \"a += b (8 += 4 ) a = \",a\n", + "print \"Subtract & assign: \"\n", + "a = a - b\n", + "print \"a -= b (12 -= 4 ) a = \",a\n", + "print \"Multiply & assign: \"\n", + "a = a * b\n", + "print \"a *= b (8 *= 4 ) a = \",a\n", + "print \"Divide & assign: \"\n", + "a = a / b\n", + "print \"a /= b (32 /= 4 ) a = \",a\n", + "print \"Modulus & assign: \"\n", + "a = a % b\n", + "print \"a %= b (8 %= 4 ) a = \",a" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Assigned values: \n", + "a = 8\n", + "b = 4\n", + "Add & assign: \n", + "a += b (8 += 4 ) a = 12\n", + "Subtract & assign: \n", + "a -= b (12 -= 4 ) a = 8\n", + "Multiply & assign: \n", + "a *= b (8 *= 4 ) a = 32\n", + "Divide & assign: \n", + "a /= b (32 /= 4 ) a = 8\n", + "Modulus & assign: \n", + "a %= b (8 %= 4 ) a = 0\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3, Page No 31" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "nil= 0\n", + "num= 0\n", + "max1 = 1\n", + "cap= 'A'\n", + "low= 'a'\n", + "print \"Equality comparisons: \"\n", + "print \"(0 == 0) \",(nil == num),\"(true)\"\n", + "print \"(A == a) \",(cap == low),\"(false)\"\n", + "print \"Inequality comparison: \"\n", + "print \"(0 != 1) \",(nil != max1),\"(true)\"\n", + "print \"Greater comparison: \"\n", + "print \"(0 > 1)\",(nil > max1),\"(false)\"\n", + "print \"Lesser comparison: \"\n", + "print \"(0 < 1) \",(nil < max1),\"(true)\"\n", + "print \"Greater or equal comparison: \"\n", + "print \"(0 >= 0) \",(nil >= num),\"(true)\"\n", + "print \"Lesser or equal comparison: \"\n", + "print \"(1 <= 0) \",(max1 <= num),\"(false)\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Equality comparisons: \n", + "(0 == 0) True (true)\n", + "(A == a) False (false)\n", + "Inequality comparison: \n", + "(0 != 1) True (true)\n", + "Greater comparison: \n", + "(0 > 1) False (false)\n", + "Lesser comparison: \n", + "(0 < 1) True (true)\n", + "Greater or equal comparison: \n", + "(0 >= 0) True (true)\n", + "Lesser or equal comparison: \n", + "(1 <= 0) False (false)\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.4, Page No : 33" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = 1\n", + "b = 0\n", + "print \"AND logic: \"\n", + "print \"(a && a) \",(a & a),\"(true)\"\n", + "print \"(a && b) \",(a & b),\"(false)\"\n", + "print \"(b && b) \",(b & b),\"(false)\"\n", + "print \"OR logic: \"\n", + "print \"(a || a) \",( a | a ),\"(true)\"\n", + "print \"(a || b) \",( a | b ),\"(true)\"\n", + "print \"(b || b) \",( b | b ),\"(false)\"\n", + "print \"NOT logic: \"\n", + "print \"a = \",a,\"!a = \",not a\n", + "print \"b = \",b,\"!b = \",not b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "AND logic: \n", + "(a && a) 1 (true)\n", + "(a && b) 0 (false)\n", + "(b && b) 0 (false)\n", + "OR logic: \n", + "(a || a) 1 (true)\n", + "(a || b) 1 (true)\n", + "(b || b) 0 (false)\n", + "NOT logic: \n", + "a = 1 !a = False\n", + "b = 0 !b = True\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Examle 2.5, Page No : 35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = 1\n", + "b = 2\n", + "print \"Variable a value is: \"\n", + "if a != 1:\n", + " print \"not one\"\n", + "else:\n", + " print \"one\"\n", + "if a % 2 != 0:\n", + " print \"odd\"\n", + "else:\n", + " print \"even\"\n", + "print \"Variable b value is: \"\n", + "if b != 1:\n", + " print \"not one\"\n", + "else:\n", + " print \"one\"\n", + "if b % 2 != 0:\n", + " print \"odd\"\n", + "else:\n", + " print \"even\"\n", + "if a > b:\n", + " max1 = a\n", + "else:\n", + " max1 = b\n", + "print \"Greater value is \",max1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Variable a value is: \n", + "one\n", + "odd\n", + "Variable b value is: \n", + "not one\n", + "even\n", + "Greater value is 2\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.6, Page No 37" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# there are no few datatypes in ipython so i assigned manually to get idea about size\n", + "import sys\n", + "num = 1\n", + "nums = range(50)\n", + "number = 0\n", + "max1 = 100\n", + "pi = 3.14\n", + "decimal = 10.10\n", + "letter = 'a'\n", + "letters = \"hii\"\n", + "isTrue = \"true\"\n", + "print \"int size: \",sys.getsizeof(num)\n", + "print \"50 int size: \",sys.getsizeof(nums)\n", + "print \"short int size: \",sys.getsizeof(number)\n", + "print \"unsigned int size: \",sys.getsizeof(max1)\n", + "print \"double size: \",sys.getsizeof(pi)\n", + "print \"float size: \",sys.getsizeof(decimal)\n", + "print \"char size: \",sys.getsizeof(letter)\n", + "print \"50 char size: \",sys.getsizeof(letters)\n", + "print \"bool size: \",sys.getsizeof(isTrue)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " int size: 24\n", + "50 int size: 464\n", + "short int size: 24\n", + "unsigned int size: 24\n", + "double size: 24\n", + "float size: 24\n", + "char size: 34\n", + "50 char size: 36\n", + "bool size: 37\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7, Page No 39" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "num = 1 + 4 * 3\n", + "print \"Default order: \",num\n", + "num = (1 + 4) * 3\n", + "print \"Forced order: \",num\n", + "num = 7 - 4 + 2\n", + "print \"Default direction: \",num\n", + "num = 7 - (4 + 2)\n", + "print \"Forced direction: \",num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Default order: 13\n", + "Forced order: 15\n", + "Default direction: 5\n", + "Forced direction: 1\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.8, Page No 41" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "num= 7\n", + "factor= 2\n", + "letter= 'A'\n", + "result= 0.0;\n", + "print \"Integer division: \",(num/factor)\n", + "result = float(num) / factor\n", + "print \"Cast division float: \",result\n", + "num = ord(letter)\n", + "print \"Cast character int: \",num\n", + "letter = chr(70)\n", + "print \"Cast integer char: \",letter" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Integer division: 3\n", + "Cast division float: 3.5\n", + "Cast character int: 65\n", + "Cast integer char: F\n" + ] + } + ], + "prompt_number": 9 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/C++_Programming_In_Easy_Steps/Chapter3.ipynb b/C++_Programming_In_Easy_Steps/Chapter3.ipynb new file mode 100755 index 00000000..fa5bbe49 --- /dev/null +++ b/C++_Programming_In_Easy_Steps/Chapter3.ipynb @@ -0,0 +1,352 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:78ef4056dbbb5cf9345972f218d0d597af5ce86eb47594e9fba3eaa74e6511a9" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 3 : Making statements" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.1, Page No 45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "num = 8\n", + "letter = 'A'\n", + "if num > 5:\n", + " print \"Number exceeds five\"\n", + "else: \n", + " \"Number is five or less\"\n", + "if letter == 'A':\n", + " print \"Letter is A\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number exceeds five\n", + "Letter is A\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.2, Page No 47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "num = 3\n", + "if num == 1:\n", + " print num,\" : Monday\"\n", + "elif num == 2:\n", + " print num,\" : Tuesday\"\n", + "elif num == 3:\n", + " print num,\" : Wednesday\"\n", + "elif num == 4:\n", + " print num,\" : Thursday\"\n", + "elif num == 5:\n", + " print num,\" : Friday\"\n", + "else:\n", + " print \"Weekend day\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "3 : Wednesday\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.3, Page No 48" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "for i in range(1,4):\n", + " print \"Loop iteration: \",i\n", + " for j in range(1,4):\n", + " print \" Inner loop iteration: \",j" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Loop iteration: 1\n", + " Inner loop iteration: 1\n", + " Inner loop iteration: 2\n", + " Inner loop iteration: 3\n", + "Loop iteration: 2\n", + " Inner loop iteration: 1\n", + " Inner loop iteration: 2\n", + " Inner loop iteration: 3\n", + "Loop iteration: 3\n", + " Inner loop iteration: 1\n", + " Inner loop iteration: 2\n", + " Inner loop iteration: 3\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.4, Page No 50" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "vec = range(10)\n", + "i = 0\n", + "print \n", + "while i < len(vec):\n", + " i = i + 1\n", + " if i == 3:\n", + " print \"| Skipped\",\n", + " continue\n", + " if i == 8:\n", + " print \"Done\"\n", + " break\n", + " vec[i-1] = i\n", + " print \"|\",vec[i-1]," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "| 1 | 2 | Skipped | 4 | 5 | 6 | 7 Done\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.5, Page No 53" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def bodyTempC():\n", + " temperature = 37.0\n", + " return temperature\n", + "def bodyTempF():\n", + " temperature = 98.6\n", + " return temperature\n", + "print \"Centigrade: \",bodyTempC()\n", + "print \"Fahrenheit: \",bodyTempF()\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Centigrade: 37.0\n", + "Fahrenheit: 98.6\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.6, Page No 54" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def fToC(degreesF = 32.0):\n", + " degreesC = ((5.0/9.0) * (degreesF - 32.0))\n", + " return degreesC\n", + "fahrenheit = float(raw_input(\"Enter a Fahrenheit temperature:\\t\"))\n", + "centigrade = fToC(fahrenheit)\n", + "print fahrenheit,\"F is \",centigrade,\"C\"\n", + "print \"Freezing point: \",fToC(),\"C\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a Fahrenheit temperature:\t98.6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 98.6 F is 37.0 C\n", + "Freezing point: 0.0 C\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.7, Page No 56" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def computeArea(diameter):\n", + " radius = (diameter/2)\n", + " return (3.141593 * (radius * radius))\n", + "def computeArea(width,height):\n", + " return width * height\n", + "def computeArea(letter,width,height):\n", + " return ((width/2) * height)\n", + "num = float(raw_input(\"Enter dimension in feet: \"))\n", + "# python does not support overriding in this way\n", + "#area = computeArea(num)\n", + "#print \"Circle: Area = \",area,\" sq.ft.\"\n", + "#area= computeArea(num,num)\n", + "#print\"Square: Area = \",area,\" sq.ft.\"\n", + "area = computeArea('T',num,num)\n", + "print \"Triangle: Area = \",area,\" sq.ft.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter dimension in feet: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Triangle: Area = 8.0 sq.ft.\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.8, Page No 58" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def computeFactorials(num,max1):\n", + " print \"Factorial of \",num,\":\",\n", + " print factorial(num)\n", + " num = num + 1\n", + " if num > max1:\n", + " return 0\n", + " else :\n", + " computeFactorials(num,max1)\n", + "def factorial(n):\n", + " if n == 1:\n", + " result = 1\n", + " else:\n", + " result = (factorial(n - 1) * n)\n", + " return result\n", + "computeFactorials( 1, 8) ;" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Factorial of 1 : 1\n", + "Factorial of 2 : 2\n", + "Factorial of 3 : 6\n", + "Factorial of 4 : 24\n", + "Factorial of 5 : 120\n", + "Factorial of 6 : 720\n", + "Factorial of 7 : 5040\n", + "Factorial of 8 : 40320\n" + ] + } + ], + "prompt_number": 28 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/C++_Programming_In_Easy_Steps/Chapter4.ipynb b/C++_Programming_In_Easy_Steps/Chapter4.ipynb new file mode 100755 index 00000000..ae827529 --- /dev/null +++ b/C++_Programming_In_Easy_Steps/Chapter4.ipynb @@ -0,0 +1,335 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:b79ee2681573b5fe1113810718887e0801a441cdcbacd23dfe8fbd78765cba80" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4 : Handling strings" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.1, Page No 63" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "text = \"9\"\n", + "term = \"9 \"\n", + "info = \"Toys\"\n", + "hue = ['R','e','d','\\0']\n", + "info = \" Balloons\"\n", + "color = \"\".join(hue)\n", + "text = text + term + color + info\n", + "print text" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "99 Red\u0000 Balloons\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.2, Page No 64" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "name = raw_input(\"Please enter your full name : \")\n", + "print \"Welcome \",name\n", + "name = raw_input(\"Please re-enter your full name : \")\n", + "print \"Thanks, \",name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter your full name : Mike McGrath\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Welcome Mike McGrath\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please re-enter your full name : Mike McGrath\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Thanks, Mike McGrath\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.3, Page No 66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "term = \"100\"\n", + "number = 100\n", + "stream = term\n", + "num = stream\n", + "num = number / 4\n", + "print \"Integer value: \",num\n", + "stream = \"\"\n", + "stream = number\n", + "text = stream\n", + "text = str(text) + \" Per Cent\"\n", + "print \"String value: \",text" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Integer value: 25\n", + "String value: 100 Per Cent\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.4, Page No 68" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#There is no such functions in python like size, capacity and empty " + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.5, Page No 70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "lang = \"C++\"\n", + "term = \"Programming\"\n", + "text = \"C++ Programming\"\n", + "print \"Concatinated: \",lang + term\n", + "print \"Original: \",lang\n", + "#print \"Appended: \",lang.append(term) there in no append method for string in python\n", + "print \"Original: \",lang\n", + "print \"Differ: \",(lang==term)\n", + "print \"Match: \",(lang==text)\n", + "# print \"Match: \",(lang.compare(text)) there in no compare method for string in python\n", + "# print \"Differ: \",(lang.compare(term)) there in no compare method for string in python\n", + "# print \"Lower ASCII: \",lang.compare(\"zzzzz\") there in no compare method for string in python" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Concatinated: C++Programming\n", + "Original: C++\n", + "Original: C++\n", + "Differ: False\n", + "Match: False\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.6, Page No 72" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "text = \"Always laugh when you can. It\\\u2019s cheap medicine.\"\n", + "front = text\n", + "print \"Front: \",front\n", + "front = text\n", + "print \"Front: \",front\n", + "back = text\n", + "print \"Back: \",back\n", + "back = front\n", + "print \"Front: \",front\n", + "print \"Back: \",back\n", + "#There is no option to assing specific length in python also no swap function" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Front: Always laugh when you can. It\\\u2019s cheap medicine.\n", + "Front: Always laugh when you can. It\\\u2019s cheap medicine.\n", + "Back: Always laugh when you can. It\\\u2019s cheap medicine.\n", + "Front: Always laugh when you can. It\\\u2019s cheap medicine.\n", + "Back: Always laugh when you can. It\\\u2019s cheap medicine.\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.7, Page No 74" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "text = \"I can resist anything but temptation.\"\n", + "num = text.find(\"resist\",0)\n", + "print \"Position: \",num\n", + "num= text.find(\"nonsuch\",0)\n", + "print \"Result: \",num\n", + "num= text.find(\"I\",0)\n", + "print \"First I\",num\n", + "num= text.find(\"i\",text.find(\"I\",0))\n", + "print \"First not I\",num\n", + "num= text.rfind(\"t\",0)\n", + "print \"Last t: \",num\n", + "num= text.rfind(\"t\",text.rfind(\"t\",0))\n", + "print \"Last not t: \",num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Position: 6\n", + "Result: -1\n", + "First I 0\n", + "First not I 9\n", + "Last t: 32\n", + "Last not t: 32\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4.8, Page No 76" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "text = \"I do like the seaside\"\n", + "print \"Original: \",text\n", + "# text.insert(10,\"to be aside\") There is no insert method in python\n", + "print \"Inserted: \",text\n", + "# text.erase(2,3) There is no erase method in python\n", + "print \"Erased: \",text\n", + "text = text.replace(\"the seaside\",\"strolling by the sea\")\n", + "print \"Replaced: \",text\n", + "print \"Copied: \",text[10:19]\n", + "print \"Last character: \",text[len(text)-1]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original: I do like the seaside\n", + "Inserted: I do like the seaside\n", + "Erased: I do like the seaside\n", + "Replaced: I do like strolling by the sea\n", + "Copied: strolling\n", + "Last character: a\n" + ] + } + ], + "prompt_number": 46 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/C++_Programming_In_Easy_Steps/Chapter5.ipynb b/C++_Programming_In_Easy_Steps/Chapter5.ipynb new file mode 100755 index 00000000..5f544b56 --- /dev/null +++ b/C++_Programming_In_Easy_Steps/Chapter5.ipynb @@ -0,0 +1,381 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:be4a857e05d2149d20f6d26486c5784a7f1b2096e43de372a2b4b95639d8b93a" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 5 : Reading and writing files" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.1, Page No 81" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "poem = [\"\\n\\tI never saw a man who looked\"]\n", + "poem.append(\"\\n\\tWith such a wistful eye\")\n", + "poem.append(\"\\n\\tUpon that little tent of blue\")\n", + "poem.append(\"\\n\\tWhich prisoners call the sky\")\n", + "try:\n", + " writer = open(\"poem.txt\",\"w\")\n", + " if(not isinstance(writer,file)):\n", + " print \"Error opening file for output\"\n", + " writer.writelines(poem)\n", + " writer.close()\n", + "except IOError:\n", + " print \"Error opening file for output\"\n", + "\"\"\"\n", + "Out put Text File :- poem.txt\n", + "\n", + " I never saw a man who looked\n", + " With such a wistful eye\n", + " Upon that little tent of blue\n", + " Which prisoners call the sky\n", + " The Ballad of Reading Gaol\n", + " Oscar Wilde 1898\n", + " \n", + "\"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.2, Page No 83" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "info = \"\\n\\tThe Ballad of Reading Gaol\"\n", + "info = info + \"\\n\\t\\t\\tOscar Wilde 1898\" #There is no append for string in python\n", + "try:\n", + " writer = open(\"poem.txt\",\"a\")\n", + " if(not isinstance(writer,file)):\n", + " print \"Error opening file for output\"\n", + " writer.writelines(info)\n", + " writer.close()\n", + "except IOError:\n", + " print \"Error opening file for output\"" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.3, Page No 84" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "try:\n", + " reader = open(\"poem.txt\",\"r\")\n", + " if(not isinstance(reader,file)):\n", + " print \"Error opening input file\"\n", + " i = 0\n", + " while(1):\n", + " c = reader.read(1)\n", + " if c == \"\":\n", + " break\n", + " else:\n", + " print c,\n", + " i= i + 1 \n", + " reader.close()\n", + " print \"\\nIterations: \",i\n", + "except IOError:\n", + " print \"Error opening input file\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "\tI n e v e r s a w a m a n w h o l o o k e d \n", + "\tW i t h s u c h a w i s t f u l e y e \n", + "\tU p o n t h a t l i t t l e t e n t o f b l u e \n", + "\tW h i c h p r i s o n e r s c a l l t h e s k y \n", + "\tT h e B a l l a d o f R e a d i n g G a o l \n", + "\t\t\tO s c a r W i l d e 1 8 9 8 \n", + "Iterations: 164\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.4, Page No 86" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# this program gives output same as book program but logic is different because some of functions are not available in python\n", + "RANGE = 12\n", + "tab = range(RANGE)\n", + "i = 0\n", + "j = 1\n", + "t = \"\"\n", + "try:\n", + " reader = open(\"records.txt\",\"r\")\n", + " if(not isinstance(reader,file)):\n", + " print \"Error opening input file\"\n", + " while(1):\n", + " c = reader.read(1)\n", + " if c == \"\":\n", + " break\n", + " else:\n", + " t = t + c\n", + " if c == '\\t':\n", + " tab[i] = t\n", + " t = \"\"\n", + " i = i + 1\n", + " elif c == '\\n':\n", + " tab[i] = t\n", + " t = \"\"\n", + " i = i + 1\n", + " elif i == RANGE-1:\n", + " tab[i] = t\n", + " reader.close()\n", + " i= 0;\n", + " while i < RANGE:\n", + " print \"Record Number: \",j\n", + " j = j + 1\n", + " print \"Forename: \",tab[i]\n", + " i = i + 1\n", + " print \"Surname: \",tab[i]\n", + " i = i + 1\n", + " print \"Department: \",tab[i]\n", + " i = i + 1\n", + " print \"Telephone: \",tab[i]\n", + " i = i + 1\n", + "except IOError:\n", + " print \"Error opening input file\"\n", + "\n", + "\"\"\"\n", + " INPUT FILE :- records.txt\n", + " \n", + " John Smith Sales 555-1234\n", + " Mary Jones Wages 555-9876\n", + " Paul Harris Accts 555-4321\n", + " \n", + "\"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Record Number: 1\n", + "Forename: John\t\n", + "Surname: Smith\t\n", + "Department: Sales\t\n", + "Telephone: 555-1234\n", + "\n", + "Record Number: 2\n", + "Forename: Mary\t\n", + "Surname: Jones\t\n", + "Department: Wages\t\n", + "Telephone: 555-9876\n", + "\n", + "Record Number: 3\n", + "Forename: Paul\t\n", + "Surname: Harris\t\n", + "Department: Accts\t\n", + "Telephone: 555-4321\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.5, Page No 89" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "isTrue = 1\n", + "num = 255\n", + "for i in range(0,40):\n", + " print \".\",\n", + "print \"Output\"\n", + "print \"Pi: \",\"3.1415926536\"\n", + "print isTrue,\":\",bool(isTrue)\n", + "print num,\":\",\"{:01X}\".format(num)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Output\n", + "Pi: 3.1415926536\n", + "1 : True\n", + "255 : FF\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.6, Page No 91" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "try:\n", + " for number in range(1,21):\n", + " if number > 4:\n", + " raise Exception(number)\n", + " else:\n", + " print \"Number: \",number\n", + "except:\n", + " print \"Exception at: \",number" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number: 1\n", + "Number: 2\n", + "Number: 3\n", + "Number: 4\n", + "Exception at: 5\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.7, Page No 92" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "lang = \"C++\"\n", + "try:\n", + " lang.erase(4,6)\n", + "except:\n", + " print \"Exception\",sys.exc_info()[0]\n", + "# There is no support for erse method in python" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Exception \n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5.8, Page No 94" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#there is no replace or resize method in python\n", + "import sys\n", + "lang = \"C++\"\n", + "num = 1000000000\n", + "try:\n", + " lang.replace(lang,\"C\",\"1\")\n", + " lang.resize(3*num)\n", + " reader = open(\"nonsuch.txt\",\"r\")\n", + " print \"Program continues...\"\n", + "except:\n", + " print sys.exc_info()[0]\n", + " print \"Program terminated.\"\n", + "#python dose not support much more exceptions" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Program terminated.\n" + ] + } + ], + "prompt_number": 29 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/C++_Programming_In_Easy_Steps/Chapter6.ipynb b/C++_Programming_In_Easy_Steps/Chapter6.ipynb new file mode 100755 index 00000000..281e89b4 --- /dev/null +++ b/C++_Programming_In_Easy_Steps/Chapter6.ipynb @@ -0,0 +1,342 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:ebda2195f5f56b03f9c5aecb268e628cfb74a1a1fa1ea18ae277cc3f964d54bd" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 6: Pointing to data" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.1, Page No.98" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "num=100\n", + "sum=0.0123456789\n", + "text=\"C++ Fun\"\n", + "print \"Integer variable starts at\",hex(id(num))\n", + "print \"Double variable starts at\",hex(id(sum))\n", + "print \"String variable starts at\",hex(id(text))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Integer variable starts at 0x126eb04\n", + "Double variable starts at 0x279b490\n", + "String variable starts at 0x2b7a160\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.2, Page No.100" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a=8\n", + "b=16\n", + "aPtr=id(a)\n", + "bPtr=id(b)\n", + "print \"Address of pointers...\"\n", + "print \"aPtr:\",hex(id(aPtr))\n", + "print \"bPtr:\",hex(id(bPtr))\n", + "print \"Values in pointers...\"\n", + "print \"aPtr:\",hex(aPtr)\n", + "print \"bPtr:\",hex(bPtr)\n", + "#As python does not support pointers values can't be displayed using * symbol" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of pointers...\n", + "aPtr: 0x2808ac8\n", + "bPtr: 0x2808b1c\n", + "Values in pointers...\n", + "aPtr: 0x126e794\n", + "bPtr: 0x126e734\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.3, Page No.102" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "nums={1,2,3,4,5,6,7,8,9,10}\n", + "ptr=id(nums)\n", + "#As python does not support concept of pointers we can not use pointer arithmetic to print numbers" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.4, Page No.104" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "num=5\n", + "def writeOutput(value):\n", + " print \"Current Value: \",value\n", + "def computeTriple(value):\n", + " global num\n", + " num=num*3\n", + "\n", + "\n", + "writeOutput(num)\n", + "num=num+15\n", + "writeOutput(num)\n", + "computeTriple(num)\n", + "writeOutput(num)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Current Value: 5\n", + "Current Value: 20\n", + "Current Value: 60\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.5, Page No.106" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "letters=['C','+','+',' ','F','u','n','\\0']\n", + "text=\"C++ Fun\"\n", + "term=\"Element\"\n", + "lang=\"C++\"\n", + "ap1=[\"Great \",\"Program \",\"Code \"]\n", + "ap2=[lang,\"is \",\"Fun\"]\n", + "ap3=[ap2[0],ap2[1],ap1[0]]\n", + "ap4=[ap1[2],ap2[1],ap2[2]]\n", + "print \"\".join(letters)\n", + "print text\n", + "for i in range(0,3):\n", + " print term,i,\" \",\n", + " print ap1[i],\" \",\n", + " print ap2[i],\" \",\n", + " print ap3[i],\" \",\n", + " print ap4[i],\" \"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "C++ Fun\u0000\n", + "C++ Fun\n", + "Element 0 Great C++ C++ Code \n", + "Element 1 Program is is is \n", + "Element 2 Code Fun Great Fun \n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.6, Page No.108" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "num=0\n", + "rNum=400\n", + "num=rNum\n", + "print \"Value direct\",num\n", + "print \"Value via reference \",rNum\n", + "\n", + "print \"Address direct\",hex(id(num))\n", + "print \"Address via reference\",hex(id(rNum))\n", + "\n", + "rNum=rNum*2\n", + "num=rNum\n", + "\n", + "print \"Value direct\",num\n", + "print \"Value via reference \",rNum\n", + "\n", + "#as python does not support pointers" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value direct 400\n", + "Value via reference 400\n", + "Address direct 0x27cd294\n", + "Address via reference 0x27cd294\n", + "Value direct 800\n", + "Value via reference 800\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.7, Page No. 110" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def writeOutput(value):\n", + " print \"Current Value: \",value\n", + "\n", + "def computeTriple(value):\n", + " value=value*3\n", + " print \"Current Value: \",value\n", + " \n", + "num=5\n", + "ref=num\n", + "writeOutput(num)\n", + "num=num+15\n", + "writeOutput(num)\n", + "computeTriple(num)\n", + "\n", + "#as python does not support pointers I dont have used pointer in this program" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Current Value: 5\n", + "Current Value: 20\n", + "Current Value: 60\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.8, Page No.113" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def add(a,b):\n", + " total=a+b\n", + " print \"total:\",total\n", + "\n", + "num=100\n", + "sum=500\n", + "rNum=num\n", + "ptr=num\n", + "print \"Reference:\",rNum\n", + "print \"Pointer:\",ptr\n", + "ptr=sum\n", + "print \"Pointer now:\",ptr\n", + "add(rNum,ptr)\n", + "\n", + "\n", + "#as python does not support pointers I dont have used pointer in this progaram" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Reference: 100\n", + "Pointer: 100\n", + "Pointer now: 500\n", + "total: 600\n" + ] + } + ], + "prompt_number": 10 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/C++_Programming_In_Easy_Steps/Chapter7.ipynb b/C++_Programming_In_Easy_Steps/Chapter7.ipynb new file mode 100755 index 00000000..21e4cb9a --- /dev/null +++ b/C++_Programming_In_Easy_Steps/Chapter7.ipynb @@ -0,0 +1,429 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:1bf3a3c32b50a821205df977feb0399264ec0b05a84acfd47497b8eab54ae29b" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 7: Creating Classes and objects" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.1, Page No.118" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class dog:\n", + " def bark(self):\n", + " print \"WOOF!\"\n", + " \n", + " def setAge(self,age):\n", + " self.age=age\n", + " \n", + " def setWeight(self,weight):\n", + " self.weight=weight\n", + " \n", + " def setColor(self,color):\n", + " self.color=color\n", + " \n", + " def getAge(self):\n", + " return self.age\n", + " \n", + " def getWeight(self):\n", + " return self.weight\n", + " \n", + " def getColor(self):\n", + " return self.color\n", + " \n", + " \n", + "if __name__=='__main__':\n", + " fido=dog()\n", + " fido.setAge(3)\n", + " fido.setWeight(15)\n", + " fido.setColor(\"brown\")\n", + " \n", + " print \"Fido is a\",fido.getColor(),\" dog\"\n", + " print \"Fido is \",fido.getAge(),\" years old\"\n", + " print \"Fido Weighs \",fido.getWeight(),\" pounds\"\n", + " fido.bark()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Fido is a brown dog\n", + "Fido is 3 years old\n", + "Fido Weighs 15 pounds\n", + "WOOF!\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.2, Page No.120" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class dog:\n", + " def bark(self):\n", + " print \"WOOF!\"\n", + " \n", + " def setValues(self,age,weight,color):\n", + " self.age=age\n", + " self.weight=weight\n", + " self.color=color\n", + " \n", + " def getAge(self):\n", + " return self.age\n", + " \n", + " def getWeight(self):\n", + " return self.weight\n", + " \n", + " def getColor(self):\n", + " return self.color\n", + " \n", + " \n", + "if __name__=='__main__':\n", + " fido=dog()\n", + " fido.setValues(3,15,\"brown\")\n", + " poonch=dog()\n", + " poonch.setValues(4,18,\"gray\")\n", + " print \"Fido is a\",fido.getColor(),\" dog\"\n", + " print \"Fido is \",fido.getAge(),\" years old\"\n", + " print \"Fido Weighs \",fido.getWeight(),\" pounds\"\n", + " fido.bark()\n", + " \n", + " print \"Poonch is \",poonch.getAge(),\" years old \",poonch.getColor(),\" dog who weighs \",poonch.getWeight(),\" pounds\",poonch.bark()\n", + " \n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Fido is a brown dog\n", + "Fido is 3 years old\n", + "Fido Weighs 15 pounds\n", + "WOOF!\n", + "Poonch is 4 years old gray dog who weighs 18 pounds WOOF!\n", + "None\n" + ] + } + ], + "prompt_number": 36 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.3, Page No.122" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class dog:\n", + " def __init__(self,age,weight,color):\n", + " self.age=age\n", + " self.weight=weight\n", + " self.color=color\n", + " \n", + " def bark(self):\n", + " print \"WOOF!\"\n", + " \n", + " def getAge(self):\n", + " return self.age\n", + " \n", + " def getWeight(self):\n", + " return self.weight\n", + " \n", + " def getColor(self):\n", + " return self.color\n", + " \n", + " def __del__(self):\n", + " print \"Object destroyed\"\n", + " \n", + "if __name__=='__main__':\n", + " fido=dog(3,15,\"brown\")\n", + " poonch=dog(4,18,\"gray\")\n", + " \n", + " print \"Fido is a\",fido.getColor(),\" dog\"\n", + " print \"Fido is \",fido.getAge(),\" years old\"\n", + " print \"Fido Weighs \",fido.getWeight(),\" pounds\"\n", + " fido.bark()\n", + " \n", + " print \"Poonch is \",poonch.getAge(),\" years old \",poonch.getColor(),\" dog who weighs \",poonch.getWeight(),\" pounds\",poonch.bark()\n", + " \n", + " del fido\n", + " del poonch" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Fido is a brown dog\n", + "Fido is 3 years old\n", + "Fido Weighs 15 pounds\n", + "WOOF!\n", + "Poonch is 4 years old gray dog who weighs 18 pounds WOOF!\n", + "None\n", + "Object destroyed\n", + "Object destroyed\n" + ] + } + ], + "prompt_number": 37 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Exmaple 7.4, Page No.124" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class dog:\n", + " def __init__(self,age=1,weight=2,color=\"black\"):\n", + " self.age=age\n", + " self.weight=weight\n", + " self.color=color \n", + " \n", + " def bark(self,noise):\n", + " print noise\n", + " \n", + " def getAge(self):\n", + " return self.age\n", + " \n", + " def getWeight(self):\n", + " return self.weight\n", + " \n", + " def getColor(self):\n", + " return self.color\n", + " \n", + " def __del__(self):\n", + " print \"Object destroyed\"\n", + " \n", + "if __name__=='__main__':\n", + " fido=dog(3,15,\"brown\")\n", + " poonch=dog(4,18,\"gray\")\n", + " rex=dog()\n", + " sammy=dog(2,6,\"white\")\n", + " \n", + " \n", + " print \"Fido is a\",fido.getColor(),\" dog\"\n", + " print \"Fido is \",fido.getAge(),\" years old\"\n", + " print \"Fido Weighs \",fido.getWeight(),\" pounds\"\n", + " fido.bark(\"WOOF!\")\n", + " \n", + " print \"Poonch is \",poonch.getAge(),\" years old \",poonch.getColor(),\" dog who weighs \",poonch.getWeight(),\" pounds\",poonch.bark(\"WOOF!\")\n", + " print \"Rex is \",rex.getAge(),\" years old \",rex.getColor(),\" dog who weighs \",rex.getWeight(),\" pounds\",rex.bark(\"GRRR!\")\n", + " print \"Sammy is \",sammy.getAge(),\" years old \",sammy.getColor(),\" dog who weighs \",sammy.getWeight(),\" pounds\",sammy.bark(\"BOWOW!\")\n", + " del fido\n", + " del poonch\n", + " del rex\n", + " del sammy" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Fido is a brown dog\n", + "Fido is 3 years old\n", + "Fido Weighs 15 pounds\n", + "WOOF!\n", + "Poonch is 4 years old gray dog who weighs 18 pounds WOOF!\n", + "None \n", + "Rex is 1 years old black dog who weighs 2 pounds GRRR!\n", + "None \n", + "Sammy is 2 years old white dog who weighs 6 pounds BOWOW!\n", + "None \n", + "Object destroyed\n", + "Object destroyed\n", + "Object destroyed\n", + "Object destroyed\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.5, Page No.126" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Polygon:\n", + " weight=0\n", + " height=0\n", + " def setValues(self,w,h):\n", + " self.weight=w\n", + " self.height=h\n", + "\n", + "class Rectangle(Polygon):\n", + " def area(self):\n", + " return self.weight*self.height\n", + "\n", + "class Triangle(Polygon):\n", + " def area(self):\n", + " return (self.weight*self.height)/2\n", + "\n", + "rect=Rectangle()\n", + "trgl=Triangle()\n", + "rect.setValues(4,5)\n", + "trgl.setValues(4,5)\n", + "print \"Rectangle Area:\",rect.area()\n", + "print \"Triangle Area:\",trgl.area()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rectangle Area: 20\n", + "Triangle Area: 10\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.6, Page No.128" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Parent:\n", + " def __init__(self):\n", + " print \"Default Parent Constructor Called\"\n", + " def __init__(self,a):\n", + " print \"Overloaded Parent Constructor Called\"\n", + "\n", + "class Daughter(Parent):\n", + " def __init__(self):\n", + " print \"Derived Daughter class default constructor called\"\n", + "\n", + "class Son(Parent):\n", + " def __init__(self,a):\n", + " Parent.__init__(self,a)\n", + " print \"Derived Son class overloaded constructor called\"\n", + "\n", + "emma=Daughter()\n", + "andrew=Son(0)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Derived Daughter class default constructor called\n", + "Overloaded Parent Constructor Called\n", + "Derived Son class overloaded constructor called\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example No.7.7, Page No. 130" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Man:\n", + " def Speak(self, msg=\"Hello\"):\n", + " print \" \",msg\n", + "\n", + "class Hombre(Man):\n", + " def Speak(self,msg):\n", + " print msg\n", + "henry=Man()\n", + "enrique=Hombre()\n", + "henry.Speak()\n", + "henry.Speak(\"It's a beautiful evening.\")\n", + "enrique.Speak(\"Hola!\")\n", + "enrique.Speak(\"Es una trade hermosa\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Hello\n", + " It's a beautiful evening.\n", + "Hola!\n", + "Es una trade hermosa\n" + ] + } + ], + "prompt_number": 22 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/C++_Programming_In_Easy_Steps/Chapter8.ipynb b/C++_Programming_In_Easy_Steps/Chapter8.ipynb new file mode 100755 index 00000000..2bb6672f --- /dev/null +++ b/C++_Programming_In_Easy_Steps/Chapter8.ipynb @@ -0,0 +1,603 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:fa5fb390e0208b333276a0ed5185d5e56d34940c09a52bd389b2d0242ea600f2" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Harnessing polymorphism" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.1, Page No.134" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Base:\n", + " def Identify(self,adr):\n", + " print \"Base Class called by \",hex(adr)\n", + "\n", + "class SubA(Base):\n", + " def __init__(self,adr):\n", + " Base.Identify(self,adr)\n", + "class SubB(Base):\n", + " def __init__(self,adr):\n", + " Base.Identify(self,adr)\n", + "ptrA=1\n", + "ptrB=SubA(id(ptrA))\n", + "ptrC=2\n", + "ptrc=SubB(id(ptrC))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Base Class called by 0x12fd8e0\n", + "Base Class called by 0x12fd8d4\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.2, Page No.136" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Parent:\n", + " def Common(self):\n", + " print \"I am part of this family\"\n", + " def identify(self):\n", + " print \"I am the parent\"\n", + "\n", + "class Child(Parent):\n", + " def identify(self):\n", + " print \"I am the child\"\n", + " \n", + "class Grandchild(Child):\n", + " def identify(self):\n", + " print \"I am the grandchild\"\n", + " def Relate(self):\n", + " print \"Grandchild has parent and grandparent\"\n", + "\n", + "son=Child()\n", + "grandson=Grandchild()\n", + "parent=Parent()\n", + "print parent.Common(),\" \",son.identify()\n", + "print parent.Common(),\" \",grandson.identify()\n", + "print parent.Common(),\" \",parent.identify()\n", + "print grandson.Relate()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I am part of this family\n", + "None I am the child\n", + "None\n", + "I am part of this family\n", + "None I am the grandchild\n", + "None\n", + "I am part of this family\n", + "None I am the parent\n", + "None\n", + "Grandchild has parent and grandparent\n", + "None\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.3, Page No.138" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Bird:\n", + " def Talk(self):\n", + " print \"A Bird talks...\"\n", + " def Fly(self):\n", + " print \"A Bird flies...\"\n", + "class Pigeon(Bird):\n", + " def Talk(self):\n", + " print \"Coo!Coo!\"\n", + " def Fly(self):\n", + " print \"A Pigeon flies away...\"\n", + "class Chicken(Bird):\n", + " def Talk(self):\n", + " print \"Cluk!Cluk!\"\n", + " def Fly(self):\n", + " print \"I'm just a chicken- I can't fly!\"\n", + "pPigeon=Pigeon()\n", + "pChicken=Chicken()\n", + "pBird=Bird()\n", + "print \"\",pPigeon.Talk()\n", + "print \"\",pPigeon.Fly()\n", + "print \"\",pChicken.Talk()\n", + "print \"\",pChicken.Fly()\n", + "print \"\",pBird.Talk()\n", + "print \"\",pBird.Fly()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Coo!Coo!\n", + "None\n", + " A Pigeon flies away...\n", + "None\n", + " Cluk!Cluk!\n", + "None\n", + " I'm just a chicken- I can't fly!\n", + "None\n", + " A Bird talks...\n", + "None\n", + " A Bird flies...\n", + "None\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.4, Page No. 140" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Bird:\n", + " def Talk(self):\n", + " return -1\n", + " def Fly(self):\n", + " return -1\n", + "class Pigeon(Bird):\n", + " def Talk(self):\n", + " print \"Coo!Coo!\"\n", + " def Fly(self):\n", + " print \"A Pigeon flies away...\"\n", + "class Chicken(Bird):\n", + " def Talk(self):\n", + " print \"Cluk!Cluk!\"\n", + " def Fly(self):\n", + " print \"I'm just a chicken- I can't fly!\"\n", + "pPegion=Pigeon()\n", + "pChicken=Chicken()\n", + "pPegion.Talk()\n", + "pChicken.Talk()\n", + "pBird=Bird()\n", + "data=pBird.Talk()\n", + "if data==-1:\n", + " print \"Error!-Program ended\"\n", + " exit()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Coo!Coo!\n", + "Cluk!Cluk!\n", + "Error!-Program ended\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.5, Page No.142" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Rect:\n", + " height=0\n", + " width=0\n", + " def __init__(self,initWidth,initHeight):\n", + " self.height=initHeight\n", + " self.width=initWidth\n", + " def getArea(self):\n", + " return self.height*self.width\n", + " def getEdge(self):\n", + " return ((2*self.height)+(2*self.width)) \n", + " def Draw(self):\n", + " for i in range(0,self.height):\n", + " for j in range(0,self.width):\n", + " print \"X\",\n", + " print \"\\n\"\n", + "\n", + "pQuad=Rect(7,3)\n", + "pSquare=Rect(5,5)\n", + "\n", + "pQuad.Draw()\n", + "print \"Area is:\",pQuad.getArea()\n", + "print \"Perimeter is:\",pQuad.getEdge()\n", + "\n", + "pSquare.Draw()\n", + "print \"Area is:\",pSquare.getArea()\n", + "print \"Perimeter is:\",pSquare.getEdge()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X X X X X X X \n", + "\n", + "X X X X X X X \n", + "\n", + "X X X X X X X \n", + "\n", + "Area is: 21\n", + "Perimeter is: 20\n", + "X X X X X \n", + "\n", + "X X X X X \n", + "\n", + "X X X X X \n", + "\n", + "X X X X X \n", + "\n", + "X X X X X \n", + "\n", + "Area is: 25\n", + "Perimeter is: 20\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.6, Page No.144" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Boat:\n", + " length=0\n", + " def getLength(self):\n", + " return self.length\n", + "class Sailboat(Boat):\n", + " mast=0\n", + " def getMast(self):\n", + " return self.mast\n", + "class Laser(Sailboat):\n", + " def __init__(self):\n", + " self.mast=19\n", + " self.length=35\n", + " def Model(self):\n", + " print \"Laser Classic:\"\n", + " def Boom(self):\n", + " print \"Boom: 14ft\"\n", + "pLaser=Laser()\n", + "pLaser.Model()\n", + "print \"Length:\",pLaser.getLength(),\"ft\"\n", + "print \"Height:\",pLaser.getMast(),\"ft\"\n", + "pLaser.Boom()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Laser Classic:\n", + "Length: 35 ft\n", + "Height: 19 ft\n", + "Boom: 14ft\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8.7, Page No.147" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Calculator:\n", + " status=\"true\"\n", + " num1=0.0\n", + " num2=0.0\n", + " oper='+'\n", + " def __init__(self):\n", + " self.status=\"true\"\n", + " def launch(self):\n", + " print \"***SUM CALCULATOR***\"\n", + " print \"Enter a number, an operator (+,-,*,/), and another number\\nEnter Zero to exit\"\n", + " def readInput(self):\n", + " print \">\",\n", + " self.num1=float(raw_input())\n", + " if self.num1==0:\n", + " self.status=\"false\"\n", + " else:\n", + " self.oper=raw_input()\n", + " self.num2=float(raw_input())\n", + " def writeOutput(self):\n", + " if self.status==\"true\":\n", + " if self.oper=='+':\n", + " print self.num1+self.num2\n", + " elif self.oper=='-':\n", + " print self.num1-self.num2\n", + " elif self.oper=='*':\n", + " print self.num1*self.num2\n", + " elif self.oper=='/':\n", + " if self.num2!=0:\n", + " print self.num1/self.num2\n", + " else:\n", + " print \"Cannot divide by zero\"\n", + " def run(self):\n", + " return self.status\n", + " \n", + " \n", + " \n", + " \n", + "pCalc=Calculator()\n", + "pCalc.launch()\n", + "while (pCalc.run()==\"true\"):\n", + " pCalc.readInput()\n", + " pCalc.writeOutput()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " ***SUM CALCULATOR***\n", + "Enter a number, an operator (+,-,*,/), and another number\n", + "Enter Zero to exit\n", + ">" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "32\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "+\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "32\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 64.0\n", + ">" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5.25\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8.75\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " -3.5\n", + ">" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "*\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 24.0\n", + ">" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "/\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 4.0\n", + ">" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "/\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Cannot divide by zero\n", + ">" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 4 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/C++_Programming_In_Easy_Steps/Chapter9.ipynb b/C++_Programming_In_Easy_Steps/Chapter9.ipynb new file mode 100755 index 00000000..18bdfd17 --- /dev/null +++ b/C++_Programming_In_Easy_Steps/Chapter9.ipynb @@ -0,0 +1,316 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:53d2aae81c824182838b074246429ee4c039bafa2ea2a0b5b38839beb8288df9" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 9: Processing macros" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.1, Page No.153" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"This is a simple test program\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This is a simple test program\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.2, Page No.154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "BOOK=\"C++ Programming in easy steps\"\n", + "NUM=200\n", + "RULE=\"*****************************\"\n", + "print RULE\n", + "print BOOK\n", + "print RULE\n", + "print \"NUM is:\",NUM\n", + "print \"Double NUM:\",NUM*2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "*****************************\n", + "C++ Programming in easy steps\n", + "*****************************\n", + "NUM is: 200\n", + "Double NUM: 400\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.3, Page No.156" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "if not 'BOOK' in locals():\n", + " BOOK=\"C++ Programming in easy steps\"\n", + "\n", + "print BOOK,\n", + "if not 'AUTHOR' in locals():\n", + " AUTHOR=\"Mike McGrath\"\n", + "print \" by \",AUTHOR\n", + "if not 'BOOK' in locals():\n", + " BOOK=\"\"\n", + "if 'BOOK' in locals():\n", + " BOOK=\"Linux in easy steps\"\n", + "\n", + "print BOOK,\" by \",AUTHOR" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Linux in easy steps by Mike McGrath\n", + "Linux in easy steps by Mike McGrath\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.4, Page No.159" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import platform\n", + "plat=platform.system()\n", + "print plat,\" system\"\n", + "if plat==\"Windows\":\n", + " print \"Performing Windows-only tasks\"\n", + "else:\n", + " print \"Performing Linux-only tasks\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Windows system\n", + "Performing Windows-only tasks\n" + ] + } + ], + "prompt_number": 32 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.5, Page No.160" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def add(x,y):\n", + " return x+y\n", + "def triple(x):\n", + " return (add(x,add(x,x)))\n", + "print \"9 + 3 = \",add(9,3)\n", + "print \"9 x 3 = \",triple(9)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "9 + 3 = 12\n", + "9 x 3 = 27\n" + ] + } + ], + "prompt_number": 34 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.6, Page No.162" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def SQUARE(n):\n", + " return n*n\n", + "def CUBE(n):\n", + " return n*n*n\n", + "num=5\n", + "print \"Macro SQUARE:\",SQUARE(num)\n", + "print \"inline square:\",SQUARE(num)\n", + "print \"Macro CUBE:\",CUBE(num)\n", + "print \"inline CUBE:\",CUBE(num)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Macro SQUARE: 25\n", + "inline square: 25\n", + "Macro CUBE: 125\n", + "inline CUBE: 125\n" + ] + } + ], + "prompt_number": 35 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.7, Page No.164" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def LINEOUT(str):\n", + " print str\n", + "def GLUEOUT(a,b):\n", + " print a,\" \",b\n", + "longer=\"They carried a net \"\n", + "line=\"and their hearts \"\n", + "LINEOUT(\"In a bowl to sea went wise men three\")\n", + "LINEOUT(\"On a brilliant night in June\")\n", + "GLUEOUT(longer,line)\n", + "LINEOUT(\"On fishing up the moon.\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In a bowl to sea went wise men three\n", + "On a brilliant night in June\n", + "They carried a net and their hearts \n", + "On fishing up the moon.\n" + ] + } + ], + "prompt_number": 36 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.8, Page No.166" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "DEBUG=0\n", + "if DEBUG == 1:\n", + " def ASSERT(expr):\n", + " print expr, \"...\", num\n", + " if expr != True:\n", + " print \"Fails in file: \"# There is no concept of line in python\n", + " print \"at line\"# There is no concept of line in python\n", + " else:\n", + " print \"Succeeds\"\n", + "elif DEBUG == 0:\n", + " def ASSERT(expr):\n", + " print \"Number is \",num\n", + "num = 9\n", + "ASSERT(num < 10)\n", + "num = num + num\n", + "ASSERT(num < 10)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number is 9\n", + "Number is 18\n" + ] + } + ], + "prompt_number": 10 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/C++_Programming_In_Easy_Steps/README.txt b/C++_Programming_In_Easy_Steps/README.txt new file mode 100755 index 00000000..ae8d74aa --- /dev/null +++ b/C++_Programming_In_Easy_Steps/README.txt @@ -0,0 +1,10 @@ +Contributed By: Chirag Kotak +Course: mca +College/Institute/Organization: Shri Brahmanand Institute of Management and Computer Science +Department/Designation: Computer Science +Book Title: C++ Programming In Easy Steps +Author: Mike McGrath +Publisher: Easy Steps Limited Southfield Road .Southam Warwickshire CV47 0FB .United Kingdom +Year of publication: 2011 +Isbn: 978-1-84078-432-9 +Edition: 4th edition \ No newline at end of file diff --git a/C++_Programming_In_Easy_Steps/screenshots/1.png b/C++_Programming_In_Easy_Steps/screenshots/1.png new file mode 100755 index 00000000..c1176161 Binary files /dev/null and b/C++_Programming_In_Easy_Steps/screenshots/1.png differ diff --git a/C++_Programming_In_Easy_Steps/screenshots/2.png b/C++_Programming_In_Easy_Steps/screenshots/2.png new file mode 100755 index 00000000..5ee82d29 Binary files /dev/null and b/C++_Programming_In_Easy_Steps/screenshots/2.png differ diff --git a/C++_Programming_In_Easy_Steps/screenshots/3.png b/C++_Programming_In_Easy_Steps/screenshots/3.png new file mode 100755 index 00000000..c1a54d3b Binary files /dev/null and b/C++_Programming_In_Easy_Steps/screenshots/3.png differ 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=\n", + "Address=505552912 Content=\n", + "Address=505547744 Content=\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: \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 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 new file mode 100755 index 00000000..8209b4ff Binary files /dev/null and b/Teach_Yourself_C_in_24_Hours/screenshots/1.png differ diff --git a/Teach_Yourself_C_in_24_Hours/screenshots/2.png b/Teach_Yourself_C_in_24_Hours/screenshots/2.png new file mode 100755 index 00000000..0cb942d5 Binary files /dev/null and b/Teach_Yourself_C_in_24_Hours/screenshots/2.png differ diff --git a/Teach_Yourself_C_in_24_Hours/screenshots/3.png b/Teach_Yourself_C_in_24_Hours/screenshots/3.png new file mode 100755 index 00000000..3534e22c Binary files /dev/null and b/Teach_Yourself_C_in_24_Hours/screenshots/3.png differ -- cgit