diff options
Diffstat (limited to 'Let_us_C')
-rw-r--r-- | Let_us_C/README.txt | 10 | ||||
-rw-r--r-- | Let_us_C/chapter-1.ipynb | 221 | ||||
-rw-r--r-- | Let_us_C/chapter-10.ipynb | 499 | ||||
-rw-r--r-- | Let_us_C/chapter-11.ipynb | 383 | ||||
-rw-r--r-- | Let_us_C/chapter-12.ipynb | 2351 | ||||
-rw-r--r-- | Let_us_C/chapter-13.ipynb | 83 | ||||
-rw-r--r-- | Let_us_C/chapter-14.ipynb | 853 | ||||
-rw-r--r-- | Let_us_C/chapter-15.ipynb | 621 | ||||
-rw-r--r-- | Let_us_C/chapter-16.ipynb | 66 | ||||
-rw-r--r-- | Let_us_C/chapter-17.ipynb | 115 | ||||
-rw-r--r-- | Let_us_C/chapter-18.ipynb | 261 | ||||
-rw-r--r-- | Let_us_C/chapter-2.ipynb | 571 | ||||
-rw-r--r-- | Let_us_C/chapter-20.ipynb | 181 | ||||
-rw-r--r-- | Let_us_C/chapter-21.ipynb | 226 | ||||
-rw-r--r-- | Let_us_C/chapter-3.ipynb | 616 | ||||
-rw-r--r-- | Let_us_C/chapter-4.ipynb | 361 | ||||
-rw-r--r-- | Let_us_C/chapter-5.ipynb | 804 | ||||
-rw-r--r-- | Let_us_C/chapter-6.ipynb | 683 | ||||
-rw-r--r-- | Let_us_C/chapter-7.ipynb | 210 | ||||
-rw-r--r-- | Let_us_C/chapter-8.ipynb | 936 | ||||
-rw-r--r-- | Let_us_C/chapter-9.ipynb | 616 | ||||
-rw-r--r-- | Let_us_C/screenshots/binary.png | bin | 0 -> 30718 bytes | |||
-rw-r--r-- | Let_us_C/screenshots/hellowindows.png | bin | 0 -> 67812 bytes | |||
-rw-r--r-- | Let_us_C/screenshots/macro.png | bin | 0 -> 32392 bytes |
24 files changed, 10667 insertions, 0 deletions
diff --git a/Let_us_C/README.txt b/Let_us_C/README.txt new file mode 100644 index 00000000..752b7f71 --- /dev/null +++ b/Let_us_C/README.txt @@ -0,0 +1,10 @@ +Contributed By: Akshata M +Course: btech +College/Institute/Organization: NITK Surathkal +Department/Designation: Computer Science +Book Title: Let us C +Author: Yashavant P. Kanetkar +Publisher: BPB Publications +Year of publication: 2008 +Isbn: 8176569402 +Edition: 5th
\ No newline at end of file diff --git a/Let_us_C/chapter-1.ipynb b/Let_us_C/chapter-1.ipynb new file mode 100644 index 00000000..19d3a98f --- /dev/null +++ b/Let_us_C/chapter-1.ipynb @@ -0,0 +1,221 @@ +{
+ "metadata": {
+ "name": "chapter-1.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 1: Getting Started<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>First C Program, Page number: 14<h3>\n",
+ " "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''calculate simple interest for a set of values representing principle,\n",
+ "number of years and rate of interest.'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "p = 1000 #principle\n",
+ "n = 3 # number of years\n",
+ "r = 8.5 # rate of interest\n",
+ "\n",
+ "#Calculation\n",
+ "si = p * n * r / 100 ; #formula for simple interest\n",
+ "\n",
+ "#Result\n",
+ "print ( si )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "255.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Simple Interest, Page number: 21<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''calculate simple interest by inputing principle,\n",
+ "number of years and rate of interest from the user.'''\n",
+ "\n",
+ "#Input from the user\n",
+ "#p,n,r = raw_input(\"Enter values of p, n, r : \").split()\n",
+ "p = 100 # principle\n",
+ "n = 5 # number of years\n",
+ "r = 15.5 # rate of interest\n",
+ "\n",
+ "#Calculation\n",
+ "si = p * n * r / 100 ; #formula for simple interest\n",
+ "\n",
+ "#Result\n",
+ "print ( si )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "77.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Just for fun, Page number: 22<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Just for fun. Author: Bozo'''\n",
+ "\n",
+ "#Input from the user\n",
+ "#num = raw_input(\"Enter a number : \")\n",
+ "num = 11\n",
+ "\n",
+ "#Result\n",
+ "print \"Now I am letting you on a secret...\" \n",
+ "print \"You have just entered the number\", num \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Now I am letting you on a secret...\n",
+ "You have just entered the number 11\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 1.1, Page number: 32<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Determine the hierarchy of operations and evaluate the following expression:\n",
+ "i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8'''\n",
+ "\n",
+ "#Calculation\n",
+ "i1 = 2 * 3 # operation *\n",
+ "i2 = i1 / 4 # operation /\n",
+ "i3 = 4 / 4 # operation /\n",
+ "i4 = 5 / 8 # operation /\n",
+ "i5 = i2 + i3 # operation +\n",
+ "i6 = i5 + 8 # operation +\n",
+ "i7 = i6 - 2 # operation -\n",
+ "i8 = i7 + i4 # operation +\n",
+ "i = i8\n",
+ "\n",
+ "#Result\n",
+ "print \"i = \", i"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "i = 8\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 1.2, Page number: 33<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Determine the hierarchy of operations and evaluate the following expression:\n",
+ "kk = 3 / 2 * 4 + 3 / 8 + 3'''\n",
+ "\n",
+ "#Calculation\n",
+ "kk1 = 3 / 2# operation /\n",
+ "kk2 = kk1 * 4 # operation *\n",
+ "kk3 = 3 / 8 # operation /\n",
+ "kk4 = kk2 + kk3 # operation +\n",
+ "kk5 = kk4 + 3 # operation +\n",
+ "kk = kk5\n",
+ "\n",
+ "#Result\n",
+ "print \"kk = \", kk\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "kk = 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-10.ipynb b/Let_us_C/chapter-10.ipynb new file mode 100644 index 00000000..db8bf067 --- /dev/null +++ b/Let_us_C/chapter-10.ipynb @@ -0,0 +1,499 @@ +{
+ "metadata": {
+ "name": "chapter-10.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 10: Structures <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Arrays , Page number: 365<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to store data about 3 books that is their name\n",
+ "(a string), price (a float) and number of pages (an int).'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "name = []\n",
+ "price = []\n",
+ "pages = []\n",
+ "\n",
+ "n =[\"A\",\"C\",\"F\"]\n",
+ "p=[100.00,256.50,233.70]\n",
+ "pg=[354,682,512]\n",
+ "\n",
+ "print \"Enter names, prices and no. of pages of 3 books \" \n",
+ "\n",
+ "for i in range(0,3):\n",
+ " #n,p,pg = raw_input(\"\").split()\n",
+ " print n[i],p[i],pg[i]\n",
+ " name.append(n[i])\n",
+ " price.append(p[i])\n",
+ " pages.append(pg[i])\n",
+ "\n",
+ "print \"And this is what you entered\" \n",
+ "\n",
+ "for i in range(0,3):\n",
+ " print name[i], price[i], pages[i] "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter names, prices and no. of pages of 3 books \n",
+ "A 100.0 354\n",
+ "C 256.5 682\n",
+ "F 233.7 512\n",
+ "And this is what you entered\n",
+ "A 100.0 354\n",
+ "C 256.5 682\n",
+ "F 233.7 512\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Structure Example , Page number: 366<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to store data about 3 books that is their name\n",
+ "(a string), price (a float) and number of pages (an int).'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure defintion\n",
+ "struct_book = namedtuple(\"struct_book\", \"name price pages\")\n",
+ "\n",
+ "#Input from user\n",
+ "print \"Enter names, prices & no. of pages of 3 books\" \n",
+ "#b1n,b1p,b1pg = raw_input(\"\").split()\n",
+ "#b2n,b2p,b2pg = raw_input(\"\").split()\n",
+ "#b3n,b3p,b3pg = raw_input(\"\").split()\n",
+ "n =[\"A\",\"C\",\"F\"]\n",
+ "p=[100.00,256.50,233.70]\n",
+ "pg=[354,682,512]\n",
+ "print n[0],p[0],pg[0]\n",
+ "print n[1],p[1],pg[1]\n",
+ "print n[2],p[2],pg[2]\n",
+ "#Structures for 3 books\n",
+ "b1 = struct_book(n[0], p[0], pg[0])\n",
+ "b2 = struct_book(n[1], p[1], pg[1])\n",
+ "b3 = struct_book(n[2], p[2], pg[2])\n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "print \"And this is what you entered\" \n",
+ "print b1.name, b1.price, b1.pages \n",
+ "print b2.name, b2.price, b2.pages \n",
+ "print b3.name, b3.price, b3.pages \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter names, prices & no. of pages of 3 books\n",
+ "A 100.0 354\n",
+ "C 256.5 682\n",
+ "F 233.7 512\n",
+ "And this is what you entered\n",
+ "A 100.0 354\n",
+ "C 256.5 682\n",
+ "F 233.7 512\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Memory Map of Structures , Page number: 370<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate that elements of a structure,\n",
+ "are always stored in contiguous memory locations'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure defintion\n",
+ "struct_book = namedtuple(\"struct_book\", \"name price pages\")\n",
+ "\n",
+ "#Structures for a book\n",
+ "b1 = struct_book('B', 130.00, 550)\n",
+ "\n",
+ "#Result\n",
+ "print \"Address of name = \", id(b1.name )\n",
+ "print \"Address of price = \", id(b1.price )\n",
+ "print \"Address of pages = \", id(b1.pages )"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of name = 31603728\n",
+ "Address of price = 108997488\n",
+ "Address of pages = 133808864\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Array of Structures , Page number: 371<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to show how to use an array of structures.'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure defintion\n",
+ "struct_book = namedtuple(\"struct_book\", \"name price pages\")\n",
+ "\n",
+ "#Array of structures\n",
+ "b =[]\n",
+ "\n",
+ "n =[\"A\",\"C\",\"F\"]\n",
+ "p=[100.00,256.50,233.70]\n",
+ "pg=[354,682,512]\n",
+ "\n",
+ "#Storing data in the array\n",
+ "for i in range(0,3):\n",
+ " #bn, bp, bpg =raw_input( \"Enter name, price and pages: \" ).split()\n",
+ " print \"Enter name, price and pages: \"\n",
+ " print n[i],p[i],pg[i]\n",
+ " b.append(struct_book(n[i], p[i], pg[i]))\n",
+ "\n",
+ "#Result\n",
+ "for i in range(0,3):\n",
+ " print b[i].name, b[i].price, b[i].pages \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name, price and pages: \n",
+ "A 100.0 354\n",
+ "Enter name, price and pages: \n",
+ "C 256.5 682\n",
+ "Enter name, price and pages: \n",
+ "F 233.7 512\n",
+ "A 100.0 354\n",
+ "C 256.5 682\n",
+ "F 233.7 512\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Copying Structures , Page number: 374<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate that values of a structure variable can be\n",
+ "assigned to another structure variable of the same type\n",
+ "using the assignment operator.'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure defintion\n",
+ "struct_employee = namedtuple(\"struct_employee\", \"name age salary\")\n",
+ "\n",
+ "#Structures for 3 employees\n",
+ "e1 = struct_employee(\"Sanjay\", 30, 5500.50)\n",
+ "e2 = struct_employee(\" \",0,0)\n",
+ "e3 = struct_employee(\" \",0,0)\n",
+ "\n",
+ "#piece-meal copying \n",
+ "import copy\n",
+ "e2 = e2._replace(name = e1.name)\n",
+ "e2 = e2._replace(age = e1.age)\n",
+ "e2 = e2._replace(salary = e1.salary)\n",
+ "\n",
+ "\n",
+ "#copying all elements at one go \n",
+ "e3 = e2\n",
+ "\n",
+ "#Result\n",
+ "print e1.name, e1.age, e1.salary \n",
+ "print e2.name, e2.age, e2.salary \n",
+ "print e3.name, e3.age, e3.salary \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sanjay 30 5500.5\n",
+ "Sanjay 30 5500.5\n",
+ "Sanjay 30 5500.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Nested Structures , Page number: 375<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate nested structures at work.'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure defintions\n",
+ "struct_address = namedtuple(\"struct_address\", \"phone city pin\")\n",
+ "struct_emp = namedtuple(\"struct_emp\", \"name struct_address\") #nested structures\n",
+ "\n",
+ "#Structures for employee\n",
+ "a = struct_address(\"531046\", \"nagpur\", 10)\n",
+ "e = struct_emp(\"jeru\",a) #nested structure\n",
+ "\n",
+ "#Result\n",
+ "print \"name = %s phone = %s\" %( e.name, e.struct_address.phone )\n",
+ "print \"city = %s pin = %d\" %( e.struct_address.city, e.struct_address.pin )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "name = jeru phone = 531046\n",
+ "city = nagpur pin = 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Passing Individual Structure Elements to Functions, Page number: 377<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the method of passing individual structure elements to\n",
+ "functions'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure defintions\n",
+ "struct_book = namedtuple(\"struct_book\", \"name author callno\")\n",
+ "\n",
+ "#Function definition\n",
+ "def display (s,t,n):\n",
+ " print s, t, n \n",
+ " \n",
+ "#Structures for book\n",
+ "b1 = struct_book(\"Let us C\", \"YPK\", 101)\n",
+ "\n",
+ "\n",
+ "display ( b1.name, b1.author, b1.callno ) #function call\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Let us C YPK 101\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Passing Structure to a Function , Page number: 378<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the method of passing \n",
+ "the entire structure variable to a function'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure defintions\n",
+ "struct_book = namedtuple(\"struct_book\", \"name author callno\")\n",
+ "\n",
+ "#Function definition\n",
+ "def display (b):\n",
+ " print b.name, b.author, b.callno \n",
+ " \n",
+ "#Structures for book\n",
+ "b1 = struct_book(\"Let us C\", \"YPK\", 101)\n",
+ "\n",
+ "\n",
+ "display ( b1) #function call\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Let us C YPK 101\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Structure Pointers , Page number: 379<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that demonstrates the usage of a structure pointer.'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure defintions\n",
+ "struct_book = namedtuple(\"struct_book\", \"name author callno\")\n",
+ "\n",
+ "#Structure for book\n",
+ "b1 = struct_book(\"Let us C\", \"YPK\", 101)\n",
+ "ptr = id(b1) #structure pointer\n",
+ "\n",
+ "#Result\n",
+ "print b1.name, b1.author, b1.callno \n",
+ "print b1.name, b1.author, b1.callno \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Let us C YPK 101\n",
+ "Let us C YPK 101\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Passing Address of a Structure Variable , Page number: 380<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that demonstrates passing address of a structure variable'''\n",
+ "\n",
+ "#function definition\n",
+ "def display (b):\n",
+ " print b.name, b.author, b.callno \n",
+ " \n",
+ "from collections import namedtuple\n",
+ "#Structure defintions\n",
+ "struct_book = namedtuple(\"struct_book\", \"name author callno\")\n",
+ "\n",
+ "#Structure for book\n",
+ "b1 = struct_book(\"Let us C\", \"YPK\", 101)\n",
+ "\n",
+ "#function call\n",
+ "display ( b1 ) \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Let us C YPK 101\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-11.ipynb b/Let_us_C/chapter-11.ipynb new file mode 100644 index 00000000..4f966f44 --- /dev/null +++ b/Let_us_C/chapter-11.ipynb @@ -0,0 +1,383 @@ +{
+ "metadata": {
+ "name": "chapter-11.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 11: Console Input/Output<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Printf Example, Page number: 397<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate printing a formatted string using printf function'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "avg = 346 \n",
+ "per = 69.2\n",
+ "\n",
+ "#Result\n",
+ "print \"Average = %d\\nPercentage = %f\" %(avg, per )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Average = 346\n",
+ "Percentage = 69.200000\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Format Specifications, Page number: 399<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate printing using format specifiers in printf function'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "weight = 63\n",
+ "\n",
+ "#Result\n",
+ "print \"weight is %d kg\" %( weight ) \n",
+ "print \"weight is %2d kg\"%( weight ) \n",
+ "print \"weight is %4d kg\" %( weight )\n",
+ "print \"weight is %6d kg\" %(weight ) \n",
+ "print \"weight is %-6d kg\" %( weight )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "weight is 63 kg\n",
+ "weight is 63 kg\n",
+ "weight is 63 kg\n",
+ "weight is 63 kg\n",
+ "weight is 63 kg\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>String Format Specifiers, Page number: 400<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate printing using format specifiers in printf function'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "firstname1 = \"Sandy\" \n",
+ "surname1 = \"Malya\" \n",
+ "firstname2 = \"AjayKumar\" \n",
+ "surname2 = \"Gurubaxani\" \n",
+ "\n",
+ "#Result\n",
+ "print \"%20s%20s\" %( firstname1, surname1 )\n",
+ "print \"%20s%20s\" %(firstname2, surname2 ) \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Sandy Malya\n",
+ " AjayKumar Gurubaxani\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Escape Sequences, Page number: 401<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrateshows usage of \\n and a\n",
+ "new escape sequence \\t, called \u2018tab\u2019'''\n",
+ "\n",
+ "#Result\n",
+ "print \"You\\tmust\\tbe\\tcrazy\\nto\\thate\\tthis\\tbook\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You\tmust\tbe\tcrazy\n",
+ "to\thate\tthis\tbook\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Format Conversions, Page number: 403<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to perform the specified conversion using printf function'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "ch = 'z' \n",
+ "i = 125 \n",
+ "a = 12.55 \n",
+ "s = \"hello there !\"\n",
+ "\n",
+ "#Result\n",
+ "print \"%c %d %f\" %( ch, ord(ch), ord(ch )) \n",
+ "print \"%s %d %f\"%( s, ord(s[1]), ord(s[1]) )\n",
+ "print \"%c %d %f\"%(i ,i, i ) \n",
+ "print \"%f %d\\n\"%( a, a )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "z 122 122.000000\n",
+ "hello there ! 101 101.000000\n",
+ "} 125 125.000000\n",
+ "12.550000 12\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Sprintf Function, Page number: 404<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the usage of sprintf function'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 10 \n",
+ "ch = 'A'\n",
+ "a = 3.14 \n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "print \"%d %c %f\" %(i, ch, a ) \n",
+ "str = \"%d %c %f\" %(i, ch, a ) #sprintf\n",
+ "print \"%s\" %(str )\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 A 3.140000\n",
+ "10 A 3.140000\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Unformatted Input, Page number: 406<h3>\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the usage of unformatted input functions'''\n",
+ "\n",
+ "import msvcrt\n",
+ "\n",
+ "print \"Press any key to continue\" \n",
+ "#msvcrt.getch( ) # will not echo the character \n",
+ "print \"Type any character\" \n",
+ "#ch = msvcrt.getche( ) # will echo the character typed \n",
+ "ch = 'A'\n",
+ "print ch\n",
+ "#ch = input(\"Type any character\")#getchar( ) will echo character, must be followed by enter key \n",
+ "print \"Type any character\"\n",
+ "ch = 8\n",
+ "print ch\n",
+ "#ch = input( \"Continue Y/N\" ) #fgetchar( ) will echo character, must be followed by enter key\n",
+ "print \"Continue Y/N\" \n",
+ "ch = 'N'\n",
+ "print ch\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Press any key to continue\n",
+ "Type any character\n",
+ "A\n",
+ "Type any character\n",
+ "8\n",
+ "Continue Y/N\n",
+ "N\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Unformatted Output, Page number: 407<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the usage of unformatted output functions'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "ch = 'A'\n",
+ "\n",
+ "#Result\n",
+ "print ch #putch\n",
+ "print ch #putchar\n",
+ "print ch #fputchar\n",
+ "print 'Z' #putch\n",
+ "print 'Z' #putchar\n",
+ "print 'Z' #fputchar\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A\n",
+ "A\n",
+ "A\n",
+ "Z\n",
+ "Z\n",
+ "Z\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Unformatted String Input/Output, Page number: 408<h3>\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the usage of gets and puts functions'''\n",
+ "\n",
+ "#Input from user\n",
+ "#footballer = input(\"Enter name\" ) # puts(\"Enter name \") ; gets(footballer)\n",
+ "print \"Enter name\"\n",
+ "footballer = \"Jonty Rhodes\"\n",
+ "print footballer\n",
+ "\n",
+ "#Result\n",
+ "print \"Happy footballing!\" \n",
+ "print footballer "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n",
+ "Jonty Rhodes\n",
+ "Happy footballing!\n",
+ "Jonty Rhodes\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-12.ipynb b/Let_us_C/chapter-12.ipynb new file mode 100644 index 00000000..f0567d09 --- /dev/null +++ b/Let_us_C/chapter-12.ipynb @@ -0,0 +1,2351 @@ +{
+ "metadata": {
+ "name": "chapter-12.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 12: File Input/Output <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Display Contents of a File , Page number: 417<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to display contents of a file on screen.'''\n",
+ "\n",
+ "fp = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #open file in read mode\n",
+ "while ( 1 ):\n",
+ " ch = fp.read(1)\n",
+ " if not ch :\n",
+ " break\n",
+ " print ch\n",
+ "\n",
+ "fp.close() #close file\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "M\n",
+ "e\n",
+ "t\n",
+ "h\n",
+ "o\n",
+ "d\n",
+ "\n",
+ "\n",
+ "T\n",
+ "h\n",
+ "r\n",
+ "o\n",
+ "u\n",
+ "g\n",
+ "h\n",
+ "l\n",
+ "y\n",
+ " \n",
+ "w\n",
+ "a\n",
+ "s\n",
+ " \n",
+ "b\n",
+ "o\n",
+ "t\n",
+ "h\n",
+ " \n",
+ "b\n",
+ "e\n",
+ "e\n",
+ "t\n",
+ "r\n",
+ "o\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "c\n",
+ "a\n",
+ "r\n",
+ "r\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "t\n",
+ "o\n",
+ " \n",
+ "r\n",
+ "e\n",
+ "m\n",
+ "o\n",
+ "v\n",
+ "e\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "m\n",
+ "u\n",
+ "d\n",
+ " \n",
+ "o\n",
+ "n\n",
+ " \n",
+ "b\n",
+ "e\n",
+ "e\n",
+ "t\n",
+ "r\n",
+ "o\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "h\n",
+ "a\n",
+ "i\n",
+ "r\n",
+ " \n",
+ "o\n",
+ "n\n",
+ " \n",
+ "c\n",
+ "a\n",
+ "r\n",
+ "r\n",
+ "o\n",
+ "t\n",
+ "\n",
+ "\n",
+ "P\n",
+ "e\n",
+ "e\n",
+ "l\n",
+ " \n",
+ "o\n",
+ "f\n",
+ "f\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "c\n",
+ "a\n",
+ "r\n",
+ "r\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "s\n",
+ "k\n",
+ "i\n",
+ "n\n",
+ ".\n",
+ " \n",
+ "d\n",
+ "o\n",
+ " \n",
+ "n\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "p\n",
+ "e\n",
+ "e\n",
+ "l\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "r\n",
+ "a\n",
+ "d\n",
+ "d\n",
+ "i\n",
+ "s\n",
+ "h\n",
+ " \n",
+ "s\n",
+ "k\n",
+ "i\n",
+ "n\n",
+ " \n",
+ "t\n",
+ "o\n",
+ " \n",
+ "a\n",
+ "v\n",
+ "o\n",
+ "i\n",
+ "d\n",
+ " \n",
+ "p\n",
+ "o\n",
+ "s\n",
+ "s\n",
+ "i\n",
+ "b\n",
+ "l\n",
+ "e\n",
+ " \n",
+ "l\n",
+ "o\n",
+ "s\n",
+ "s\n",
+ " \n",
+ "o\n",
+ "f\n",
+ " \n",
+ "v\n",
+ "i\n",
+ "t\n",
+ "a\n",
+ "m\n",
+ "i\n",
+ "n\n",
+ "s\n",
+ "\n",
+ "\n",
+ "C\n",
+ "u\n",
+ "t\n",
+ " \n",
+ "e\n",
+ "a\n",
+ "c\n",
+ "h\n",
+ " \n",
+ "b\n",
+ "e\n",
+ "e\n",
+ "t\n",
+ "r\n",
+ "o\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "c\n",
+ "a\n",
+ "r\n",
+ "r\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "i\n",
+ "n\n",
+ " \n",
+ "t\n",
+ "o\n",
+ " \n",
+ "4\n",
+ " \n",
+ "h\n",
+ "a\n",
+ "l\n",
+ "v\n",
+ "e\n",
+ "s\n",
+ "\n",
+ "\n",
+ "P\n",
+ "r\n",
+ "e\n",
+ "a\n",
+ "s\n",
+ "s\n",
+ "u\n",
+ "r\n",
+ "e\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "o\n",
+ "k\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "b\n",
+ "e\n",
+ "e\n",
+ "t\n",
+ "r\n",
+ "o\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "c\n",
+ "a\n",
+ "r\n",
+ "r\n",
+ "o\n",
+ "t\n",
+ ".\n",
+ " \n",
+ "m\n",
+ "a\n",
+ "k\n",
+ "e\n",
+ " \n",
+ "3\n",
+ " \n",
+ "w\n",
+ "h\n",
+ "i\n",
+ "s\n",
+ "t\n",
+ "l\n",
+ "e\n",
+ "s\n",
+ " \n",
+ "s\n",
+ "o\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ "t\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ "y\n",
+ " \n",
+ "b\n",
+ "e\n",
+ "c\n",
+ "o\n",
+ "m\n",
+ "e\n",
+ " \n",
+ "v\n",
+ "e\n",
+ "r\n",
+ "y\n",
+ " \n",
+ "s\n",
+ "m\n",
+ "o\n",
+ "o\n",
+ "t\n",
+ "h\n",
+ "\n",
+ "\n",
+ "D\n",
+ "r\n",
+ "a\n",
+ "i\n",
+ "n\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "r\n",
+ "e\n",
+ "s\n",
+ "e\n",
+ "r\n",
+ "v\n",
+ "e\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "w\n",
+ "a\n",
+ "t\n",
+ "e\n",
+ "r\n",
+ " \n",
+ "a\n",
+ "f\n",
+ "t\n",
+ "e\n",
+ "r\n",
+ " \n",
+ "p\n",
+ "r\n",
+ "e\n",
+ "s\n",
+ "s\n",
+ "u\n",
+ "r\n",
+ "e\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "o\n",
+ "k\n",
+ "i\n",
+ "n\n",
+ "g\n",
+ ".\n",
+ "\n",
+ "\n",
+ "M\n",
+ "a\n",
+ "s\n",
+ "h\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "o\n",
+ "k\n",
+ "e\n",
+ "d\n",
+ " \n",
+ "p\n",
+ "i\n",
+ "e\n",
+ "c\n",
+ "e\n",
+ "s\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "a\n",
+ "l\n",
+ "l\n",
+ "o\n",
+ "w\n",
+ " \n",
+ "t\n",
+ "o\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "o\n",
+ "l\n",
+ "\n",
+ "\n",
+ "C\n",
+ "u\n",
+ "t\n",
+ " \n",
+ "o\n",
+ "n\n",
+ "i\n",
+ "o\n",
+ "n\n",
+ "s\n",
+ " \n",
+ "i\n",
+ "n\n",
+ "t\n",
+ "o\n",
+ " \n",
+ "c\n",
+ "u\n",
+ "b\n",
+ "e\n",
+ "s\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "s\n",
+ "a\n",
+ "u\n",
+ "t\n",
+ "e\n",
+ " \n",
+ "t\n",
+ "i\n",
+ "l\n",
+ "l\n",
+ " \n",
+ "g\n",
+ "o\n",
+ "l\n",
+ "d\n",
+ "e\n",
+ "n\n",
+ " \n",
+ "b\n",
+ "r\n",
+ "o\n",
+ "w\n",
+ "n\n",
+ " \n",
+ "o\n",
+ "n\n",
+ " \n",
+ "o\n",
+ "i\n",
+ "l\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "a\n",
+ "l\n",
+ "l\n",
+ "o\n",
+ "w\n",
+ " \n",
+ "t\n",
+ "o\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "o\n",
+ "l\n",
+ "\n",
+ "\n",
+ "A\n",
+ "f\n",
+ "t\n",
+ "e\n",
+ "r\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "o\n",
+ "l\n",
+ "i\n",
+ "n\n",
+ "g\n",
+ " \n",
+ "g\n",
+ "r\n",
+ "i\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "m\n",
+ "a\n",
+ "s\n",
+ "h\n",
+ "e\n",
+ "d\n",
+ " \n",
+ "b\n",
+ "e\n",
+ "e\n",
+ "t\n",
+ "r\n",
+ "o\n",
+ "o\n",
+ "t\n",
+ ",\n",
+ " \n",
+ "c\n",
+ "a\n",
+ "r\n",
+ "r\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "s\n",
+ "a\n",
+ "u\n",
+ "t\n",
+ "e\n",
+ "d\n",
+ " \n",
+ "o\n",
+ "n\n",
+ "i\n",
+ "o\n",
+ "n\n",
+ "s\n",
+ " \n",
+ "t\n",
+ "o\n",
+ " \n",
+ "p\n",
+ "a\n",
+ "s\n",
+ "t\n",
+ "e\n",
+ " \n",
+ "i\n",
+ "n\n",
+ " \n",
+ "a\n",
+ " \n",
+ "m\n",
+ "i\n",
+ "x\n",
+ "y\n",
+ ".\n",
+ " \n",
+ "a\n",
+ "d\n",
+ "d\n",
+ " \n",
+ "e\n",
+ "n\n",
+ "o\n",
+ "u\n",
+ "g\n",
+ "h\n",
+ " \n",
+ "w\n",
+ "a\n",
+ "t\n",
+ "e\n",
+ "r\n",
+ " \n",
+ "t\n",
+ "o\n",
+ " \n",
+ "m\n",
+ "a\n",
+ "k\n",
+ "e\n",
+ " \n",
+ "p\n",
+ "a\n",
+ "s\n",
+ "t\n",
+ "e\n",
+ " \n",
+ "n\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "v\n",
+ "e\n",
+ "r\n",
+ "y\n",
+ " \n",
+ "f\n",
+ "i\n",
+ "n\n",
+ "e\n",
+ ".\n",
+ "\n",
+ "\n",
+ "N\n",
+ "o\n",
+ "w\n",
+ " \n",
+ "s\n",
+ "t\n",
+ "r\n",
+ "a\n",
+ "i\n",
+ "n\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "g\n",
+ "r\n",
+ "i\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "p\n",
+ "a\n",
+ "s\n",
+ "t\n",
+ "e\n",
+ ".\n",
+ "\n",
+ "\n",
+ "T\n",
+ "a\n",
+ "k\n",
+ "e\n",
+ " \n",
+ "s\n",
+ "o\n",
+ "m\n",
+ "e\n",
+ " \n",
+ "b\n",
+ "u\n",
+ "t\n",
+ "t\n",
+ "e\n",
+ "r\n",
+ " \n",
+ "i\n",
+ "n\n",
+ " \n",
+ "a\n",
+ " \n",
+ "v\n",
+ "e\n",
+ "s\n",
+ "s\n",
+ "e\n",
+ "l\n",
+ " \n",
+ "a\n",
+ "d\n",
+ "d\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "g\n",
+ "a\n",
+ "r\n",
+ "l\n",
+ "i\n",
+ "c\n",
+ " \n",
+ "p\n",
+ "a\n",
+ "s\n",
+ "t\n",
+ "e\n",
+ ",\n",
+ "g\n",
+ "i\n",
+ "n\n",
+ "g\n",
+ "e\n",
+ "r\n",
+ "p\n",
+ "a\n",
+ "s\n",
+ "t\n",
+ "e\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "g\n",
+ "r\n",
+ "e\n",
+ "e\n",
+ "n\n",
+ " \n",
+ "c\n",
+ "h\n",
+ "i\n",
+ "l\n",
+ "l\n",
+ "i\n",
+ " \n",
+ "p\n",
+ "a\n",
+ "s\n",
+ "t\n",
+ "e\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "s\n",
+ "a\n",
+ "u\n",
+ "t\n",
+ "e\n",
+ " \n",
+ "f\n",
+ "o\n",
+ "r\n",
+ " \n",
+ "s\n",
+ "o\n",
+ "m\n",
+ "e\n",
+ " \n",
+ "t\n",
+ "i\n",
+ "m\n",
+ "e\n",
+ " \n",
+ "t\n",
+ "i\n",
+ "l\n",
+ "l\n",
+ " \n",
+ "u\n",
+ " \n",
+ "g\n",
+ "e\n",
+ "t\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "s\n",
+ "p\n",
+ "e\n",
+ "c\n",
+ "i\n",
+ "f\n",
+ "i\n",
+ "c\n",
+ " \n",
+ "g\n",
+ "a\n",
+ "r\n",
+ "l\n",
+ "i\n",
+ "c\n",
+ "-\n",
+ "b\n",
+ "e\n",
+ "i\n",
+ "n\n",
+ "g\n",
+ "-\n",
+ "f\n",
+ "r\n",
+ "i\n",
+ "e\n",
+ "d\n",
+ " \n",
+ "s\n",
+ "m\n",
+ "e\n",
+ "l\n",
+ "l\n",
+ "\n",
+ "\n",
+ "A\n",
+ "d\n",
+ "d\n",
+ " \n",
+ "m\n",
+ "o\n",
+ "o\n",
+ "n\n",
+ "g\n",
+ " \n",
+ "s\n",
+ "p\n",
+ "r\n",
+ "o\n",
+ "u\n",
+ "t\n",
+ "s\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "s\n",
+ "a\n",
+ "u\n",
+ "t\n",
+ "e\n",
+ ",\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "v\n",
+ "e\n",
+ "r\n",
+ " \n",
+ "w\n",
+ "i\n",
+ "t\n",
+ "h\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "l\n",
+ "i\n",
+ "d\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "l\n",
+ "e\n",
+ "t\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "o\n",
+ "k\n",
+ " \n",
+ "f\n",
+ "o\n",
+ "r\n",
+ " \n",
+ "5\n",
+ " \n",
+ "m\n",
+ "i\n",
+ "n\n",
+ "s\n",
+ "\n",
+ "\n",
+ "N\n",
+ "o\n",
+ "w\n",
+ " \n",
+ "a\n",
+ "d\n",
+ "d\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "s\n",
+ "t\n",
+ "r\n",
+ "a\n",
+ "i\n",
+ "n\n",
+ " \n",
+ "o\n",
+ "f\n",
+ " \n",
+ "b\n",
+ "e\n",
+ "e\n",
+ "t\n",
+ "r\n",
+ "o\n",
+ "o\n",
+ "t\n",
+ ",\n",
+ "c\n",
+ "a\n",
+ "r\n",
+ "r\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "o\n",
+ "n\n",
+ "i\n",
+ "o\n",
+ "n\n",
+ "s\n",
+ " \n",
+ "p\n",
+ "a\n",
+ "s\n",
+ "t\n",
+ "e\n",
+ "\n",
+ "\n",
+ "A\n",
+ "d\n",
+ "d\n",
+ " \n",
+ "6\n",
+ " \n",
+ "c\n",
+ "u\n",
+ "p\n",
+ "s\n",
+ " \n",
+ "o\n",
+ "f\n",
+ " \n",
+ "w\n",
+ "a\n",
+ "t\n",
+ "e\n",
+ "r\n",
+ " \n",
+ "t\n",
+ "o\n",
+ " \n",
+ "i\n",
+ "t\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "a\n",
+ "l\n",
+ "s\n",
+ "o\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "w\n",
+ "a\n",
+ "t\n",
+ "e\n",
+ "r\n",
+ " \n",
+ "r\n",
+ "e\n",
+ "s\n",
+ "e\n",
+ "r\n",
+ "v\n",
+ "e\n",
+ "d\n",
+ " \n",
+ "a\n",
+ "t\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "t\n",
+ "i\n",
+ "m\n",
+ "e\n",
+ " \n",
+ "o\n",
+ "f\n",
+ " \n",
+ "p\n",
+ "r\n",
+ "e\n",
+ "s\n",
+ "s\n",
+ "u\n",
+ "r\n",
+ "e\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "o\n",
+ "k\n",
+ "i\n",
+ "n\n",
+ "g\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "b\n",
+ "e\n",
+ "e\n",
+ "t\n",
+ "r\n",
+ "o\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "c\n",
+ "a\n",
+ "r\n",
+ "r\n",
+ "o\n",
+ "t\n",
+ ".\n",
+ "\n",
+ "\n",
+ "A\n",
+ "d\n",
+ "d\n",
+ " \n",
+ "s\n",
+ "a\n",
+ "l\n",
+ "t\n",
+ ",\n",
+ " \n",
+ "c\n",
+ "i\n",
+ "n\n",
+ "n\n",
+ "a\n",
+ "m\n",
+ "o\n",
+ "n\n",
+ ",\n",
+ " \n",
+ "c\n",
+ "u\n",
+ "m\n",
+ "m\n",
+ "i\n",
+ "n\n",
+ ",\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "r\n",
+ "i\n",
+ "a\n",
+ "n\n",
+ "d\n",
+ "e\n",
+ "r\n",
+ " \n",
+ "p\n",
+ "o\n",
+ "w\n",
+ "d\n",
+ "e\n",
+ "r\n",
+ ",\n",
+ "l\n",
+ "e\n",
+ "m\n",
+ "o\n",
+ "n\n",
+ " \n",
+ "j\n",
+ "u\n",
+ "i\n",
+ "c\n",
+ "e\n",
+ "\n",
+ "\n",
+ "B\n",
+ "r\n",
+ "i\n",
+ "n\n",
+ "g\n",
+ " \n",
+ "t\n",
+ "o\n",
+ " \n",
+ "a\n",
+ " \n",
+ "b\n",
+ "o\n",
+ "i\n",
+ "l\n",
+ ",\n",
+ " \n",
+ "l\n",
+ "e\n",
+ "t\n",
+ " \n",
+ "s\n",
+ "i\n",
+ "m\n",
+ "m\n",
+ "e\n",
+ "r\n",
+ " \n",
+ "f\n",
+ "o\n",
+ "r\n",
+ " \n",
+ "a\n",
+ " \n",
+ "f\n",
+ "e\n",
+ "w\n",
+ " \n",
+ "m\n",
+ "i\n",
+ "n\n",
+ "s\n",
+ "\n",
+ "\n",
+ "M\n",
+ "a\n",
+ "k\n",
+ "e\n",
+ " \n",
+ "a\n",
+ " \n",
+ "f\n",
+ "i\n",
+ "n\n",
+ "e\n",
+ " \n",
+ "p\n",
+ "a\n",
+ "s\n",
+ "t\n",
+ "e\n",
+ " \n",
+ "o\n",
+ "f\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "r\n",
+ "n\n",
+ "f\n",
+ "l\n",
+ "o\n",
+ "u\n",
+ "r\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "a\n",
+ "d\n",
+ "d\n",
+ " \n",
+ "i\n",
+ "t\n",
+ " \n",
+ "w\n",
+ "h\n",
+ "i\n",
+ "l\n",
+ "e\n",
+ " \n",
+ "s\n",
+ "t\n",
+ "i\n",
+ "r\n",
+ "r\n",
+ "i\n",
+ "n\n",
+ "g\n",
+ " \n",
+ "t\n",
+ "o\n",
+ " \n",
+ "a\n",
+ "v\n",
+ "o\n",
+ "i\n",
+ "d\n",
+ " \n",
+ "l\n",
+ "u\n",
+ "m\n",
+ "p\n",
+ "s\n",
+ "\n",
+ "\n",
+ "T\n",
+ "i\n",
+ "l\n",
+ " \n",
+ "f\n",
+ "o\n",
+ "r\n",
+ " \n",
+ "s\n",
+ "o\n",
+ "m\n",
+ "e\n",
+ "m\n",
+ "o\n",
+ "r\n",
+ "e\n",
+ " \n",
+ "t\n",
+ "i\n",
+ "m\n",
+ "e\n",
+ " \n",
+ "s\n",
+ "o\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "a\n",
+ "t\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "r\n",
+ "n\n",
+ "f\n",
+ "l\n",
+ "o\n",
+ "u\n",
+ "r\n",
+ " \n",
+ "p\n",
+ "a\n",
+ "s\n",
+ "t\n",
+ "e\n",
+ " \n",
+ "i\n",
+ "s\n",
+ " \n",
+ "c\n",
+ "o\n",
+ "o\n",
+ "k\n",
+ "e\n",
+ "d\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "m\n",
+ "i\n",
+ "x\n",
+ "e\n",
+ "d\n",
+ " \n",
+ "w\n",
+ "e\n",
+ "l\n",
+ "l\n",
+ "\n",
+ "\n",
+ "T\n",
+ "a\n",
+ "k\n",
+ "e\n",
+ " \n",
+ "o\n",
+ "i\n",
+ "l\n",
+ " \n",
+ "i\n",
+ "n\n",
+ " \n",
+ "a\n",
+ " \n",
+ "k\n",
+ "a\n",
+ "d\n",
+ "h\n",
+ "a\n",
+ "i\n",
+ " \n",
+ "t\n",
+ "o\n",
+ " \n",
+ "f\n",
+ "r\n",
+ "y\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "n\n",
+ "o\n",
+ "o\n",
+ "d\n",
+ "l\n",
+ "e\n",
+ "s\n",
+ ".\n",
+ "f\n",
+ "r\n",
+ "y\n",
+ " \n",
+ "t\n",
+ "i"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "l\n",
+ "l\n",
+ " \n",
+ "g\n",
+ "o\n",
+ "l\n",
+ "d\n",
+ "e\n",
+ "n\n",
+ " \n",
+ "b\n",
+ "r\n",
+ "o\n",
+ "w\n",
+ "n\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "r\n",
+ "e\n",
+ "s\n",
+ "e\n",
+ "r\n",
+ "v\n",
+ "e\n",
+ " \n",
+ "o\n",
+ "n\n",
+ " \n",
+ "a\n",
+ " \n",
+ "t\n",
+ "i\n",
+ "s\n",
+ "s\n",
+ "u\n",
+ "e\n",
+ " \n",
+ "p\n",
+ "a\n",
+ "p\n",
+ "e\n",
+ "r\n",
+ "\n",
+ "\n",
+ "C\n",
+ "r\n",
+ "u\n",
+ "s\n",
+ "h\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "n\n",
+ "o\n",
+ "o\n",
+ "d\n",
+ "l\n",
+ "e\n",
+ "s\n",
+ " \n",
+ "n\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "t\n",
+ "o\n",
+ "o\n",
+ " \n",
+ "m\n",
+ "u\n",
+ "c\n",
+ "h\n",
+ "\n",
+ "\n",
+ "S\n",
+ "e\n",
+ "r\n",
+ "v\n",
+ "e\n",
+ " \n",
+ "s\n",
+ "t\n",
+ "e\n",
+ "a\n",
+ "m\n",
+ "i\n",
+ "n\n",
+ "g\n",
+ " \n",
+ "h\n",
+ "o\n",
+ "t\n",
+ " \n",
+ "i\n",
+ "n\n",
+ " \n",
+ "s\n",
+ "o\n",
+ "u\n",
+ "p\n",
+ " \n",
+ "b\n",
+ "o\n",
+ "w\n",
+ "l\n",
+ "s\n",
+ " \n",
+ "a\n",
+ "d\n",
+ "d\n",
+ " \n",
+ "s\n",
+ "o\n",
+ "m\n",
+ "e\n",
+ " \n",
+ "b\n",
+ "u\n",
+ "t\n",
+ "t\n",
+ "e\n",
+ "r\n",
+ " \n",
+ "a\n",
+ "n\n",
+ "d\n",
+ " \n",
+ "t\n",
+ "o\n",
+ "p\n",
+ " \n",
+ "w\n",
+ "i\n",
+ "t\n",
+ "h\n",
+ " \n",
+ "t\n",
+ "h\n",
+ "e\n",
+ " \n",
+ "c\n",
+ "r\n",
+ "i\n",
+ "s\n",
+ "p\n",
+ "y\n",
+ " \n",
+ "n\n",
+ "o\n",
+ "o\n",
+ "d\n",
+ "l\n",
+ "e\n",
+ "s\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Opening a File , Page number: 421<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to check whether a file has been opened successfully before\n",
+ "trying to read or write to the file'''\n",
+ "\n",
+ "try :\n",
+ " fp = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" )#open file in read mode and check if file is opened successfully\n",
+ "except:\n",
+ " print \"cannot open file\" \n",
+ " exit()\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "cannot open file\n"
+ ]
+ }
+ ],
+ "prompt_number": "*"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Counting Characters,Tabs,Spaces and Newlines , Page number: 423<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to count chars, spaces, tabs and newlines in a file.'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "nol = 0\n",
+ "Not = 0\n",
+ "nob = 0\n",
+ "noc = 0\n",
+ "\n",
+ "fp = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #open file in read mode\n",
+ "while ( 1 ):\n",
+ " ch = fp.read(1)\n",
+ " if not ch :\n",
+ " break\n",
+ " noc = noc + 1 #number of chars\n",
+ " if ( ch == \" \" ):\n",
+ " nob = nob + 1 #number of spaces\n",
+ " if ( ch == \"\\n\" ):\n",
+ " nol = nol + 1 # number of newlines\n",
+ " if ( ch == \"\\t\" ):\n",
+ " Not = Not + 1 #number of tabs\n",
+ "\n",
+ "fp.close() #close file\n",
+ "\n",
+ "#Result\n",
+ "print \"Number of characters = \", noc \n",
+ "print \"Number of blanks = \", nob \n",
+ "print \"Number of tabs = \", Not \n",
+ "print \"Number of lines = \", nol \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number of characters = 1495\n",
+ "Number of blanks = 254\n",
+ "Number of tabs = 0\n",
+ "Number of lines = 20\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>File Copy , Page number: 424<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to take the contents of a file and\n",
+ "copy them into another file, character by character.'''\n",
+ "\n",
+ "fs = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #open file in read mode\n",
+ "if(not fs):\n",
+ " print \"Cannot open source file\"\n",
+ " exit()\n",
+ "\n",
+ "ft = open ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"w\" ) #open file in write mode\n",
+ "if(not ft):\n",
+ " print \"Cannot open target file\"\n",
+ " fs.close() \n",
+ " exit()\n",
+ " \n",
+ "while ( 1 ):\n",
+ " ch = fs.read(1)\n",
+ " if not ch :\n",
+ " break\n",
+ " else:\n",
+ " ft.writelines(ch) #write into target file\n",
+ "\n",
+ "#closen files\n",
+ "fs.close()\n",
+ "ft.close()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Writing Strings to Files, Page number: 427<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to receive strings from keyboard and write them to file'''\n",
+ "\n",
+ "fp = open ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"w\" ) #open file in write mode\n",
+ "if(not fp):\n",
+ " print \"Cannot open target file\" \n",
+ " exit()\n",
+ "\n",
+ "print \"Enter a few lines of text:\" \n",
+ "#s=input(\"\") #Input strings from keyboard\n",
+ "s = \"File written\"\n",
+ "print s\n",
+ "while ( len(s) > 0 ):\n",
+ " fp.writelines(s) #write into file\n",
+ " fp.writelines(\"\\n\")\n",
+ " #s=input(\"\") #Input strings from keyboard\n",
+ " s=\"\"\n",
+ " \n",
+ "#close files\n",
+ "fp.close()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a few lines of text:\n",
+ "File written\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Reading Strings from Files , Page number: 429<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to reads strings from the file and displays them on screen.'''\n",
+ "\n",
+ "fp = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #open file in read mode\n",
+ "if ( not fp ):\n",
+ " print \"Cannot open file\" \n",
+ " exit( ) \n",
+ "\n",
+ "while ( 1 ) :#Read strings from file\n",
+ " s = fp.read(79)\n",
+ " if(s):\n",
+ " print s\n",
+ " else:\n",
+ " break\n",
+ " \n",
+ "\n",
+ "fp.close() #close file\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Method\n",
+ "Throughly was both beetroot and carrot to remove the mud on beetroot and\n",
+ " the hair on carrot\n",
+ "Peel off the carrot skin. do not peel the raddish skin to a\n",
+ "void possible loss of vitamins\n",
+ "Cut each beetroot and carrot in to 4 halves\n",
+ "Prea\n",
+ "ssure cook the beetroot and carrot. make 3 whistles so thet they become very sm\n",
+ "ooth\n",
+ "Drain and reserve the water after pressure cooking.\n",
+ "Mash the cooked pieces\n",
+ " and allow to cool\n",
+ "Cut onions into cubes and saute till golden brown on oil and\n",
+ " allow to cool\n",
+ "After cooling grind the mashed beetroot, carrot and sauted onion\n",
+ "s to paste in a mixy. add enough water to make paste not very fine.\n",
+ "Now strain \n",
+ "the grind paste.\n",
+ "Take some butter in a vessel add the garlic paste,gingerpaste \n",
+ "and green chilli paste and saute for some time till u get the specific garlic-b\n",
+ "eing-fried smell\n",
+ "Add moong sprouts and saute, cover with the lid and let cook f\n",
+ "or 5 mins\n",
+ "Now add the strain of beetroot,carrot and onions paste\n",
+ "Add 6 cups of \n",
+ "water to it and also the water reserved at the time of pressure cooking the bee\n",
+ "troot and carrot.\n",
+ "Add salt, cinnamon, cummin, coriander powder,lemon juice\n",
+ "Brin\n",
+ "g to a boil, let simmer for a few mins\n",
+ "Make a fine paste of cornflour and add i\n",
+ "t while stirring to avoid lumps\n",
+ "Til for somemore time so that the cornflour pas\n",
+ "te is cooked and mixed well\n",
+ "Take oil in a kadhai to fry the noodles.fry till go\n",
+ "lden brown and reserve on a tissue paper\n",
+ "Crush the noodles not too much\n",
+ "Serve s\n",
+ "teaming hot in soup bowls add some butter and top with the crispy noodles\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Writing Records to Files , Page number: 431<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to write records to a file using structure'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "\n",
+ "#Variable declaration\n",
+ "another = 'Y'\n",
+ "\n",
+ "#Structure defintion\n",
+ "struct_emp = namedtuple(\"struct_emp\", \"name age bs\")\n",
+ "\n",
+ "\n",
+ "fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.dat\", \"w\" ) #open file in write mode\n",
+ "if(not fp):\n",
+ " print \"Cannot open target file\"\n",
+ " exit()\n",
+ "\n",
+ "while ( another == 'Y' ):\n",
+ " print \"Enter name, age and basic salary: \" \n",
+ " #en,ea,ebs = input(\"\").split()\n",
+ " en =\"John\"\n",
+ " ea=\"34\"\n",
+ " ebs=\"25000\"\n",
+ " print en,ea,ebs\n",
+ " e = struct_emp(en,ea,ebs)\n",
+ " fp.writelines(e) #write into file\n",
+ " fp.writelines(\"\\n\")\n",
+ " #another = input( \"Add another record (Y/N): \" ) \n",
+ " print \"Add another record (Y/N): \"\n",
+ " another = 'N'\n",
+ " print another\n",
+ "#close file\n",
+ "fp.close()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name, age and basic salary: \n",
+ "John 34 25000\n",
+ "Add another record (Y/N): \n",
+ "N\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Reading Records from File , Page number: 433<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to read records from a file using structure'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure defintion\n",
+ "struct_emp = namedtuple(\"struct_emp\", \"name age bs\")\n",
+ "\n",
+ "\n",
+ "fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.txt\", \"r\" ) #open file in read mode\n",
+ "if(not fp):\n",
+ " print \"Cannot open target file\"\n",
+ " exit()\n",
+ "\n",
+ "while ( 1 ):\n",
+ " s = fp.readline()\n",
+ " if(s):\n",
+ " en,ea,ebs = s.split()\n",
+ " e = struct_emp(en,ea,ebs) #Read record\n",
+ " print e.name, e.age, e.bs \n",
+ " else:\n",
+ " break\n",
+ " \n",
+ "#close file\n",
+ "fp.close()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "John 34 25000\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Copying Binary Files , Page number: 434<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to copy text as well as binary files'''\n",
+ "\n",
+ "fs = open (\"C:/Users/Akshatha M/Desktop/Project1.exe\", \"rb\" ) #open file in read binary mode\n",
+ "if(not fs):\n",
+ " print \"Cannot open source file\"\n",
+ " exit()\n",
+ "\n",
+ "ft = open ( \"C:/Users/Akshatha M/Desktop/NewProject1.exe\", \"wb\" ) #open file in write binary mode\n",
+ "if(not ft):\n",
+ " print \"Cannot open target file\"\n",
+ " fs.close() \n",
+ " exit()\n",
+ " \n",
+ "while ( 1 ):\n",
+ " ch = fs.read(1)\n",
+ " if not ch :\n",
+ " break\n",
+ " else:\n",
+ " ft.write(ch) #write into target file\n",
+ "\n",
+ "#closen files\n",
+ "fs.close()\n",
+ "ft.close()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Writing To Files in Binary Mode, Page number: 438<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to receive records from keyboard and write them to a file in binary mode'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "\n",
+ "#Variable declaration\n",
+ "another = 'Y'\n",
+ "\n",
+ "#Structure defintion\n",
+ "struct_emp = namedtuple(\"struct_emp\", \"name age bs\")\n",
+ "\n",
+ "fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.dat\", \"wb\" ) #open file in write mode\n",
+ "if(not fp):\n",
+ " print \"Cannot open target file\" \n",
+ " exit()\n",
+ "\n",
+ "while ( another == 'Y' ):\n",
+ " print ( \"Enter name, age and basic salary: \" )\n",
+ " #en,ea,ebs = input(\"\").split()\n",
+ " en =\"John\"\n",
+ " ea=\"34\"\n",
+ " ebs=\"25000\"\n",
+ " print en,ea,ebs\n",
+ " e = struct_emp(en,ea,ebs)\n",
+ " #write into file\n",
+ " fp.write(b'e.name')\n",
+ " fp.write(b'e.age')\n",
+ " fp.write(b'e.bs')\n",
+ " fp.write(b'\\n')\n",
+ " #another = input( \"Add another record (Y/N): \" ) \n",
+ " print \"Add another record (Y/N): \"\n",
+ " another = 'N'\n",
+ " print another\n",
+ "\n",
+ "#close file\n",
+ "fp.close()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name, age and basic salary: \n",
+ "John 34 25000\n",
+ "Add another record (Y/N): \n",
+ "N\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Reading From Binary Files , Page number: 440<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to read records from binary file and displays them on VDU'''\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure defintion\n",
+ "struct_emp = namedtuple(\"struct_emp\", \"name age bs\")\n",
+ "\n",
+ "\n",
+ "fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.txt\", \"rb\" ) #open file in read mode\n",
+ "if(not fp):\n",
+ " print \"Cannot open target file\" \n",
+ " exit()\n",
+ "\n",
+ "while ( 1 ):\n",
+ " s = fp.readline()\n",
+ " if(s):\n",
+ " en,ea,ebs = s.split()\n",
+ " e = struct_emp(en,ea,ebs) #Read record\n",
+ " print e.name, e.age, e.bs \n",
+ " else:\n",
+ " break\n",
+ " \n",
+ "#close file\n",
+ "fp.close()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "John 34 25000\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Database Management, Page number: 442<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''A menu-driven program for elementary database management'''\n",
+ "\n",
+ "import os\n",
+ "from collections import namedtuple\n",
+ "#Structure defintion\n",
+ "struct_emp = namedtuple(\"struct_emp\", \"name age bs\")\n",
+ "\n",
+ "fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.txt\", \"r+b\")\n",
+ "if ( not fp ):\n",
+ " fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.txt\", \"w+b\" )\n",
+ " if ( not fp ):\n",
+ " print \"Cannot open file\" \n",
+ " exit( )\n",
+ "\n",
+ "while ( 1 ):\n",
+ " print \" 1. Add Records\"\n",
+ " print \" 2. List Records\"\n",
+ " print \" 3. Modify Records\"\n",
+ " print \" 4. Delete Records\" \n",
+ " print \" 0. Exit\" \n",
+ " #choice = input(\"\\nYour choice\")\n",
+ " print \"\\nYour choice\"\n",
+ " choice = 1\n",
+ " if(choice == '1'):\n",
+ " another = 'Y'\n",
+ " while ( another == 'Y' ):\n",
+ " print ( \"Enter name, age and basic salary: \" )\n",
+ " en,ea,ebs = input(\"\").split()\n",
+ " e = struct_emp(en,ea,ebs)\n",
+ " #write into file\n",
+ " fp.write(b'e.name')\n",
+ " fp.write(b' ')\n",
+ " fp.write(b'e.age')\n",
+ " fp.write(b' ')\n",
+ " fp.write(b'e.bs')\n",
+ " fp.write(b'\\n')\n",
+ " another = input( \"Add another record (Y/N): \" )\n",
+ " elif(choice == '2'):\n",
+ " fp.seek(0,0)\n",
+ " while ( 1 ):\n",
+ " s = fp.readline()\n",
+ " if(s):\n",
+ " en,j1,ea,j2,ebs = s.split()\n",
+ " e = struct_emp(en,ea,ebs) #Read record\n",
+ " print ( e.name, e.age, e.bs )\n",
+ " else:\n",
+ " break\n",
+ " elif(choice == '3'):\n",
+ " another = 'Y'\n",
+ " while ( another == 'Y' ):\n",
+ " empname =input(\"Enter name of employee to modify \" )\n",
+ " fp.seek(0,0)\n",
+ " while (fp.readline()):\n",
+ " if ( b'empname' == b'e.name' ):\n",
+ " en,ea,ebs = input(\"Enter new name, age & bs\" ).spilt()\n",
+ " e = struct_emp(en,ea,ebs)\n",
+ " cur = fp.tell()\n",
+ " fp.write(b'e.name')\n",
+ " fp.write(b'e.age')\n",
+ " fp.write(b'e.bs')\n",
+ " fp.write(b'\\n')\n",
+ " break\n",
+ " print ( \"\\nModify another Record (Y/N) \" )\n",
+ " another = input(\"\")\n",
+ " elif(choice == '4'):\n",
+ " another = 'Y'\n",
+ " while ( another == 'Y' ):\n",
+ " empname = input(\"Enter name of employee to delete \" )\n",
+ " ft = open ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"wb\" )\n",
+ " fp.seek(0,0)\n",
+ " while ( 1 ):\n",
+ " s = fp.readline()\n",
+ " if(s):\n",
+ " if ( not(b'empname' == b'e.name' )):\n",
+ " ft.write(s)\n",
+ " else:\n",
+ " break\n",
+ " fp.close()\n",
+ " ft.close()\n",
+ " os.remove(\"C:/Users/Akshatha M/Desktop/Employee.txt\")\n",
+ " os.rename ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"C:/Users/Akshatha M/Desktop/Employee.txt\" )\n",
+ " fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.txt\", \"r+b\" )\n",
+ " print ( \"Delete another Record (Y/N) \" )\n",
+ " another = input(\"\")\n",
+ "\n",
+ " else:\n",
+ " fp.close()#close file\n",
+ " exit( )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Low Level File Copy, Page number: 448<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''File-copy program which copies text, .com and .exe files'''\n",
+ "\n",
+ "#source=input( \"Enter source file name \" ) \n",
+ "print \"Enter source file name \" \n",
+ "source = \"Employee.txt\"\n",
+ "print source\n",
+ "source=\"C:/Users/Akshatha M/Desktop/\"+source\n",
+ "\n",
+ "inhandle = open (source, \"rb\" ) #open file in read binary mode\n",
+ "if(not inhandle):\n",
+ " print \"Cannot open file\"\n",
+ " exit()\n",
+ "\n",
+ "#target=input( \"Enter target file name \" ) \n",
+ "print \"Enter target file name \"\n",
+ "target = \"temp.txt\"\n",
+ "print target\n",
+ "target=\"C:/Users/Akshatha M/Desktop/\"+target\n",
+ "\n",
+ "outhandle = open ( target, \"wb\" ) #open file in write binary mode\n",
+ "if(not outhandle):\n",
+ " print \"Cannot open target file\"\n",
+ " inhandle.close() \n",
+ " exit()\n",
+ "\n",
+ "\n",
+ " \n",
+ "while ( 1 ):\n",
+ " Bytes = inhandle.read(1)\n",
+ " if not Bytes :\n",
+ " break\n",
+ " else:\n",
+ " outhandle.write(Bytes) #write into target file\n",
+ "\n",
+ "#closen files\n",
+ "inhandle.close()\n",
+ "outhandle.close()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter source file name \n",
+ "Employee.txt\n",
+ "Enter target file name \n",
+ "temp.txt\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Detecting Errors in Reading/Writing, Page number: 470<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''A program that illustrates the usage of ferror( )'''\n",
+ "\n",
+ "\n",
+ "fp = open ( \"C:/Users/Akshatha/Documents/extrastuff/skills.txt\", \"w\" ) \n",
+ "while (1):\n",
+ " try:\n",
+ " ch = fp.read(1) #read character from file \n",
+ " except: #ferror()\n",
+ " print \"Error in reading file\" \n",
+ " break \n",
+ " print ch \n",
+ " \n",
+ "fp.close()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Error in reading file\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Standard IO devices, Page number: 472<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''A program to write a file to a printer'''\n",
+ "\n",
+ "import io\n",
+ "\n",
+ "try:\n",
+ " fp = open (\"C:/Users/Akshatha/Documents/extrastuff/skills.txt\", \"r\")\n",
+ "except:\n",
+ " print \"cannot open file\"\n",
+ " exit()\n",
+ " \n",
+ "io.open('stdprn','w') \n",
+ "try:\n",
+ " stdprn = io.open('stdprn', 'w') #open printer\n",
+ "\n",
+ " while ( 1 ):\n",
+ " ch = fp.read(1)\n",
+ " if not ch :\n",
+ " break\n",
+ " else:\n",
+ " stdprn.write(ch) #write into printer\n",
+ "except: #no printer\n",
+ " #closen files\n",
+ " fp.close()\n",
+ " stdprn.close()\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-13.ipynb b/Let_us_C/chapter-13.ipynb new file mode 100644 index 00000000..d3b93ca6 --- /dev/null +++ b/Let_us_C/chapter-13.ipynb @@ -0,0 +1,83 @@ +{
+ "metadata": {
+ "name": "chapter-13.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 13: More Issues In Input/Output<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Using argc and argv, Page number: 467<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''File-copy program usinng command line arguments'''\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "if ( len(sys.argv) != 3 ):\n",
+ " print ( \"Improper number of arguments\" )\n",
+ " exit( )\n",
+ " \n",
+ "fs = open ( sys.argv[2], \"r\" ) #open file in read mode\n",
+ "if ( not fs):\n",
+ " print ( \"Cannot open source file\" )\n",
+ " exit( )\n",
+ " \n",
+ "ft = open ( sys.argv[2], \"w\" ) #open file in write mode\n",
+ "if ( not ft ):\n",
+ " print ( \"Cannot open target file\" )\n",
+ " fs.close()\n",
+ " exit( )\n",
+ " \n",
+ "while ( 1 ):\n",
+ " ch = fs.read(1)\n",
+ " if ( not ch ):\n",
+ " break\n",
+ " else:\n",
+ " ft.write(ch)\n",
+ "\n",
+ "#close files \n",
+ "fs.close()\n",
+ "ft.close()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Improper number of arguments\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-14.ipynb b/Let_us_C/chapter-14.ipynb new file mode 100644 index 00000000..becf792e --- /dev/null +++ b/Let_us_C/chapter-14.ipynb @@ -0,0 +1,853 @@ +{
+ "metadata": {
+ "name": "chapter-14.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 14: Operations On Bits<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Binary Conversion, Page number: 483<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to print binary equivalent of integers using showbits( ) function'''\n",
+ "\n",
+ "#Function definition\n",
+ "def showbits ( n ):\n",
+ " a = 15\n",
+ " for i in range(0,16):\n",
+ " andmask = 1 << a\n",
+ " k = n & andmask\n",
+ " if k == 0:\n",
+ " print \"0\"\n",
+ " else:\n",
+ " print \"1\" \n",
+ " a = a-1\n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "for j in range(0,6):\n",
+ " print \"Decimal %d is same as binary \"%( j )\n",
+ " showbits ( j ) #function call\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Decimal 0 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "Decimal 1 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "Decimal 2 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "Decimal 3 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "Decimal 4 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "Decimal 5 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>One's Complement, Page number: 484<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to shows one\u2019s complement operator in action.'''\n",
+ "\n",
+ "#Function definition\n",
+ "def showbits ( n ):\n",
+ " a = 15\n",
+ " for i in range(0,16):\n",
+ " andmask = 1 << a\n",
+ " k = n & andmask\n",
+ " if k == 0:\n",
+ " print \"0\" \n",
+ " else:\n",
+ " print \"1\" \n",
+ " a = a-1\n",
+ "\n",
+ "\n",
+ "for j in range(0,4):\n",
+ " print \"Decimal %d is same as binary \"%(j )\n",
+ " showbits ( j ) \n",
+ " k = ~j \n",
+ " print \"One\u2019s complement of %d is \"%( j ) \n",
+ " showbits ( k )"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Decimal 0 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "One\u2019s complement of 0 is \n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "Decimal 1 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "One\u2019s complement of 1 is \n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "Decimal 2 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "One\u2019s complement of 2 is \n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "Decimal 3 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "One\u2019s complement of 3 is \n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "0\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>File Encryption, Page number: 485<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate file encryption utility'''\n",
+ "\n",
+ "#Function definition\n",
+ "def encrypt( ):\n",
+ " fs = open ( \"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #normal file \n",
+ " ft = open ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"w\" ) # encrypted file \n",
+ " if ( not fs or not ft ):\n",
+ " print \"File opening error!\" \n",
+ " exit()\n",
+ " while (1):\n",
+ " ch = fs.read(1)\n",
+ " if(not ch): #EOF\n",
+ " break\n",
+ " else:\n",
+ " ft.write(ascii(~ord(ch))) #complemented\n",
+ " #close files\n",
+ " fs.close()\n",
+ " ft.close()\n",
+ "\n",
+ "\n",
+ "encrypt() #function call\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Right Shift Operator, Page number: 487<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the effect of right shift operator.'''\n",
+ "\n",
+ "#Function definition\n",
+ "def showbits ( n ):\n",
+ " a = 15\n",
+ " for i in range(0,16):\n",
+ " andmask = 1 << a\n",
+ " k = n & andmask\n",
+ " if k == 0:\n",
+ " print \"0\" \n",
+ " else:\n",
+ " print \"1\" \n",
+ " a = a-1\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 5225\n",
+ "\n",
+ "print \"Decimal %d is same as binary \"%( i )\n",
+ "showbits(i)#function call\n",
+ "\n",
+ "for j in range(0,6):\n",
+ " k = i >>j #right shift \n",
+ " print \"%d right shift %d gives \" %(i, j )\n",
+ " showbits ( k ) #function call "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Decimal 5225 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "5225 right shift 0 gives \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "5225 right shift 1 gives \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "5225 right shift 2 gives \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "5225 right shift 3 gives \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "5225 right shift 4 gives \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "5225 right shift 5 gives \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Left Shift Operator, Page number: 488<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the effect of left shift operator.'''\n",
+ "\n",
+ "#Function definition\n",
+ "def showbits ( n ):\n",
+ " a = 15\n",
+ " for i in range(0,16):\n",
+ " andmask = 1 << a\n",
+ " k = n & andmask\n",
+ " if k == 0:\n",
+ " print \"0\" \n",
+ " else:\n",
+ " print \"1\" \n",
+ " a = a-1\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 5225\n",
+ "\n",
+ "print \"Decimal %d is same as binary \"%( i )\n",
+ "showbits(i)#function call\n",
+ "\n",
+ "for j in range(0,6):\n",
+ " k = i <<j #left shift \n",
+ " print \"%d left shift %d gives \" %(i, j )\n",
+ " showbits ( k ) #function call \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Decimal 5225 is same as binary \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "5225 left shift 0 gives \n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "5225 left shift 1 gives \n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "5225 left shift 2 gives \n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "5225 left shift 3 gives \n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "5225 left shift 4 gives \n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "5225 left shift 5 gives \n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "1\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "1\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n",
+ "0\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Date Field in Directory, Page number: 492<h3>\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to decode the date field in directory entry using bitwise operators'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "d = 9\n",
+ "m = 3\n",
+ "y = 1990\n",
+ "\n",
+ "#Calculation and result\n",
+ "date = ( y - 1980 ) * 512 + m * 32 + d \n",
+ "print \"Date = %u\"%( date ) \n",
+ "year = 1980 + ( date >> 9 ) \n",
+ "month = ( (date << 7 ) >> 12 ) \n",
+ "day = ( (date << 11 ) >> 11 ) \n",
+ "print \"Year = \", year \n",
+ "print \"Month = %u\" %(m)\n",
+ "print \"Day = %u\" %(d)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Date = 5225\n",
+ "Year = 1990\n",
+ "Month = 3\n",
+ "Day = 9\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Bitwise AND Operator, Page number: 495<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to test whether a bit in a number is ON or OFF'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 65\n",
+ "\n",
+ "print \"value of i = \", i \n",
+ "\n",
+ "j = i & 32 #bitwise and\n",
+ "if ( j == 0 ):\n",
+ " print \"and its fifth bit is off\" \n",
+ "else:\n",
+ " print \"and its fifth bit is on\"\n",
+ "\n",
+ "j = i & 64 #bitwise and\n",
+ "if ( j == 0 ):\n",
+ " print \"whereas its sixth bit is off\" \n",
+ "else:\n",
+ " print \"whereas its sixth bit is on\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "value of i = 65\n",
+ "and its fifth bit is off\n",
+ "whereas its sixth bit is on\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Bitwise XOR Operator, Page number: 500<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate that a number XORed with another number\n",
+ "twice gives the original number.'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "b = 50\n",
+ "\n",
+ "#Calculation and result\n",
+ "b = b ^ 12 \n",
+ "print b # this will print 62 \n",
+ "b = b ^ 12 \n",
+ "print b # this will print 50 \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "62\n",
+ "50\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-15.ipynb b/Let_us_C/chapter-15.ipynb new file mode 100644 index 00000000..bf1ab52b --- /dev/null +++ b/Let_us_C/chapter-15.ipynb @@ -0,0 +1,621 @@ +{
+ "metadata": {
+ "name": "chapter-15.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 15: Miscellaneous Features <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Enumerated Data Type , Page number: 508<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to print a payroll using enum'''\n",
+ "\n",
+ "def enum(**enums):\n",
+ " return type('Enum', (), enums)\n",
+ "#Enum declaration\n",
+ "emp_dept = enum(assembly = 0, manufacturing = 1,accounts = 2, stores = 3)\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure declaration\n",
+ "struct_employee = namedtuple(\"struct_employee\", \"name age bs emp_dept\")\n",
+ "\n",
+ "\n",
+ "#Structure for employee\n",
+ "department = emp_dept.manufacturing\n",
+ "e = struct_employee(\"Lothar Mattheus\",28,5575.50,department)\n",
+ "\n",
+ "#Result\n",
+ "print \"Name = \",e.name\n",
+ "print \"Age = \",e.age\n",
+ "print \"Basic salary = \",e.bs\n",
+ "print \"Dept = \",e.emp_dept\n",
+ "\n",
+ "if(e.emp_dept == 2):\n",
+ " print \"%s is an accountant\" %(e.name)\n",
+ "else:\n",
+ " print \"%s is not an accountant\" %(e.name)\n",
+ " \n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name = Lothar Mattheus\n",
+ "Age = 28\n",
+ "Basic salary = 5575.5\n",
+ "Dept = 1\n",
+ "Lothar Mattheus is not an accountant\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Are Enums Necessary, Page number: 510<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to achieve the task of enums using macros'''\n",
+ "\n",
+ "#macro definition\n",
+ "ASSEMBLY = 0\n",
+ "MANUFACTURING = 1\n",
+ "ACCOUNTS = 2\n",
+ "STORES = 3\n",
+ "\n",
+ "from collections import namedtuple\n",
+ "#Structure declaration\n",
+ "struct_employee = namedtuple(\"struct_employee\", \"name age bs department\")\n",
+ "\n",
+ "#Structure for employee\n",
+ "e = struct_employee(\"Lothar Mattheus\",28,5575.50,MANUFACTURING)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Division Without Typecasting , Page number: 513<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate no typecasting'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "x=6\n",
+ "y=4\n",
+ "\n",
+ "#Calculation\n",
+ "a = int( x/y) \n",
+ "\n",
+ "#Result\n",
+ "print \"Value of a = %f\" %(a)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Value of a = 1.000000\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Division With Typecasting , Page number: 513<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate explicit typecasting'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "x=6\n",
+ "y=4\n",
+ "\n",
+ "#Calculation\n",
+ "a = float( x)/y\n",
+ "\n",
+ "#Result\n",
+ "print \"Value of a = %f\" %(a)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Value of a = 1.500000\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Type Casting , Page number: 514<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate typecasting'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=6.35\n",
+ "\n",
+ "#Result\n",
+ "print \"Value of a on typecasting = %d\" %(int(a))\n",
+ "print \"Value of a = %f\"%(a)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Value of a on typecasting = 6\n",
+ "Value of a = 6.350000\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Bit Fields , Page number: 516<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate bit fields in structures'''\n",
+ "\n",
+ "from ctypes import *\n",
+ "import ctypes\n",
+ "import math\n",
+ "\n",
+ "#macro definition\n",
+ "MALE = 0\n",
+ "FEMALE = 1\n",
+ "SINGLE = 0\n",
+ "MARRIED = 1\n",
+ "DIVORCED = 2\n",
+ "WIDOWED = 3\n",
+ "\n",
+ "#Structure declaration\n",
+ "class employee(Structure):\n",
+ " _fields_ = [(\"gender\", c_int, 1),(\"mar_stat\", c_int, 3),(\"hobby\", c_int, 3),(\"scheme\", c_int, 4)]\n",
+ " _sizeof_ = 2\n",
+ "#Structure for employee\n",
+ "e = employee()\n",
+ "e.gender = MALE\n",
+ "e.mar_stat = DIVORCED\n",
+ "e.hobby = 5\n",
+ "e.scheme =9\n",
+ "\n",
+ "#Result\n",
+ "print \"Gender = \",e.gender\n",
+ "print \"Marital status = \",e.mar_stat\n",
+ "print \"Bytes occupied by e = \",sizeof(e)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Gender = 0\n",
+ "Marital status = 2\n",
+ "Bytes occupied by e = 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Address of a Function , Page number: 517<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate pointers to functions'''\n",
+ "\n",
+ "#Function definition\n",
+ "def display():\n",
+ " print \"Long live viruses!!\"\n",
+ "\n",
+ "#Result\n",
+ "print \"Address of function display is \",id(display)\n",
+ "display() #function call\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of function display is 133993192\n",
+ "Long live viruses!!\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Invoking a Function Using Pointer, Page number: 518<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to invoke a function using a pointer to a function'''\n",
+ "\n",
+ "#Function definition\n",
+ "def display():\n",
+ " print \"Long live viruses!!\"\n",
+ "\n",
+ "func_ptr = id(display) #assigning address of function\n",
+ "print \"Address of function display is \",func_ptr\n",
+ "display() #function call\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of function display is 133993080\n",
+ "Long live viruses!!\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Functions Returning Pointers , Page number: 520<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate functions returning pointers'''\n",
+ "\n",
+ "#Function definition\n",
+ "def fun():\n",
+ " i = 20\n",
+ " return (i)\n",
+ "\n",
+ "#Result\n",
+ "p =fun() #function call\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>String Copy , Page number: 520<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program which copies one string into another and\n",
+ "returns the pointer to the target string'''\n",
+ "\n",
+ "#Function definition\n",
+ "def copy(t,s):\n",
+ " i = 0\n",
+ " while ( s[i] != '\\0' ):\n",
+ " t = t + s[i]\n",
+ " i = i + 1\n",
+ " return t\n",
+ "\n",
+ "#Variable declaration\n",
+ "source = \"Jaded\\0\" \n",
+ "target = ''\n",
+ "string = copy( target, source ) # function call\n",
+ "\n",
+ "#Result\n",
+ "print string\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Jaded\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Findmax Function , Page number: 522<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate a findmax() function\n",
+ "to find out max value from a set of values\n",
+ "irrespective of number of values passed to it'''\n",
+ "\n",
+ "#function declaration\n",
+ "def findmax(*arg):\n",
+ " maxi = arg[1]\n",
+ " for count in range(2,arg[0]):\n",
+ " num = arg[count]\n",
+ " if (num > maxi):\n",
+ " maxi = num\n",
+ " return maxi\n",
+ "\n",
+ "maxi = findmax(5,23,15,1,92,50)#function call \n",
+ "print \"maximum = \",maxi\n",
+ "\n",
+ "maxi = findmax(3,100,300,29)#function call \n",
+ "print \"maximum = \",maxi\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "maximum = 92\n",
+ "maximum = 300\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Display Function, Page number: 524<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate a display() function\n",
+ "to print any number of arguments of any type'''\n",
+ "\n",
+ "#function declaration\n",
+ "def display(*arg):\n",
+ " if(arg[0] == 1): #case int\n",
+ " for j in range(2,arg[1]+2):\n",
+ " i = arg[j]\n",
+ " print \"%d\"%(i)\n",
+ " elif(arg[0] == 2): #case char\n",
+ " for j in range(2,arg[1]+2):\n",
+ " i = arg[j]\n",
+ " print \"%c\"%(i)\n",
+ " elif(arg[0] == 3): #case double\n",
+ " for j in range(2,arg[1]+2):\n",
+ " i = arg[j]\n",
+ " print \"%lf\"%(i)\n",
+ " \n",
+ "#function calls\n",
+ "display(1,2,5,6)\n",
+ "display(2,4,'A','a','b','c')\n",
+ "display(3,3,2.5,299.3,-1.0)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n",
+ "6\n",
+ "A\n",
+ "a\n",
+ "b\n",
+ "c\n",
+ "2.500000\n",
+ "299.300000\n",
+ "-1.000000\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Union Demo , Page number: 526<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a union at work'''\n",
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "#union declaration\n",
+ "class union_a(Union):\n",
+ " _fields_ = [(\"i\", c_int),\n",
+ " (\"ch\", ((c_char * 1)*2))]\n",
+ "\n",
+ "key = union_a()\n",
+ "key.i = 512\n",
+ "print \"key.i = \",key.i\n",
+ "print \"key.ch[0] = \",(key.ch[0][0])\n",
+ "print \"key.ch[1] = \",(key.ch[1][0])\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Union Example , Page number: 530<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate that we cant assign different values to\n",
+ "different union element at the same time'''\n",
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "#Union declaration\n",
+ "class union_a(Union):\n",
+ " _fields_ = [(\"i\", c_int),\n",
+ " (\"ch\", ((c_char * 1)*2))]\n",
+ "\n",
+ "key = union_a()\n",
+ "key.i = 512\n",
+ "print \"key.i = \",key.i\n",
+ "print \"key.ch[0] = \",(key.ch[0][0])\n",
+ "print \"key.ch[1] = \",(key.ch[1][0])\n",
+ "\n",
+ "key.ch[0][0] = 50 #assign new value to key.ch[0]\n",
+ "print\"key.i = \",key.i\n",
+ "print\"key.ch[0] = \",(key.ch[0][0])\n",
+ "print\"key.ch[1] = \",(key.ch[1][0])\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Union of Structures , Page number: 531<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate structures nested in a union'''\n",
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "#Structure declarations\n",
+ "class a(Structure):\n",
+ " _fields_ = [(\"i\", c_int),\n",
+ " (\"c\", ((c_char * 1)*2))]\n",
+ "\n",
+ "class b(Structure):\n",
+ " _fields_ = [(\"j\", c_int),\n",
+ " (\"d\", ((c_char * 1)*2))]\n",
+ " \n",
+ "#Union declaration\n",
+ "class union_z(Union):\n",
+ " _fields_ = [(\"key\", a),\n",
+ " (\"data\", b )]\n",
+ "\n",
+ "strange = union_z()\n",
+ "strange.key.i = 512\n",
+ "strange.data.d[0][0] = 0\n",
+ "strange.data.d[1][0] = 32\n",
+ "\n",
+ "print strange.key.i\n",
+ "print strange.data.j\n",
+ "print strange.key.c[0][0]\n",
+ "print strange.data.d[0][0]\n",
+ "print strange.key.c[1][0]\n",
+ "print strange.data.d[1][0]\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-16.ipynb b/Let_us_C/chapter-16.ipynb new file mode 100644 index 00000000..8c3c03f2 --- /dev/null +++ b/Let_us_C/chapter-16.ipynb @@ -0,0 +1,66 @@ +{
+ "metadata": {
+ "name": "chapter-16.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 16: C Under Windows<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>The first Windows Program, Page number: 554<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to display a \u201cHello\u201d message in a message box'''\n",
+ "\n",
+ "import ctypes\n",
+ "MessageBox = ctypes.windll.user32.MessageBoxW\n",
+ "MessageBox(None, 'Hello', 'Title', 0)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 1,
+ "text": [
+ "1"
+ ]
+ },
+ {
+ "output_type": "pyout",
+ "prompt_number": 2,
+ "text": [
+ "1"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-17.ipynb b/Let_us_C/chapter-17.ipynb new file mode 100644 index 00000000..1b6a9007 --- /dev/null +++ b/Let_us_C/chapter-17.ipynb @@ -0,0 +1,115 @@ +{
+ "metadata": {
+ "name": "chapter-17.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 17: Windows Programming<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Message Box, Page number: 563<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to create a window on the screen.'''\n",
+ "\n",
+ "from tkinter import *\n",
+ "\n",
+ "\n",
+ "#creating window\n",
+ "root = Tk()\n",
+ "root.title(\"Press Me\")\n",
+ "button1 = Button(root, text=\"Press Me\") #creating button\n",
+ "button1.pack()\n",
+ "import ctypes #creating message box\n",
+ "MessageBox = ctypes.windll.user32.MessageBoxW\n",
+ "MessageBox(None, 'Hi!', 'Waiting', 0)\n",
+ "root.mainloop()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>More Windows, Page number: 566<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to create several windows on the screen'''\n",
+ "\n",
+ "from tkinter import *\n",
+ "root = []\n",
+ "#creating windows\n",
+ "for x in range(0,10):\n",
+ " root.append(Tk())\n",
+ " root[x].title(\"Press Me\")\n",
+ " button1 = Button(root[x], text=\"Press Me\") #creating button\n",
+ " button1.pack()\n",
+ " \n",
+ "import ctypes #creating message box\n",
+ "MessageBox = ctypes.windll.user32.MessageBoxW\n",
+ "MessageBox(None, 'Hi!', 'Waiting', 0)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>A Real World Window, Page number: 568<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to create a window using window class and interact with it'''\n",
+ "\n",
+ "from tkinter import *\n",
+ "\n",
+ "\n",
+ "class Example(Frame):\n",
+ " def __init__(self, parent):\n",
+ " Frame.__init__(self, parent)\n",
+ "\n",
+ " self.display = Canvas(self, width=700, height=200)\n",
+ " self.display.pack(side=\"top\", fill=\"both\", expand=True)\n",
+ " \n",
+ "if __name__ == \"__main__\":\n",
+ " root = Tk()\n",
+ " root.title(\"Title\")\n",
+ " Frame = Example(parent=root)\n",
+ " Frame.pack(side=\"top\", fill=\"both\", expand=True)\n",
+ " root.mainloop()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-18.ipynb b/Let_us_C/chapter-18.ipynb new file mode 100644 index 00000000..7a30b0b6 --- /dev/null +++ b/Let_us_C/chapter-18.ipynb @@ -0,0 +1,261 @@ +{
+ "metadata": {
+ "name": "chapter-18.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 18: Graphics Under Windows<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Hello Windows, Page number: 582<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to display a message \u201cHello Windows\u201d in different fonts'''\n",
+ "\n",
+ "from tkinter import *\n",
+ "\n",
+ "\n",
+ "class Example(Frame):\n",
+ " def __init__(self, parent):\n",
+ " Frame.__init__(self, parent)\n",
+ "\n",
+ " self.display = Canvas(self, width=700, height=200)\n",
+ " self.display.pack(side=\"top\", fill=\"both\", expand=True)\n",
+ " self.display.create_text(10, 10, fill = \"blue\",text = \"Hello Windows\", font=\"Arial 20 italic\",\n",
+ " anchor=\"nw\")\n",
+ " self.display.create_text(10, 50, fill = \"blue\",text = \"Hello Windows\", font=\"TimesNewRoman 30 italic\",\n",
+ " anchor=\"nw\")\n",
+ " self.display.create_text(10, 100, fill = \"blue\",text = \"Hello Windows\", font=\"ComicSansMS 40 italic\",\n",
+ " anchor=\"nw\")\n",
+ "\n",
+ "if __name__ == \"__main__\":\n",
+ " root = Tk()\n",
+ " root.title(\"Text\")\n",
+ " Frame = Example(parent=root)\n",
+ " Frame.pack(side=\"top\", fill=\"both\", expand=True)\n",
+ " root.mainloop()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Drawing Shapes, Page number: 587<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that displays different shapes in a window'''\n",
+ "\n",
+ "from tkinter import *\n",
+ "\n",
+ "\n",
+ "top = Tk()\n",
+ "top.title(\"Shapes\")\n",
+ "C = Canvas(top, height=500, width=500)\n",
+ "rcoor = 10,20,200,100\n",
+ "rect = C.create_rectangle(rcoor,fill=\"blue\")#rectangle\n",
+ "ecoor = 10,280,200,380\n",
+ "ellipse = C.create_oval(ecoor,fill = \"blue\")#ellipse\n",
+ "picoor = 250,0,350,100\n",
+ "pie = C.create_arc(picoor, start=300, extent=100, fill=\"blue\")#pie\n",
+ "pocoor = 250, 150, 250, 300, 300, 350, 400, 300, 320, 190\n",
+ "polygon = C.create_polygon(pocoor,fill=\"blue\")#polygon\n",
+ "#roundedrectangle\n",
+ "c1= C.create_arc(155,115,195,150,start=320, extent=80, fill=\"blue\",outline=\"blue\")\n",
+ "c2= C.create_arc(155,208,195,243,start=320, extent=80, fill=\"blue\",outline=\"blue\")\n",
+ "c3= C.create_arc(25,118,60,153,start=100, extent=150, fill=\"blue\",outline=\"blue\")\n",
+ "c4= C.create_arc(25,207,60,242,start=100, extent=150, fill=\"blue\",outline=\"blue\")\n",
+ "roundrect = C.create_rectangle(30,120,190,240,fill=\"blue\",outline=\"blue\")\n",
+ "C.pack()\n",
+ "top.mainloop()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Pen Styles, Page number: 590<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to create pens of different style, color and\n",
+ "thickness to draw'''\n",
+ "\n",
+ "from tkinter import *\n",
+ "\n",
+ "\n",
+ "top = Tk()\n",
+ "top.title(\"Pen styles\")\n",
+ "C = Canvas(top, height=100, width=500)\n",
+ "l1 = C.create_line(0,10,500,10,fill=\"red\",dash=(5)) #dashed line\n",
+ "l2 = C.create_line(0,30,500,30,fill=\"red\",dash=(1)) #dotted line\n",
+ "l3 = C.create_line(0,50,500,50,fill=\"red\",dash=(5,1,1,1)) #dash dot\n",
+ "l4 = C.create_line(0,70,500,70,fill=\"red\",dash=(5,1,1,1,1)) #dash dot dot\n",
+ "l5 = C.create_line(0,90,500,90,fill=\"red\",width=4) #solid line\n",
+ "C.pack()\n",
+ "top.mainloop()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Types of Brushes, Page number: 592<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "from PyQt4 import QtGui, QtCore\n",
+ "\n",
+ "\n",
+ "class Example(QtGui.QWidget):\n",
+ " \n",
+ " def __init__(self):\n",
+ " super(Example, self).__init__()\n",
+ " \n",
+ " self.initUI()\n",
+ " \n",
+ " def initUI(self): \n",
+ "\n",
+ " self.setGeometry(300, 300, 355, 280)\n",
+ " self.setWindowTitle('Brush Styles')\n",
+ " self.show()\n",
+ "\n",
+ " def paintEvent(self, e):\n",
+ "\n",
+ " qp = QtGui.QPainter()\n",
+ " qp.begin(self)\n",
+ " self.drawBrushes(qp)\n",
+ " qp.end()\n",
+ " \n",
+ " def drawBrushes(self, qp):\n",
+ " \n",
+ " brush = QtGui.QBrush(QtCore.Qt.SolidPattern)\n",
+ " qp.setBrush(brush)\n",
+ " qp.drawRect(10, 15, 90, 60)\n",
+ "\n",
+ " brush.setStyle(QtCore.Qt.CrossPattern)\n",
+ " qp.setBrush(brush)\n",
+ " qp.drawRect(130, 15, 90, 60)\n",
+ "\n",
+ " \n",
+ " image = QtGui.QImage(\"C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg\")\n",
+ " brush.setTextureImage (image)\n",
+ " qp.setBrush(brush)\n",
+ " qp.drawRect(250, 15, 90, 60)\n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ "def main():\n",
+ " \n",
+ " app = QtGui.QApplication(sys.argv)\n",
+ " ex = Example()\n",
+ " sys.exit(app.exec_())\n",
+ "\n",
+ "\n",
+ "if __name__ == '__main__':\n",
+ " main()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Displaying a Bitmap, Page number: 605<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to display a image in a window.'''\n",
+ "\n",
+ "from tkinter import *\n",
+ "\n",
+ "\n",
+ "top = Tk()\n",
+ "top.title(\"Pen styles\")\n",
+ "C = Canvas(top, height=300, width=500)\n",
+ "filename = PhotoImage(file = \"C:/Users/Akshatha M/Desktop/dialog1.gif\")\n",
+ "image = C.create_image(50, 50, anchor=NE, image=filename)\n",
+ "C.pack()\n",
+ "top.mainloop()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Animation at Work, Page number: 608<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate a animation of a ball bouncing off a window '''\n",
+ "from visual import *\n",
+ "\n",
+ "floor = box(length=4, height=0.5, width=4, color=color.blue)\n",
+ "\n",
+ "ball = sphere(pos=(0,4,0), color=color.red)\n",
+ "ball.velocity = vector(0,-1,0)\n",
+ "\n",
+ "dt = 0.01\n",
+ "while 1:\n",
+ " rate(100)\n",
+ " ball.pos = ball.pos + ball.velocity*dt\n",
+ " if ball.y < 1:\n",
+ " ball.velocity.y = -ball.velocity.y\n",
+ " else:\n",
+ " ball.velocity.y = ball.velocity.y - 9.8*dt\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-2.ipynb b/Let_us_C/chapter-2.ipynb new file mode 100644 index 00000000..fc025445 --- /dev/null +++ b/Let_us_C/chapter-2.ipynb @@ -0,0 +1,571 @@ +{
+ "metadata": {
+ "name": "chapter-2.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 2: The Decision Control Structure <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>If Demo , Page number: 52<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Demonstration of if statement\n",
+ "Here is a simple program, which demonstrates the use of if and the relational operators'''\n",
+ "\n",
+ "#taking in input from the user\n",
+ "#num = raw_input(\"Enter a number less than 10: \")\n",
+ "print \"Enter a number less than 10: \"\n",
+ "num = 8\n",
+ "print num\n",
+ "\n",
+ "#if statement\n",
+ "if num <= 10:\n",
+ " print(\"What an obedient servant you are !\") #display result\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number less than 10: \n",
+ "8\n",
+ "What an obedient servant you are !\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.1 , Page number: 53<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000.\n",
+ "If quantity and price per item are input through the keyboard, write a program to calculate the total expenses.'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "dis = 0 #Initial Discount (%0)\n",
+ "\n",
+ "#Input from the user\n",
+ "#qty,rate = raw_input(\"Enter quantity and rate: \").split()\n",
+ "print \"Enter quantity and rate: \"\n",
+ "qty = 1200 # Quantity of item\n",
+ "rate = 15.50 # Rate of item (Rs)\n",
+ "print qty , rate\n",
+ "\n",
+ "#discount of 10% if quantity > 1000\n",
+ "if qty > 1000:\n",
+ " dis = 10\n",
+ "\n",
+ "#Calculation\n",
+ "tot = (qty * rate) - (qty * rate * dis / 100 ) # total expenses (Rs)\n",
+ "\n",
+ "#Result\n",
+ "print \"Total expenses = Rs. \", tot \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter quantity and rate: \n",
+ "1200 15.5\n",
+ "Total expenses = Rs. 16740.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.2, Page number: 57<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''The current year and the year in which the employee joined the organization are entered through the keyboard.\n",
+ "If the number of years for which the employee has served the organization is greater than 3 then a bonus of Rs. 2500/- is given to the employee.\n",
+ "If the years of service are not greater than 3, then the program should do nothing'''\n",
+ "\n",
+ "#input from user\n",
+ "#cy,yoj = raw_input(\"Enter current year and year of joining: \").split() \n",
+ "print \"Enter current year and year of joining: \"\n",
+ "cy = 2013 # Current year\n",
+ "yoj = 1990 # Year of joining\n",
+ "print cy, yoj \n",
+ "#Calculation\n",
+ "yr_of_ser = cy - yoj # number of years of service\n",
+ "\n",
+ "#Assign bonus if years of service > 3\n",
+ "if yr_of_ser > 3:\n",
+ " bonus = 2500 # Bonus of Rs. 2500\n",
+ " print \"Bonus = Rs.\", bonus #display result\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter current year and year of joining: \n",
+ "2013 1990\n",
+ "Bonus = Rs. 2500\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.3 , Page number: 58<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''In a company an employee is paid as under:\n",
+ "If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and\n",
+ "DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary.\n",
+ "If the employee's salary is input through the keyboard ,write a program to find his gross salary.'''\n",
+ "\n",
+ "#input from user\n",
+ "#bs = raw_input(\"Enter basic salary: \")\n",
+ "print \"Enter basic salary: \"\n",
+ "bs = 2561.1 #Basic salary (Rs)\n",
+ "print bs\n",
+ "\n",
+ "#Calculation\n",
+ "if bs < 1500: # if basic salary is less than Rs.1500\n",
+ " hra = bs * 10 / 100 # HRA (Rs)\n",
+ " da = bs * 90 / 100 #DA (Rs)\n",
+ "else: #if basic salary is greater than or equal to Rs.1500\n",
+ " hra = 500 # HRA (Rs)\n",
+ " da = bs * 98 / 100 # DA (Rs)\n",
+ "\n",
+ "gs = bs + hra + da # gross salary (Rs)\n",
+ "\n",
+ "#Result\n",
+ "print \"gross salary = Rs. \", gs \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter basic salary: \n",
+ "2561.1\n",
+ "gross salary = Rs. 5570.978\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Nested If-else , Page number: 61<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''A quick demo of nested if-else'''\n",
+ "\n",
+ "#input from user\n",
+ "#i = raw_input(\"Enter either 1 or 2: \")\n",
+ "print \"Enter either 1 or 2: \"\n",
+ "i = 1\n",
+ "print i\n",
+ "\n",
+ "#nested if-else\n",
+ "if i == 1 :\n",
+ " print \"You would go to heaven !\" \n",
+ "else:\n",
+ " if i == 2 :\n",
+ " print \"Hell was created with you in mind\" \n",
+ " else:\n",
+ " print \"How about mother earth !\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter either 1 or 2: \n",
+ "1\n",
+ "You would go to heaven !\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.4 (Method 1), Page number: 64<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules:\n",
+ "Percentage above or equal to 60 - First division\n",
+ "Percentage between 50 and 59 - Second division\n",
+ "Percentage between 40 and 49 - Third division\n",
+ "Percentage less than 40 - Fail\n",
+ "Write a program to calculate the division obtained by the student.\n",
+ "Method 1'''\n",
+ "\n",
+ "#input from user\n",
+ "#m1,m2,m3,m4,m5 = raw_input(\"Enter marks in five subjects: \").split() \n",
+ "print \"Enter marks in five subjects: \"\n",
+ "m1 = 88 #Marks in 1st subject\n",
+ "m2 = 92 #Marks in 2nd subject\n",
+ "m3 = 87 #Marks in 3rd subject\n",
+ "m4 = 66 #Marks in 4th subject\n",
+ "m5 = 56 #Marks in 5th subject\n",
+ "print m1,m2,m3,m4,m5\n",
+ "\n",
+ "#Calculation\n",
+ "per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n",
+ "\n",
+ "#check for different cases and display appropriate result\n",
+ "if per >= 60:\n",
+ " print \"First division\"\n",
+ "else:\n",
+ " if per >= 50:\n",
+ " print \"Second division\"\n",
+ " else:\n",
+ " if per >= 40:\n",
+ " print \"Third division\"\n",
+ " else:\n",
+ " print \"Fail\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter marks in five subjects: \n",
+ "88 92 87 66 56\n",
+ "First division\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.4 (Method 2), Page number: 65<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Method 2'''\n",
+ "\n",
+ "#input from user\n",
+ "#m1,m2,m3,m4,m5 = raw_input(\"Enter marks in five subjects: \").split() \n",
+ "print \"Enter marks in five subjects: \"\n",
+ "m1 = 88 #Marks in 1st subject\n",
+ "m2 = 92 #Marks in 2nd subject\n",
+ "m3 = 87 #Marks in 3rd subject\n",
+ "m4 = 66 #Marks in 4th subject\n",
+ "m5 = 56 #Marks in 5th subject\n",
+ "print m1,m2,m3,m4,m5\n",
+ "\n",
+ "#Calculation\n",
+ "per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n",
+ "\n",
+ "#check for different cases and display appropriate result\n",
+ "if per >= 60:\n",
+ " print \"First division\"\n",
+ "\n",
+ "if (per >= 50) and (per <60):\n",
+ " print\"Second division\"\n",
+ "\n",
+ "if (per >= 40) and (per <50):\n",
+ " print\"Third division\"\n",
+ "\n",
+ "if per < 40 :\n",
+ " print\"Fail\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks in five subjects: \n",
+ "88 92 87 66 56\n",
+ "First division\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.4 (Method 3), Page number: 67<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Method 3 - else if ladder demo'''\n",
+ "\n",
+ "#input from user\n",
+ "#m1,m2,m3,m4,m5 = raw_input(\"Enter marks in five subjects: \").split() \n",
+ "print \"Enter marks in five subjects: \"\n",
+ "m1 = 88 #Marks in 1st subject\n",
+ "m2 = 92 #Marks in 2nd subject\n",
+ "m3 = 87 #Marks in 3rd subject\n",
+ "m4 = 66 #Marks in 4th subject\n",
+ "m5 = 56 #Marks in 5th subject\n",
+ "print m1,m2,m3,m4,m5\n",
+ "\n",
+ "#Calculation\n",
+ "per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n",
+ "\n",
+ "#check for different cases and display appropriate result\n",
+ "if per >= 60:\n",
+ " print\"First division\"\n",
+ "elif per >= 50:\n",
+ " print\"Second division\"\n",
+ "elif per >= 40:\n",
+ " print\"Third division\"\n",
+ "else:\n",
+ " print\"Fail\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks in five subjects: \n",
+ "88 92 87 66 56\n",
+ "First division\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.5 (Method 1) , Page number: 68<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''A company insures its drivers in the following cases:\n",
+ "\u2212 If the driver is married.\n",
+ "\u2212 If the driver is unmarried, male & above 30 years of age.\n",
+ "\u2212 If the driver is unmarried, female & above 25 years of age.\n",
+ "In all other cases the driver is not insured.\n",
+ "If the marital status, sex and age of the driver are the inputs, write a program to determine whether the driver is to be insured or not.'''\n",
+ "\n",
+ "#input from user\n",
+ "#age,sex,ms = raw_input(\"Enter age, sex, marital status: \").split() # Age , sex and marital status of the driver\n",
+ "print \"Enter age, sex, marital status: \"\n",
+ "age = 43 # Age of driver (years)\n",
+ "sex = 'M'\n",
+ "ms = 'M'\n",
+ "print age,sex,ms\n",
+ "#check for different cases and display appropriate result\n",
+ "if ms == 'M':\n",
+ " print(\"Driver is insured\")\n",
+ "else:\n",
+ " if sex == 'M':\n",
+ " if age > 30:\n",
+ " print (\"Driver is insured\")\n",
+ " else:\n",
+ " print (\"Driver is not insured\")\n",
+ " else:\n",
+ " if age > 25:\n",
+ " print (\"Driver is insured\")\n",
+ " else:\n",
+ " print (\"Driver is not insured\")\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter age, sex, marital status: \n",
+ "43 M M\n",
+ "Driver is insured\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.5 (Method 2) , Page number: 69<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Using logical operators'''\n",
+ "\n",
+ "#input from user\n",
+ "#age,sex,ms = raw_input(\"Enter age, sex, marital status: \").split() # Age , sex and marital status of the driver\n",
+ "print \"Enter age, sex, marital status: \"\n",
+ "age = 43 # Age of driver (years)\n",
+ "sex = 'M'\n",
+ "ms = 'M'\n",
+ "print age,sex,ms\n",
+ "\n",
+ "#check for different cases and display appropriate result\n",
+ "if ((ms == 'M') or (ms == 'U' and sex == 'M' and age > 30) or (ms == 'U' and sex == 'F' and age >25) ) :\n",
+ " print\"Driver is insured\"\n",
+ "else:\n",
+ " print\"Driver is not insured\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter age, sex, marital status: \n",
+ "43 M M\n",
+ "Driver is insured\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.6, Page number: 71<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Write a program to calculate the salary as per the given table'''\n",
+ "\n",
+ "#Input gender( m/f), years of service and qualification from the user\n",
+ "#g,yos,qual = raw_input(\"Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):\").split()\n",
+ "print \"Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):\"\n",
+ "g = 'f'\n",
+ "yos = 8 # Years of service(years)\n",
+ "qual = 1 # Qualification ( 0=G, 1=PG)\n",
+ "print g,yos,qual\n",
+ "\n",
+ "# Assign salary depending upon the conditions\n",
+ "if (g == 'm') and (yos >= 10) and (qual == 1):\n",
+ " sal = 15000 #salary\n",
+ "elif ((g == 'm' and yos >= 10 and qual == 0) or ( g == 'm' and yos < 10 and qual == 1 )):\n",
+ " sal = 10000 #salary\n",
+ "elif ( g == 'm' and yos < 10 and qual == 0 ):\n",
+ " sal = 7000 #salary\n",
+ "elif ( g == 'f' and yos >= 10 and qual == 1 ):\n",
+ " sal = 12000 #salary\n",
+ "elif ( g == 'f' and yos >= 10 and qual == 0 ):\n",
+ " sal = 9000 #salary\n",
+ "elif ( g == 'f' and yos < 10 and qual == 1 ):\n",
+ " sal = 10000 #salary\n",
+ "elif ( g == 'f' and yos < 10 and qual == 0 ):\n",
+ " sal = 6000 #salary\n",
+ "\n",
+ "#Result\n",
+ "print \"Salary of Employee = \", sal "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):\n",
+ "f 8 1\n",
+ "Salary of Employee = 10000\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-20.ipynb b/Let_us_C/chapter-20.ipynb new file mode 100644 index 00000000..52b3cd37 --- /dev/null +++ b/Let_us_C/chapter-20.ipynb @@ -0,0 +1,181 @@ +{
+ "metadata": {
+ "name": "chapter-20.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 20: C Under Linux <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Fork , Page number: 655<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate how to create a child process'''\n",
+ "\n",
+ "import os\n",
+ "\n",
+ "print \"Before Forking\" \n",
+ "child = os.fork() #create a child process\n",
+ "print \"After Forking\\n\" \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Creating a Child Process , Page number: 656<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate how to create a child process'''\n",
+ "\n",
+ "import os\n",
+ "\n",
+ "pid = os.fork()\n",
+ "if pid == 0:\n",
+ " print \"In child process\" # code to play animated GIF file\n",
+ "else:\n",
+ " print \"In parent process\" #code to copy file \n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>PID of Parent And Child Processes , Page number: 657<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to get the pids of parent and child processes'''\n",
+ "\n",
+ "import os\n",
+ "from multiprocessing import Process\n",
+ "\n",
+ "if __name__ == '__main__':\n",
+ " ppid=os.getpid()\n",
+ " p = Process()\n",
+ " p.start()\n",
+ " cid = os.getpid()\n",
+ " \n",
+ "\n",
+ "if (cid):\n",
+ " print (\"Child : Hello I am the child process\")\n",
+ " print (\"Child : Child\u2019s PID: \", os.getpid( ) )\n",
+ " print (\"Child : Parent\u2019s PID: \", os.getppid( ) )\n",
+ "else:\n",
+ " print (\"Parent : Hello I am the parent process\" )\n",
+ " print (\"Parent : Parent\u2019s PID: \", os.getpid( ) )\n",
+ " print (\"Parent : Child\u2019s PID: \", cid )\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Execl , Page number: 659<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that uses execl( ) to run a new program in the child process.'''\n",
+ "\n",
+ "import os\n",
+ "\n",
+ "\n",
+ "pid = os.fork()\n",
+ "if pid == 0:\n",
+ " os.execl ( \"/bin/ls\",\"-al\", \"/etc\", NULL ) \n",
+ " print \"Child: After exec( )\"\n",
+ "else:\n",
+ " print \"Parent process\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": "*"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Waitpid , Page number: 662<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that gets the exit code\n",
+ "of the terminated process and thereby ensures a proper cleanup.'''\n",
+ "\n",
+ "import os\n",
+ "\n",
+ "i = 0 \n",
+ "pid = os.fork( ) \n",
+ "if ( pid == 0 ):\n",
+ " while ( i < 4294967295 ):\n",
+ " i=i+1\n",
+ " print \"The child is now terminating\" \n",
+ "else:\n",
+ " os.waitpid ( pid, status, 0 )\n",
+ " if ( os.WIFEXITED ( status ) ):\n",
+ " print \"Parent: Child terminated normally\" \n",
+ " else:\n",
+ " print \"Parent: Child terminated abnormally\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": "*"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-21.ipynb b/Let_us_C/chapter-21.ipynb new file mode 100644 index 00000000..7cf37612 --- /dev/null +++ b/Let_us_C/chapter-21.ipynb @@ -0,0 +1,226 @@ +{
+ "metadata": {
+ "name": "chapter-21.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 21: More Linux Programming <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>SIGINT Example , Page number: 669<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to prevent the termination of a program even after hitting Ctrl + C'''\n",
+ "\n",
+ "import signal, os\n",
+ "\n",
+ "def sighandler ( signum,arg ):\n",
+ " print ( \"SIGINT received. Inside sighandler\" ) \n",
+ "\n",
+ "\n",
+ "signal.signal(signal.SIGINT,sighandler)\n",
+ "while ( 1 ):\n",
+ " print \"Program Running\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Handling Multiple Signals , Page number: 671<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to handle multiple signals.'''\n",
+ "\n",
+ "import signal, os\n",
+ "\n",
+ "def inthandler ( signum,arg ):\n",
+ " print \"SIGINT Received\" \n",
+ " \n",
+ "def termhandler ( signum ,arg):\n",
+ " print \"SIGTERM Received\" \n",
+ " \n",
+ "def conthandler ( signum,arg ):\n",
+ " print \"SIGCONT Received\" \n",
+ " \n",
+ "\n",
+ "signal.signal(signal.SIGINT,inthandler)\n",
+ "signal.signal(signal.SIGTERM,termhandler)\n",
+ "signal.signal(signal.SIGCONT,conthandler)\n",
+ "\n",
+ "while ( 1 ):\n",
+ " print \"Program Running\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Registering A Common Handler , Page number: 673<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a common signal handler to handle all signals\n",
+ "instead of registering a separate handler for each signal'''\n",
+ "\n",
+ "import signal, os\n",
+ "\n",
+ "def sighandler ( signum ,arg):\n",
+ " if(signum == SIGINT):\n",
+ " print \"SIGINT Received\" \n",
+ " elif(signum == SIGTERM):\n",
+ " print \"SIGTERM Received\" \n",
+ " elif(signum == SIGCONT):\n",
+ " print \"SIGCONT Received\" \n",
+ "\n",
+ "signal.signal(signal.SIGINT,sighandler)\n",
+ "signal.signal(signal.SIGTERM,sighandler)\n",
+ "signal.signal(signal.SIGCONT,sighandler)\n",
+ "\n",
+ "while ( 1 ):\n",
+ " print \"Program running\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Blocking Signals , Page number: 675<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to understand signal blocking '''\n",
+ "\n",
+ "import signal, os\n",
+ "\n",
+ "def sighandler ( signum ,arg):\n",
+ " if(signum == SIGINT):\n",
+ " print (\"SIGINT Received\") \n",
+ " elif(signum == SIGTERM):\n",
+ " print (\"SIGTERM Received\" )\n",
+ " elif(signum == SIGCONT):\n",
+ " print ( \"SIGCONT Received\" )\n",
+ "\n",
+ "buffer = \"\\0\"\n",
+ "signal.signal(signal.SIGINT,sighandler)\n",
+ "signal.signal(signal.SIGTERM,sighandler)\n",
+ "signal.signal(signal.SIGCONT,sighandler)\n",
+ "\n",
+ "signal.pthread_sigmask(signal.SIG_BLOCK, [])\n",
+ "\n",
+ "while ( buffer == \"\\0\" ):\n",
+ " buffer = input(\"Enter a string\")\n",
+ " print (buffer)\n",
+ "signal.pthread_sigmask(signal.SIG_UNBLOCK, [])\n",
+ "while(1):\n",
+ " print(\"Program running\")\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Event Driven Programming , Page number: 678<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that uses GTK toolkit to create a window on the screen.'''\n",
+ "\n",
+ "from tkinter import *\n",
+ "\n",
+ "\n",
+ "class Example(Frame):\n",
+ " def __init__(self, parent):\n",
+ " Frame.__init__(self, parent)\n",
+ "\n",
+ " self.display = Canvas(self, width=700, height=200)\n",
+ " self.display.pack(side=\"top\", fill=\"both\", expand=True)\n",
+ " \n",
+ "if __name__ == \"__main__\":\n",
+ " root = Tk()\n",
+ " root.title(\"Sample Window\")\n",
+ " Frame = Example(parent=root)\n",
+ " Frame.pack(side=\"top\", fill=\"both\", expand=True)\n",
+ " root.mainloop()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>MyShapes , Page number: 681<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to draw a few shapes in a window'''\n",
+ "\n",
+ "from tkinter import *\n",
+ "\n",
+ "\n",
+ "top = Tk()\n",
+ "top.title(\"Sample Window\")\n",
+ "C = Canvas(top, height=500, width=500)\n",
+ "rcoor = 10,20,200,100\n",
+ "rect = C.create_rectangle(rcoor,fill=\"black\")#rectangle\n",
+ "picoor = 250,0,350,100\n",
+ "pie = C.create_arc(picoor, start=300, extent=100, fill=\"black\")#pie\n",
+ "pocoor = 250, 150, 250, 300, 300, 350, 400, 300, 320, 190\n",
+ "polygon = C.create_polygon(pocoor,fill=\"black\")#polygon\n",
+ "C.pack()\n",
+ "top.mainloop()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-3.ipynb b/Let_us_C/chapter-3.ipynb new file mode 100644 index 00000000..eae87c95 --- /dev/null +++ b/Let_us_C/chapter-3.ipynb @@ -0,0 +1,616 @@ +{
+ "metadata": {
+ "name": "chapter-3.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 3: The Loop Control Structure <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Simple Interest using While Loop, Page number: 99<h3>\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Calculation of simple interest for 3 sets of p, n and r (while loop)'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "count = 1\n",
+ "pr = [1000,2000,3500]\n",
+ "yr = [5,5,5]\n",
+ "intr = [13.5,13.5,3.5]\n",
+ "\n",
+ "# while loop\n",
+ "while count <= 3:\n",
+ " #Input from the user\n",
+ " #p,n,r = raw_input(\"Enter values of p, n and r : \").split()\n",
+ " p = pr[count-1] # principle\n",
+ " n = yr[count-1] # number of years\n",
+ " r = intr[count-1]# rate of interest\n",
+ "\n",
+ " #Calculation\n",
+ " si = p * n * r / 100 ; #formula for simple interest\n",
+ "\n",
+ " #Result\n",
+ " print \"Simple interest = Rs.\",si \n",
+ "\n",
+ " #Increment count\n",
+ " count = count + 1\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Simple interest = Rs. 675.0\n",
+ "Simple interest = Rs. 1350.0\n",
+ "Simple interest = Rs. 612.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Simple Interest using For Loop , Page number: 109<h3>\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Calculation of simple interest for 3 sets of p, n and r (for loop)'''\n",
+ "\n",
+ "pr = [1000,2000,3500]\n",
+ "yr = [5,5,5]\n",
+ "intr = [13.5,13.5,3.5]\n",
+ "\n",
+ "#for loop\n",
+ "for count in range(1, 4):\n",
+ " #Input from the user\n",
+ " #p,n,r = raw_input(\"Enter values of p, n and r : \").split()\n",
+ " p = pr[count-1] # principle\n",
+ " n = yr[count-1] # number of years\n",
+ " r = intr[count-1]# rate of interest\n",
+ " \n",
+ " #Calculation\n",
+ " si = p * n * r / 100 ; #formula for simple interest\n",
+ "\n",
+ " #Result\n",
+ " print \"Simple interest = Rs.\",si "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Simple interest = Rs. 675.0\n",
+ "Simple interest = Rs. 1350.0\n",
+ "Simple interest = Rs. 612.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Nested For Loops , Page number: 114<h3>\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Demonstration of nested loops'''\n",
+ "\n",
+ "#nested for loops\n",
+ "for r in range(1,4): #outer loop\n",
+ " for c in range(1,3): #inner loop\n",
+ " s = r + c #find the sum\n",
+ " print \"r = %d c = %d sum = %d\" % (r, c, s) #Display result\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "r = 1 c = 1 sum = 2\n",
+ "r = 1 c = 2 sum = 3\n",
+ "r = 2 c = 1 sum = 3\n",
+ "r = 2 c = 2 sum = 4\n",
+ "r = 3 c = 1 sum = 4\n",
+ "r = 3 c = 2 sum = 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Do While Loop , Page number: 116<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Execution of a loop an unknown number of times'''\n",
+ "\n",
+ "#do while loop\n",
+ "while True:\n",
+ " #num = raw_input(\"Enter a number: \")\n",
+ " num = 11\n",
+ " print \"square of %d is %d\"%(num, num * num )\n",
+ " print \"Want to enter another number y/n: \" \n",
+ " another = 'n'\n",
+ " print another\n",
+ " if another == 'y':\n",
+ " continue\n",
+ " else:\n",
+ " break\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "square of 11 is 121\n",
+ "Want to enter another number y/n: \n",
+ "n\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Do While using For Loop, Page number: 117<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''odd loop using a for loop'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "another = 'y'\n",
+ "\n",
+ "#do while loop\n",
+ "import sys\n",
+ "for i in range(1,10000): #infinte loop\n",
+ " #num = raw_input(\"Enter a number: \")\n",
+ " num = 11\n",
+ " print \"square of %d is %d\"%(num, num * num )\n",
+ " print \"Want to enter another number y/n: \" \n",
+ " another = 'n'\n",
+ " print another\n",
+ " if another == 'y':\n",
+ " continue\n",
+ " else:\n",
+ " break\n",
+ " \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "square of 11 is 121\n",
+ "Want to enter another number y/n: \n",
+ "n\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Do While using While Loop, Page number: 117<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''odd loop using a while loop'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "another = 'y'\n",
+ "\n",
+ "#do while loop\n",
+ "while another == 'y':\n",
+ " #num = raw_input(\"Enter a number: \")\n",
+ " num = 11\n",
+ " print \"square of %d is %d\"%(num, num * num )\n",
+ " print \"Want to enter another number y/n: \" \n",
+ " another = 'n'\n",
+ " print another\n",
+ " \n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "square of 11 is 121\n",
+ "Want to enter another number y/n: \n",
+ "n\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Prime Number, Page number: 118<h3>\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Write a program to determine whether a number is prime or not.\n",
+ "A prime number is one, which is divisible only by 1 or itself.'''\n",
+ "\n",
+ "#Input from user\n",
+ "#num = raw_input(\"Enter a number: \")\n",
+ "num = 11\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 2\n",
+ "\n",
+ "#while loop\n",
+ "while i <=(num - 1):\n",
+ " if num % i == 0:\n",
+ " print \"Not a prime number\" #Display if not prime number\n",
+ " break\n",
+ " i += 1\n",
+ "\n",
+ "#Display if prime number\n",
+ "if i == num:\n",
+ " print \"Prime number\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Prime number\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Break Statement , Page number: 119<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''The following program illustrates the fact that the keyword break,\n",
+ "breaks the control only from the loop in which it is placed.'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 1\n",
+ "j = 1\n",
+ "\n",
+ "#while loops\n",
+ "while i <= 100 : #outer loop\n",
+ " i = i+1\n",
+ " while j <= 200 : #inner loop\n",
+ " j = j+1\n",
+ " if j == 150:\n",
+ " break #break statement in inner loop\n",
+ " else:\n",
+ " print i, j \n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2 2\n",
+ "2 3\n",
+ "2 4\n",
+ "2 5\n",
+ "2 6\n",
+ "2 7\n",
+ "2 8\n",
+ "2 9\n",
+ "2 10\n",
+ "2 11\n",
+ "2 12\n",
+ "2 13\n",
+ "2 14\n",
+ "2 15\n",
+ "2 16\n",
+ "2 17\n",
+ "2 18\n",
+ "2 19\n",
+ "2 20\n",
+ "2 21\n",
+ "2 22\n",
+ "2 23\n",
+ "2 24\n",
+ "2 25\n",
+ "2 26\n",
+ "2 27\n",
+ "2 28\n",
+ "2 29\n",
+ "2 30\n",
+ "2 31\n",
+ "2 32\n",
+ "2 33\n",
+ "2 34\n",
+ "2 35\n",
+ "2 36\n",
+ "2 37\n",
+ "2 38\n",
+ "2 39\n",
+ "2 40\n",
+ "2 41\n",
+ "2 42\n",
+ "2 43\n",
+ "2 44\n",
+ "2 45\n",
+ "2 46\n",
+ "2 47\n",
+ "2 48\n",
+ "2 49\n",
+ "2 50\n",
+ "2 51\n",
+ "2 52\n",
+ "2 53\n",
+ "2 54\n",
+ "2 55\n",
+ "2 56\n",
+ "2 57\n",
+ "2 58\n",
+ "2 59\n",
+ "2 60\n",
+ "2 61\n",
+ "2 62\n",
+ "2 63\n",
+ "2 64\n",
+ "2 65\n",
+ "2 66\n",
+ "2 67\n",
+ "2 68\n",
+ "2 69\n",
+ "2 70\n",
+ "2 71\n",
+ "2 72\n",
+ "2 73\n",
+ "2 74\n",
+ "2 75\n",
+ "2 76\n",
+ "2 77\n",
+ "2 78\n",
+ "2 79\n",
+ "2 80\n",
+ "2 81\n",
+ "2 82\n",
+ "2 83\n",
+ "2 84\n",
+ "2 85\n",
+ "2 86\n",
+ "2 87\n",
+ "2 88\n",
+ "2 89\n",
+ "2 90\n",
+ "2 91\n",
+ "2 92\n",
+ "2 93\n",
+ "2 94\n",
+ "2 95\n",
+ "2 96\n",
+ "2 97\n",
+ "2 98\n",
+ "2 99\n",
+ "2 100\n",
+ "2 101\n",
+ "2 102\n",
+ "2 103\n",
+ "2 104\n",
+ "2 105\n",
+ "2 106\n",
+ "2 107\n",
+ "2 108\n",
+ "2 109\n",
+ "2 110\n",
+ "2 111\n",
+ "2 112\n",
+ "2 113\n",
+ "2 114\n",
+ "2 115\n",
+ "2 116\n",
+ "2 117\n",
+ "2 118\n",
+ "2 119\n",
+ "2 120\n",
+ "2 121\n",
+ "2 122\n",
+ "2 123\n",
+ "2 124\n",
+ "2 125\n",
+ "2 126\n",
+ "2 127\n",
+ "2 128\n",
+ "2 129\n",
+ "2 130\n",
+ "2 131\n",
+ "2 132\n",
+ "2 133\n",
+ "2 134\n",
+ "2 135\n",
+ "2 136\n",
+ "2 137\n",
+ "2 138\n",
+ "2 139\n",
+ "2 140\n",
+ "2 141\n",
+ "2 142\n",
+ "2 143\n",
+ "2 144\n",
+ "2 145\n",
+ "2 146\n",
+ "2 147\n",
+ "2 148\n",
+ "2 149\n",
+ "3 151\n",
+ "3 152\n",
+ "3 153\n",
+ "3 154\n",
+ "3 155\n",
+ "3 156\n",
+ "3 157\n",
+ "3 158\n",
+ "3 159\n",
+ "3 160\n",
+ "3 161\n",
+ "3 162\n",
+ "3 163\n",
+ "3 164\n",
+ "3 165\n",
+ "3 166\n",
+ "3 167\n",
+ "3 168\n",
+ "3 169\n",
+ "3 170\n",
+ "3 171\n",
+ "3 172\n",
+ "3 173\n",
+ "3 174\n",
+ "3 175\n",
+ "3 176\n",
+ "3 177\n",
+ "3 178\n",
+ "3 179\n",
+ "3 180\n",
+ "3 181\n",
+ "3 182\n",
+ "3 183\n",
+ "3 184\n",
+ "3 185\n",
+ "3 186\n",
+ "3 187\n",
+ "3 188\n",
+ "3 189\n",
+ "3 190\n",
+ "3 191\n",
+ "3 192\n",
+ "3 193\n",
+ "3 194\n",
+ "3 195\n",
+ "3 196\n",
+ "3 197\n",
+ "3 198\n",
+ "3 199\n",
+ "3 200\n",
+ "3 201\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Continue Statement , Page number: 120<h3>\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''The following program illustrates the use of the 'continue' statement'''\n",
+ "\n",
+ "#for loops\n",
+ "for i in range(1,3):\n",
+ " for j in range(1,3):\n",
+ " if i==j :\n",
+ " continue # continue statement\n",
+ " print i , j\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 2\n",
+ "2 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-4.ipynb b/Let_us_C/chapter-4.ipynb new file mode 100644 index 00000000..d6b348c1 --- /dev/null +++ b/Let_us_C/chapter-4.ipynb @@ -0,0 +1,361 @@ +{
+ "metadata": {
+ "name": "chapter-4.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 4: The Case Control Structure<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Switch - Case, Page number: 137<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate switch-case control structure(without break)'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 2\n",
+ "\n",
+ "#Switch case statements\n",
+ "if i == 1: # case 1\n",
+ " print \"I am in case 1\"\n",
+ "else:\n",
+ " print \"I am in case 2\"# case 2\n",
+ " print \"I am in case 3\"#case 3\n",
+ " print \"I am in default\"# default\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I am in case 2\n",
+ "I am in case 3\n",
+ "I am in default\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Switch - Case, Page number: 138<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate switch-case control structure'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 2\n",
+ "\n",
+ "#Switch case statements\n",
+ "if i == 1: # case 1\n",
+ " print \"I am in case 1\"\n",
+ "elif i == 2: # case 2\n",
+ " print \"I am in case 2\"\n",
+ "elif i == 3: #case 3\n",
+ " print \"I am in case 3\"\n",
+ "else: # default\n",
+ " print \"I am in default\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I am in case 2\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>The Tips and Traps a), Page number: 140<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''An example of scrambled case order'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 22\n",
+ "\n",
+ "#Switch case statements\n",
+ "if i == 121: # case 121\n",
+ " print \"I am in case 121\"\n",
+ "elif i == 7: # case 7\n",
+ " print \"I am in case 7\"\n",
+ "elif i == 22: #case 22\n",
+ " print \"I am in case 22\"\n",
+ "else: # default\n",
+ " print \"I am in default\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I am in case 22\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>The Tips and Traps b), Page number: 140<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''An example which shows the use of 'char' values in case and switch'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "c = 'x'\n",
+ "\n",
+ "#Switch case statements\n",
+ "if c == 'v': # case 'v'\n",
+ " print \"I am in case v\"\n",
+ "elif c == 'a': # case 'a'\n",
+ " print \"I am in case a\"\n",
+ "elif c == 'x': #case 'x'\n",
+ " print \"I am in case x\"\n",
+ "else: # default\n",
+ " print \"I am in default\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I am in case x\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>The Tips and Traps c), Page number: 141<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''An example which shows how to execute a common set of statements for\n",
+ "multiple cases.'''\n",
+ "\n",
+ "#Input from user\n",
+ "#ch = raw_input(\"Enter any of the alphabet a, b, or c : \")\n",
+ "ch = 'a'\n",
+ "\n",
+ "#Switch case statements\n",
+ "if ch == 'a' or ch == 'A' : # case 'a' and case 'A'\n",
+ " print \"a as in ashar\"\n",
+ "elif ch == 'b'or ch == 'B': # case 'b' and case 'B'\n",
+ " print \"b as in brain\"\n",
+ "elif ch == 'c'or ch == 'C': # case 'c' and case 'C'\n",
+ " print \"c as in cookie\"\n",
+ "else: # default\n",
+ " print (\"wish you knew what are alphabets\")\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "a as in ashar\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>The Tips and Traps e) , Page number: 143<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''An example that shows every statement in a switch must belong to some \n",
+ "case or the other. If it doesn\u2019t,the compiler won\u2019t report an error.\n",
+ "However, the statement would never get executed.'''\n",
+ "\n",
+ "#Input from user\n",
+ "#i = raw_input(\"Enter value of i \")\n",
+ "i = 1\n",
+ "\n",
+ "#Switch case statements\n",
+ "#print \"Hello\"\n",
+ "if i == 1 : # case 1\n",
+ " j = 10\n",
+ "elif i == 2 :# case 2\n",
+ " j = 20\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Goto , Page number: 146<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program shows how to use goto statement'''\n",
+ "'''Python doesnt support statements like 'goto' '''\n",
+ "#Input from user\n",
+ "#goals = raw_input(\"Enter the number of goals scored against India: \")\n",
+ "goals = 3\n",
+ "\n",
+ "if goals <= 5 : #goto\n",
+ " print \"To err is human!\" #label sos\n",
+ "else:\n",
+ " print \"About time soccer players learnt C\" \n",
+ " print \"and said goodbye! adieu! to soccer\" \n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "To err is human!\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Goto , Page number: 148<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''The following program illustrates the usage of 'goto'\n",
+ "when we want to take the control out of the loop that is contained in\n",
+ "several other loops.'''\n",
+ "\n",
+ "\n",
+ "#nested for loops\n",
+ "for i in range(1,4):\n",
+ " for j in range(1,4):\n",
+ " for k in range(1,4):\n",
+ " if ( i == 3 and j == 3 and k == 3 ): #goto\n",
+ " print \"Out of the loop at last!\" # label out\n",
+ " break\n",
+ " else:\n",
+ " print i, j, k "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 1 1\n",
+ "1 1 2\n",
+ "1 1 3\n",
+ "1 2 1\n",
+ "1 2 2\n",
+ "1 2 3\n",
+ "1 3 1\n",
+ "1 3 2\n",
+ "1 3 3\n",
+ "2 1 1\n",
+ "2 1 2\n",
+ "2 1 3\n",
+ "2 2 1\n",
+ "2 2 2\n",
+ "2 2 3\n",
+ "2 3 1\n",
+ "2 3 2\n",
+ "2 3 3\n",
+ "3 1 1\n",
+ "3 1 2\n",
+ "3 1 3\n",
+ "3 2 1\n",
+ "3 2 2\n",
+ "3 2 3\n",
+ "3 3 1\n",
+ "3 3 2\n",
+ "Out of the loop at last!\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-5.ipynb b/Let_us_C/chapter-5.ipynb new file mode 100644 index 00000000..62f0970a --- /dev/null +++ b/Let_us_C/chapter-5.ipynb @@ -0,0 +1,804 @@ +{
+ "metadata": {
+ "name": "chapter-5.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 5: Functions & Pointers <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Message Function, Page number: 159<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate a simple function'''\n",
+ "\n",
+ "# function definition\n",
+ "def message():\n",
+ " print \"Smile, and the world smiles with you...\" \n",
+ "\n",
+ "message() #function call\n",
+ "print \"Cry, and you stop the monotony!\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Smile, and the world smiles with you...\n",
+ "Cry, and you stop the monotony!\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Calling More Than One Funnction , Page number: 159<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate a call to more than one function'''\n",
+ "\n",
+ "# function definitions\n",
+ "def italy():\n",
+ " print \"I am in italy\" \n",
+ " \n",
+ "def brazil():\n",
+ " print \"I am in brazil\" \n",
+ "\n",
+ "def argentina():\n",
+ " print \"I am in argentina\" \n",
+ " \n",
+ "\n",
+ "print \"I am in main\" \n",
+ "#function calls\n",
+ "italy()\n",
+ "brazil()\n",
+ "argentina()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I am in main\n",
+ "I am in italy\n",
+ "I am in brazil\n",
+ "I am in argentina\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Function Calling Function, Page number: 161<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate a call to more than one function'''\n",
+ "\n",
+ "# function definitions\n",
+ "def argentina():\n",
+ " print \"I am in argentina\" \n",
+ "\n",
+ "def brazil():\n",
+ " print \"I am in brazil\" \n",
+ " argentina() #function call\n",
+ "\n",
+ "def italy():\n",
+ " print \"I am in italy\" \n",
+ " brazil() #function call\n",
+ " print \"I am back in italy\" \n",
+ " \n",
+ "print \"I am in main\" \n",
+ "#function call\n",
+ "italy()\n",
+ "print \"I am finally back in main\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I am in main\n",
+ "I am in italy\n",
+ "I am in brazil\n",
+ "I am in argentina\n",
+ "I am back in italy\n",
+ "I am finally back in main\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Calsum Function , Page number: 166<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate sending and receiving values between functions'''\n",
+ "\n",
+ "# function definition\n",
+ "def calsum ( x, y, z ):\n",
+ " d = x + y + z\n",
+ " return d\n",
+ "\n",
+ "#Input from user\n",
+ "#a,b,c = raw_input (\"Enter any three numbers: \").split()\n",
+ "print \"Enter any three numbers: \"\n",
+ "a = 10\n",
+ "b = 20\n",
+ "c = 30\n",
+ "print a,b,c\n",
+ "\n",
+ "#function call\n",
+ "s = calsum(a,b,c) \n",
+ "\n",
+ "#Result\n",
+ "print \"Sum = \", s \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any three numbers: \n",
+ "10 20 30\n",
+ "Sum = 60\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Return Statement , Page number: 169<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate that there is no restriction on the number of return\n",
+ "statements that may be present in a function.\n",
+ "Also, the return statement need not always be present at the end of the called\n",
+ "function.'''\n",
+ "\n",
+ "# function definition\n",
+ "def fun():\n",
+ " #Input from user\n",
+ " #ch = raw_input (\"Enter any alphabet: \")\n",
+ " print \"Enter any alphabet: \"\n",
+ " ch = 'a'\n",
+ " print ch\n",
+ " ch = ord(ch)\n",
+ " if ch >= 65 and ch <= 90:\n",
+ " return ascii(ch)\n",
+ " else:\n",
+ " return ascii( ch + 32 )\n",
+ "\n",
+ "#function call\n",
+ "ch = fun()\n",
+ "\n",
+ "#Result\n",
+ "print ch\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Formal Arguments, Page number: 170<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate that If the value of a formal argument is\n",
+ "changed in the called function, the corresponding\n",
+ "change does not take place in the calling function.'''\n",
+ "\n",
+ "# function definition\n",
+ "def fun(b):\n",
+ " b = 60\n",
+ " print b \n",
+ "\n",
+ "a = 30\n",
+ "fun(a) #function call\n",
+ "print a"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "60\n",
+ "30\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Scope Rule of Functions , Page number: 171<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate Scope Rule of Functions'''\n",
+ "\n",
+ "# function definition\n",
+ "def display(j):\n",
+ " k = 35\n",
+ " print j\n",
+ " print k\n",
+ "\n",
+ "i = 20 \n",
+ "display(i) #function call\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "20\n",
+ "35\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Square Function , Page number: 176<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to find out square of a number using a function.'''\n",
+ "\n",
+ "# function definition\n",
+ "def square(x):\n",
+ " y = x * x\n",
+ " return y\n",
+ "\n",
+ "#Input from user\n",
+ "#a = raw_input(\"Enter any number: \")\n",
+ "print \"Enter any number: \"\n",
+ "a = 4.5\n",
+ "print a\n",
+ "\n",
+ "b = square(a) #function call\n",
+ "\n",
+ "#Result\n",
+ "print \"Square of %f is %f\" %( a, b )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number: \n",
+ "4.5\n",
+ "Square of 4.500000 is 20.250000\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Void Function , Page number: 177<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a called function without returning any value using\n",
+ "the keyword void.'''\n",
+ "\n",
+ "# function definition\n",
+ "def gospel(): # void function\n",
+ " print \"Viruses are electronic bandits...\" \n",
+ " print \"who eat nuggets of information...\" \n",
+ " print \"and chunks of bytes...\" \n",
+ " print \"when you least expect...\" \n",
+ "\n",
+ "gospel() # function call"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Viruses are electronic bandits...\n",
+ "who eat nuggets of information...\n",
+ "and chunks of bytes...\n",
+ "when you least expect...\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Address of a Variable , Page number: 180<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to print the address of a variable'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 3\n",
+ "\n",
+ "#Result\n",
+ "print \"Address of i = \" , id(i) #printing address\n",
+ "print \"Value of i = \", i \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of i = 30301288\n",
+ "Value of i = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Pointer Relations , Page number: 182<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that demonstrates the relationships between pointers , variables\n",
+ "and addresses'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 3 \n",
+ "j = id(i) # address of variable 'i'\n",
+ "\n",
+ "#Result\n",
+ "print \"Address of i = \", id(i) \n",
+ "print \"Address of i = \", j \n",
+ "print \"Address of j = \", id(j)\n",
+ "print \"Value of j = \", j \n",
+ "print \"Value of i = \", i \n",
+ "print \"Value of i = \", i \n",
+ "print \"Value of i = \", i \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of i = 30301288\n",
+ "Address of i = 30301288\n",
+ "Address of j = 134133200\n",
+ "Value of j = 30301288\n",
+ "Value of i = 3\n",
+ "Value of i = 3\n",
+ "Value of i = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Pointer to Another Pointer , Page number: 184<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that demonstrates a pointer that\n",
+ "points to another pointer'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 3\n",
+ "j = id(i) # address of i\n",
+ "k = id(j) # address of j\n",
+ "\n",
+ "#Result\n",
+ "print \"Address of i = \", id(i) \n",
+ "print \"Address of i = \", j \n",
+ "print \"Address of i = \", j \n",
+ "print \"Address of j = \", id(j) \n",
+ "print \"Address of j = \", k \n",
+ "print \"Address of k = \", id(k)\n",
+ "print \"Value of j = \", j \n",
+ "print \"Value of k = \", k \n",
+ "print \"Value of i = \", i \n",
+ "print \"Value of i = \", i \n",
+ "print \"Value of i = \", i \n",
+ "print \"Value of i = \", i "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of i = 30301288\n",
+ "Address of i = 30301288\n",
+ "Address of i = 30301288\n",
+ "Address of j = 134132944\n",
+ "Address of j = 134132944\n",
+ "Address of k = 134133200\n",
+ "Value of j = 30301288\n",
+ "Value of k = 134132944\n",
+ "Value of i = 3\n",
+ "Value of i = 3\n",
+ "Value of i = 3\n",
+ "Value of i = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Call By Value , Page number: 186<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the \u2018Call by Value\u2019 method of passing arguments'''\n",
+ "\n",
+ "# function definition\n",
+ "def swapv (x,y):\n",
+ " x,y=y,x\n",
+ " print \"x = %d y = %d\" %( x, y )\n",
+ "\n",
+ "#Variable declaration\n",
+ "a = 10\n",
+ "b = 20\n",
+ "\n",
+ "swapv ( a, b ) # function call\n",
+ "\n",
+ "#Result\n",
+ "print \"a = %d b = %d\" %( a, b )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 20 y = 10\n",
+ "a = 10 b = 20\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Call By Reference , Page number: 187<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the \u2018Call by Reference\u2019 method of passing arguments'''\n",
+ "\n",
+ "# function definition\n",
+ "def swapv (a,b):\n",
+ " a,b=b,a\n",
+ " return a,b\n",
+ "\n",
+ "#Variable declaration\n",
+ "a = 10\n",
+ "b = 20\n",
+ "\n",
+ "a,b = swapv ( a, b ) # function call\n",
+ "\n",
+ "#Result\n",
+ "print \"a = %d b = %d\" %( a, b )"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "a = 20 b = 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Area And Perimeter of a Circle , Page number: 188<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate how to use a call by reference to make a function\n",
+ "return more than one value at a time, which is not possible ordinarily.'''\n",
+ "\n",
+ "#Input from user\n",
+ "#radius = raw_input(\"Enter radius of a circle: \" )\n",
+ "print \"Enter radius of a circle: \"\n",
+ "radius = 5\n",
+ "print radius\n",
+ "\n",
+ "#Function definition\n",
+ "def areaperi ( r ):\n",
+ " a = 3.14 * r * r\n",
+ " p = 2 * 3.14 * r \n",
+ " return a,p\n",
+ "\n",
+ "area,perimeter = areaperi ( radius ) #function call\n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "print \"Area = \", area \n",
+ "print \"Perimeter = \", perimeter "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter radius of a circle: \n",
+ "5\n",
+ "Area = 78.5\n",
+ "Perimeter = 31.4\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Iterative Factorial Function , Page number: 190<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a non-recursive function for calculating\n",
+ "the factorial value of an integer'''\n",
+ "\n",
+ "# function definition\n",
+ "def factorial(x):\n",
+ " f = 1\n",
+ " for i in range(x,0,-1 ):\n",
+ " f = f * i\n",
+ " return f\n",
+ "\n",
+ "#Input from user\n",
+ "#a = raw_input( \"Enter any number: \" )\n",
+ "print \"Enter any number: \"\n",
+ "a = 6\n",
+ "print a\n",
+ "\n",
+ "fact = factorial(a) #function call\n",
+ "\n",
+ "#Result\n",
+ "print \"Factorial value = \", fact\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number: \n",
+ "6\n",
+ "Factorial value = 720\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Recursive Factorial Function, Page number: 191<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a the recursive version of\n",
+ "the function to calculate the factorial value.'''\n",
+ "\n",
+ "# function definition\n",
+ "def rec(x):\n",
+ " if x == 1:\n",
+ " return 1\n",
+ " else:\n",
+ " f = x * rec ( x - 1 ) # calling the function\n",
+ " return f\n",
+ "\n",
+ "#Input from user\n",
+ "#a = raw_input( \"Enter any number: \" )\n",
+ "print \"Enter any number: \"\n",
+ "a = 5\n",
+ "print a\n",
+ "\n",
+ "fact = rec(a) #function call\n",
+ "\n",
+ "#Result\n",
+ "print \"Factorial value = \", fact \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number: \n",
+ "5\n",
+ "Factorial value = 120\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Recursion Stack , Page number: 195<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the use of a stack for\n",
+ "implementing function calls'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "a = 5\n",
+ "b = 2\n",
+ "\n",
+ "#Function definition\n",
+ "def add ( i, j ):\n",
+ " s = i + j\n",
+ " return s\n",
+ "\n",
+ "c = add ( a, b ) # function call\n",
+ "\n",
+ "#Result\n",
+ "print \"sum = \", c "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "sum = 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-6.ipynb b/Let_us_C/chapter-6.ipynb new file mode 100644 index 00000000..7cf66240 --- /dev/null +++ b/Let_us_C/chapter-6.ipynb @@ -0,0 +1,683 @@ +{
+ "metadata": {
+ "name": "chapter-6.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 6: Data Types Revisted <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>char Example, Page number: 218<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the range of char'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "ch = 291\n",
+ "\n",
+ "#Result\n",
+ "print \"%d %c\" %( ch, (ch%128) ) \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "291 #\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Range of char, Page number: 218<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the range of an unsigned char'''\n",
+ "\n",
+ "for ch in range(0,256):\n",
+ " print \"%d %c\" %(ch, ch%128 )\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0 \u0000\n",
+ "1 \u0001\n",
+ "2 \u0002\n",
+ "3 \u0003\n",
+ "4 \u0004\n",
+ "5 \u0005\n",
+ "6 \u0006\n",
+ "7 \u0007\n",
+ "8 \b\n",
+ "9 \t\n",
+ "10 \n",
+ "\n",
+ "11 \u000b",
+ "\n",
+ "12 \f",
+ "\n",
+ "13 \r\n",
+ "14 \u000e\n",
+ "15 \u000f\n",
+ "16 \u0010\n",
+ "17 \u0011\n",
+ "18 \u0012\n",
+ "19 \u0013\n",
+ "20 \u0014\n",
+ "21 \u0015\n",
+ "22 \u0016\n",
+ "23 \u0017\n",
+ "24 \u0018\n",
+ "25 \u0019\n",
+ "26 \u001a\n",
+ "27 \u001b\n",
+ "28 \u001c",
+ "\n",
+ "29 \u001d",
+ "\n",
+ "30 \u001e",
+ "\n",
+ "31 \u001f\n",
+ "32 \n",
+ "33 !\n",
+ "34 \"\n",
+ "35 #\n",
+ "36 $\n",
+ "37 %\n",
+ "38 &\n",
+ "39 '\n",
+ "40 (\n",
+ "41 )\n",
+ "42 *\n",
+ "43 +\n",
+ "44 ,\n",
+ "45 -\n",
+ "46 .\n",
+ "47 /\n",
+ "48 0\n",
+ "49 1\n",
+ "50 2\n",
+ "51 3\n",
+ "52 4\n",
+ "53 5\n",
+ "54 6\n",
+ "55 7\n",
+ "56 8\n",
+ "57 9\n",
+ "58 :\n",
+ "59 ;\n",
+ "60 <\n",
+ "61 =\n",
+ "62 >\n",
+ "63 ?\n",
+ "64 @\n",
+ "65 A\n",
+ "66 B\n",
+ "67 C\n",
+ "68 D\n",
+ "69 E\n",
+ "70 F\n",
+ "71 G\n",
+ "72 H\n",
+ "73 I\n",
+ "74 J\n",
+ "75 K\n",
+ "76 L\n",
+ "77 M\n",
+ "78 N\n",
+ "79 O\n",
+ "80 P\n",
+ "81 Q\n",
+ "82 R\n",
+ "83 S\n",
+ "84 T\n",
+ "85 U\n",
+ "86 V\n",
+ "87 W\n",
+ "88 X\n",
+ "89 Y\n",
+ "90 Z\n",
+ "91 [\n",
+ "92 \\\n",
+ "93 ]\n",
+ "94 ^\n",
+ "95 _\n",
+ "96 `\n",
+ "97 a\n",
+ "98 b\n",
+ "99 c\n",
+ "100 d\n",
+ "101 e\n",
+ "102 f\n",
+ "103 g\n",
+ "104 h\n",
+ "105 i\n",
+ "106 j\n",
+ "107 k\n",
+ "108 l\n",
+ "109 m\n",
+ "110 n\n",
+ "111 o\n",
+ "112 p\n",
+ "113 q\n",
+ "114 r\n",
+ "115 s\n",
+ "116 t\n",
+ "117 u\n",
+ "118 v\n",
+ "119 w\n",
+ "120 x\n",
+ "121 y\n",
+ "122 z\n",
+ "123 {\n",
+ "124 |\n",
+ "125 }\n",
+ "126 ~\n",
+ "127 \u007f\n",
+ "128 \u0000\n",
+ "129 \u0001\n",
+ "130 \u0002\n",
+ "131 \u0003\n",
+ "132 \u0004\n",
+ "133 \u0005\n",
+ "134 \u0006\n",
+ "135 \u0007\n",
+ "136 \b\n",
+ "137 \t\n",
+ "138 \n",
+ "\n",
+ "139 \u000b",
+ "\n",
+ "140 \f",
+ "\n",
+ "141 \r\n",
+ "142 \u000e\n",
+ "143 \u000f\n",
+ "144 \u0010\n",
+ "145 \u0011\n",
+ "146 \u0012\n",
+ "147 \u0013\n",
+ "148 \u0014\n",
+ "149 \u0015\n",
+ "150 \u0016\n",
+ "151 \u0017\n",
+ "152 \u0018\n",
+ "153 \u0019\n",
+ "154 \u001a\n",
+ "155 \u001b\n",
+ "156 \u001c",
+ "\n",
+ "157 \u001d",
+ "\n",
+ "158 \u001e",
+ "\n",
+ "159 \u001f\n",
+ "160 \n",
+ "161 !\n",
+ "162 \"\n",
+ "163 #\n",
+ "164 $\n",
+ "165 %\n",
+ "166 &\n",
+ "167 '\n",
+ "168 (\n",
+ "169 )\n",
+ "170 *\n",
+ "171 +\n",
+ "172 ,\n",
+ "173 -\n",
+ "174 .\n",
+ "175 /\n",
+ "176 0\n",
+ "177 1\n",
+ "178 2\n",
+ "179 3\n",
+ "180 4\n",
+ "181 5\n",
+ "182 6\n",
+ "183 7\n",
+ "184 8\n",
+ "185 9\n",
+ "186 :\n",
+ "187 ;\n",
+ "188 <\n",
+ "189 =\n",
+ "190 >\n",
+ "191 ?\n",
+ "192 @\n",
+ "193 A\n",
+ "194 B\n",
+ "195 C\n",
+ "196 D\n",
+ "197 E\n",
+ "198 F\n",
+ "199 G\n",
+ "200 H\n",
+ "201 I\n",
+ "202 J\n",
+ "203 K\n",
+ "204 L\n",
+ "205 M\n",
+ "206 N\n",
+ "207 O\n",
+ "208 P\n",
+ "209 Q\n",
+ "210 R\n",
+ "211 S\n",
+ "212 T\n",
+ "213 U\n",
+ "214 V\n",
+ "215 W\n",
+ "216 X\n",
+ "217 Y\n",
+ "218 Z\n",
+ "219 [\n",
+ "220 \\\n",
+ "221 ]\n",
+ "222 ^\n",
+ "223 _\n",
+ "224 `\n",
+ "225 a\n",
+ "226 b\n",
+ "227 c\n",
+ "228 d\n",
+ "229 e\n",
+ "230 f\n",
+ "231 g\n",
+ "232 h\n",
+ "233 i\n",
+ "234 j\n",
+ "235 k\n",
+ "236 l\n",
+ "237 m\n",
+ "238 n\n",
+ "239 o\n",
+ "240 p\n",
+ "241 q\n",
+ "242 r\n",
+ "243 s\n",
+ "244 t\n",
+ "245 u\n",
+ "246 v\n",
+ "247 w\n",
+ "248 x\n",
+ "249 y\n",
+ "250 z\n",
+ "251 {\n",
+ "252 |\n",
+ "253 }\n",
+ "254 ~\n",
+ "255 \u007f\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Scope and Life of Automatic Variable, Page number: 225<h3>\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the scope and life of an automatic variable'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 1 #auto int\n",
+ "\n",
+ "def b2 (i):\n",
+ " print i \n",
+ "\n",
+ "def b3 (i):\n",
+ " print i \n",
+ "\n",
+ "\n",
+ "def b1 (i):\n",
+ " print i \n",
+ " b2(i)\n",
+ " b3(i)\n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "b1(i)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n",
+ "1\n",
+ "1\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Scope and Life of Automatic Variable, Page number: 226<h3>\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the scope and life of an automatic variable'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 1 #auto int\n",
+ "\n",
+ "def b2 (i):\n",
+ " i = 2 # auto int \n",
+ " print i \n",
+ "\n",
+ "def b3 (i):\n",
+ " i = 3 #auto int \n",
+ " print i \n",
+ "\n",
+ "\n",
+ "def b1 (i):\n",
+ " b3(i)\n",
+ " b2(i)\n",
+ " print i \n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "b1(i)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n",
+ "2\n",
+ "1\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Different Data Types, Page number: 220<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that puts to use all the data types'''\n",
+ "\n",
+ "#char\n",
+ "#c,d = raw_input(\"char: \").split()\n",
+ "c = 'a'\n",
+ "d = 'b'\n",
+ "print \"%c %c\" %( c, d )\n",
+ "\n",
+ "#int \n",
+ "#i,j = raw_input( \"int: \").split()\n",
+ "i = 333\n",
+ "j = 288\n",
+ "print \"%d %u\"%( i, j ) \n",
+ "\n",
+ "#short int \n",
+ "#,l = raw_input( \"short int: \" ).split()\n",
+ "k = 2\n",
+ "l = 1\n",
+ "print \"%d %u \" %(k, l ) \n",
+ "\n",
+ "#long int \n",
+ "#m,n = raw_input( \"long int: \" ).split()\n",
+ "m = 73277727727\n",
+ "n = 189189819891\n",
+ "print \"%ld %lu\"%( m, n ) \n",
+ "\n",
+ "\n",
+ "#float, double, long double\n",
+ "from decimal import Decimal \n",
+ "#x,y,z = raw_input ( \"float double long double: \").split()\n",
+ "x = 72.12\n",
+ "y = Decimal(8282910.0109010)\n",
+ "z = Decimal(29189999111.128918918)\n",
+ "print x, y, z "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "a b\n",
+ "333 288\n",
+ "2 1 \n",
+ "73277727727 189189819891\n",
+ "72.12 8282910.0109010003507137298583984375 29189999111.128917694091796875\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Auto Increment, Page number: 228<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to understand the difference\n",
+ "between the automatic and static storage classes.'''\n",
+ "\n",
+ "#function definition \n",
+ "def increment( ):\n",
+ " i = 1 #auto int\n",
+ " print i \n",
+ " i = i + 1 \n",
+ "\n",
+ "#function calls\n",
+ "increment( )\n",
+ "increment( )\n",
+ "increment( )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n",
+ "1\n",
+ "1\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Static Increment, Page number: 228<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to understand the difference\n",
+ "between the automatic and static storage classes.'''\n",
+ "\n",
+ "#function definition \n",
+ "def increment(i = [1]): #static int \n",
+ " print i[0] \n",
+ " i[0] += 1 \n",
+ "\n",
+ "#function calls\n",
+ "increment()\n",
+ "increment()\n",
+ "increment()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n",
+ "2\n",
+ "3\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Static Storage Class, Page number: 229<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the advantages of using static storage class'''\n",
+ "\n",
+ "\n",
+ "#function definition\n",
+ "def fun( ):\n",
+ " k = 35 \n",
+ " return (k)\n",
+ "\n",
+ "j = fun( ) #function call\n",
+ "print j #result\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "35\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>External Storage Class, Page number: 231<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the extrnal storage class'''\n",
+ "\n",
+ "#Variable declaration \n",
+ "i = [0] # external variable\n",
+ "\n",
+ "#Function definitions \n",
+ "def increment(i = [0]):\n",
+ " i[0] += 1\n",
+ " print \"on incrementing i = \", i[0] \n",
+ "\n",
+ "\n",
+ "def decrement(i = [2]):\n",
+ " i[0] -= 1\n",
+ " print \"on decrementing i = \", i[0] \n",
+ " \n",
+ "\n",
+ "\n",
+ "print \"i = \", i[0]\n",
+ "#function calls\n",
+ "increment() \n",
+ "increment() \n",
+ "decrement() \n",
+ "decrement() "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "i = 0\n",
+ "on incrementing i = 1\n",
+ "on incrementing i = 2\n",
+ "on decrementing i = 1\n",
+ "on decrementing i = 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-7.ipynb b/Let_us_C/chapter-7.ipynb new file mode 100644 index 00000000..9024266b --- /dev/null +++ b/Let_us_C/chapter-7.ipynb @@ -0,0 +1,210 @@ +{
+ "metadata": {
+ "name": "chapter-7..ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 7: The C Preprocessor<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Macro Expansion, Page number: 244<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate macros'''\n",
+ "\n",
+ "#Macro declaration\n",
+ "UPPER = 25\n",
+ "\n",
+ "for i in range(1,UPPER+1): #macro expansion\n",
+ " print(i)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "9\n",
+ "10\n",
+ "11\n",
+ "12\n",
+ "13\n",
+ "14\n",
+ "15\n",
+ "16\n",
+ "17\n",
+ "18\n",
+ "19\n",
+ "20\n",
+ "21\n",
+ "22\n",
+ "23\n",
+ "24\n",
+ "25\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Macro Definition, Page number: 244<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate macros'''\n",
+ "\n",
+ "#Macro declaration\n",
+ "PI = 3.1415\n",
+ "\n",
+ "#Variable declaration\n",
+ "r = 6.25 \n",
+ "\n",
+ "#Calculation\n",
+ "area = PI * r * r\n",
+ "\n",
+ "#Result\n",
+ "print \"Area of circle = \", area \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Area of circle = 122.71484375\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Macros with Arguments, Page number: 248<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate macros with arguments'''\n",
+ "\n",
+ "#Macro declaration\n",
+ "def AREA(x): #define AREA(x) ( 3.14 * x * x )\n",
+ " return(3.14 * x * x )\n",
+ "\n",
+ "#Variable declaration\n",
+ "r1 = 6.25\n",
+ "r2 = 2.5\n",
+ "\n",
+ "#Result\n",
+ "a = AREA(r1)\n",
+ "print \"Area of circle = \", a \n",
+ "a = AREA(r2) \n",
+ "print \"Area of circle = \", a\n",
+ " \n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Area of circle = 122.65625\n",
+ "Area of circle = 19.625\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Macros with Arguments, Page number: 249<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate macros with arguments'''\n",
+ "\n",
+ "#Macro declaration\n",
+ "def ISDIGIT(y): #define ISDIGIT(y) ( y >= 48 && y <= 57 )\n",
+ " return( y >= 48 and y <= 57 )\n",
+ "\n",
+ "#Input from user\n",
+ "#ch = raw_input(\"Enter any digit \")\n",
+ "ch = 'a'\n",
+ "\n",
+ "#Result\n",
+ "if ISDIGIT ( ch ):\n",
+ " print \"You entered a digit\" \n",
+ "else:\n",
+ " print \"Illegal input\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Illegal input\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-8.ipynb b/Let_us_C/chapter-8.ipynb new file mode 100644 index 00000000..3c03d537 --- /dev/null +++ b/Let_us_C/chapter-8.ipynb @@ -0,0 +1,936 @@ +{
+ "metadata": {
+ "name": "chapter-8.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 8: Arrays <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Average Marks, Page number: 272<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to find average marks obtained by a class of 30 students in a test'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "s = 0\n",
+ "marks = [] # array declaration\n",
+ "\n",
+ "#for i in range(0,30):\n",
+ " # marks.append(int(raw_input(\"Enter marks: \" ))) # store data in array\n",
+ "marks = [89,85,57,25,90,45,87,48,98,12,39,66,75,30,87,100,5,78,56,99,84,0,39,79,93,61,87,45,90,56] \n",
+ "print \"Enter marks: \"\n",
+ "for i in range(0,30):\n",
+ " print marks[i]\n",
+ "\n",
+ "for i in range(0,30):\n",
+ " s = s + marks[i] # read data from array\n",
+ "\n",
+ "#Calculation\n",
+ "avg = s / 30 #Average formula\n",
+ "\n",
+ "#Result\n",
+ "print \"Average marks = \", avg \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks: \n",
+ "89\n",
+ "85\n",
+ "57\n",
+ "25\n",
+ "90\n",
+ "45\n",
+ "87\n",
+ "48\n",
+ "98\n",
+ "12\n",
+ "39\n",
+ "66\n",
+ "75\n",
+ "30\n",
+ "87\n",
+ "100\n",
+ "5\n",
+ "78\n",
+ "56\n",
+ "99\n",
+ "84\n",
+ "0\n",
+ "39\n",
+ "79\n",
+ "93\n",
+ "61\n",
+ "87\n",
+ "45\n",
+ "90\n",
+ "56\n",
+ "Average marks = 63\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Call By Value, Page number: 277<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate call by value by passing values of\n",
+ "array elements to the function'''\n",
+ "\n",
+ "#Funcion definition\n",
+ "def display(m):\n",
+ " print m \n",
+ " \n",
+ "#Variable declaration\n",
+ "marks = [ 55, 65, 75, 56, 78, 78, 90 ] #array\n",
+ "\n",
+ "for i in range(0,7):\n",
+ " display(marks[i]) #function call \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "55\n",
+ "65\n",
+ "75\n",
+ "56\n",
+ "78\n",
+ "78\n",
+ "90\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Call By Reference , Page number: 278<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate call by reference by passing values of\n",
+ "array elements to the function'''\n",
+ "\n",
+ "#Funcion definition\n",
+ "def display(n):\n",
+ " print n #return\n",
+ " \n",
+ "#Variable declaration\n",
+ "marks = [ 55, 65, 75, 56, 78, 78, 90 ] #array\n",
+ "\n",
+ "for i in range(0,7):\n",
+ " display(marks[i]) #function call\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "55\n",
+ "65\n",
+ "75\n",
+ "56\n",
+ "78\n",
+ "78\n",
+ "90\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Pointer Arithmetic, Page number: 279<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the relationship between\n",
+ "pointers and arrays'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 3\n",
+ "j = 1.5\n",
+ "k = 'c'\n",
+ "\n",
+ "print \"Value of i = \", i \n",
+ "print \"Value of j = \", j \n",
+ "print \"Value of k = \", k \n",
+ "\n",
+ "#addresses of the variables\n",
+ "x = id(i)\n",
+ "y = id(j)\n",
+ "z = id(k)\n",
+ "\n",
+ "print \"Original address in x = \", x \n",
+ "print \"Original address in y = \", y \n",
+ "print \"Original address in z = \", z \n",
+ "\n",
+ "x += 2\n",
+ "y += 4\n",
+ "z += 1\n",
+ "\n",
+ "print \"New address in x = \", x \n",
+ "print \"New address in y = \", y \n",
+ "print \"New address in z = \", z \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Value of i = 3\n",
+ "Value of j = 1.5\n",
+ "Value of k = c\n",
+ "Original address in x = 32529512\n",
+ "Original address in y = 105587440\n",
+ "Original address in z = 32744192\n",
+ "New address in x = 32529514\n",
+ "New address in y = 105587444\n",
+ "New address in z = 32744193\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Pointer Subtraction , Page number: 281<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to calculate the number of bytes\n",
+ "separating the corresponding array elements'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "arr = [ 10, 20, 30, 45, 67, 56, 74 ]\n",
+ "i = id(arr[1]) #address \n",
+ "j = id(arr[5]) #Address\n",
+ "\n",
+ "#Result\n",
+ "print j - i, arr[5] - arr[1] \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1128 36\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Pointer Comparison, Page number: 282<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate comparison of 2 pointer variables'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "arr = [ 10, 20, 36, 72, 45, 36 ]\n",
+ "j = id(arr[ 4 ])\n",
+ "k = id( arr[0 + 4] )\n",
+ "\n",
+ "#Result \n",
+ "if j == k : #comparison\n",
+ " print \"The two pointers point to the same location\" \n",
+ "else:\n",
+ " print \"The two pointers do not point to the same location\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The two pointers point to the same location\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Memory Locations, Page number: 283<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that prints out the memory locations\n",
+ "in which the elements of the array are stored'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "num = [ 24, 34, 12, 44, 56, 17]\n",
+ "\n",
+ "#Result\n",
+ "for i in range(0,6):\n",
+ " print \"element no. \", i \n",
+ " print \"address = \", id(num[i]) \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "element no. 0\n",
+ "address = 32529008\n",
+ "element no. 1\n",
+ "address = 32528768\n",
+ "element no. 2\n",
+ "address = 32529296\n",
+ "element no. 3\n",
+ "address = 32530520\n",
+ "element no. 4\n",
+ "address = 32530232\n",
+ "element no. 5\n",
+ "address = 32529176\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Accessing Array Elements , Page number: 284<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program shows a way in which we can access the elements of an array.'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "num = [ 24, 34, 12, 44, 56, 17]\n",
+ "\n",
+ "#Result\n",
+ "for i in range(0,6):\n",
+ " print \"address = \", id(num[i]) \n",
+ " print \"element = \", num[i] \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "address = 32529008\n",
+ "element = 24\n",
+ "address = 32528768\n",
+ "element = 34\n",
+ "address = 32529296\n",
+ "element = 12\n",
+ "address = 32530520\n",
+ "element = 44\n",
+ "address = 32530232\n",
+ "element = 56\n",
+ "address = 32529176\n",
+ "element = 17\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Accessing Array Elements Using Pointers, Page number: 284<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program shows a way in which we can access the elements of an array using\n",
+ "pointers'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "num = [ 24, 34, 12, 44, 56, 17]\n",
+ "j = id(num[0]) # assign address of zeroth element\n",
+ "\n",
+ "#Result\n",
+ "for i in range(0,6):\n",
+ " print \"address = \", j \n",
+ " print \"element = \", num[i] \n",
+ " j = id(num[(i+1)%6]) # increment pointer to point to next location \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "address = 32529008\n",
+ "element = 24\n",
+ "address = 32528768\n",
+ "element = 34\n",
+ "address = 32529296\n",
+ "element = 12\n",
+ "address = 32530520\n",
+ "element = 44\n",
+ "address = 32530232\n",
+ "element = 56\n",
+ "address = 32529176\n",
+ "element = 17\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Passing An Array to a Function , Page number: 286<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the of passing an entire array to a function'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "num = [ 24, 34, 12, 44, 56, 17 ]\n",
+ "\n",
+ "#Function definition:\n",
+ "def display ( j,n ):\n",
+ " for i in range(0,n):\n",
+ " print \"element = \", j\n",
+ " j = num[(i+1)%n] #increment pointer to point to next element \n",
+ "\n",
+ "display ( num[0], 6 ) #function call\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "element = 24\n",
+ "element = 34\n",
+ "element = 12\n",
+ "element = 44\n",
+ "element = 56\n",
+ "element = 17\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Accessing Array Elements in Different Ways, Page number: 288<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate accessing array elements in different ways '''\n",
+ "\n",
+ "#Variable declaration\n",
+ "num = [ 24, 34, 12, 44, 56, 17]\n",
+ "\n",
+ "for i in range(0,6):\n",
+ " print \"address = \", id(num[i])\n",
+ " print \"element = \", num[i], num[(0+ i)]\n",
+ " print num[(i+0)], num[i] \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "address = 32529008\n",
+ "element = 24 24\n",
+ "24 24\n",
+ "address = 32528768\n",
+ "element = 34 34\n",
+ "34 34\n",
+ "address = 32529296\n",
+ "element = 12 12\n",
+ "12 12\n",
+ "address = 32530520\n",
+ "element = 44 44\n",
+ "44 44\n",
+ "address = 32530232\n",
+ "element = 56 56\n",
+ "56 56\n",
+ "address = 32529176\n",
+ "element = 17 17\n",
+ "17 17\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Two Dimensional Array , Page number: 289<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate two dimensional arrays'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "stud = [] #array\n",
+ "\n",
+ "#for i in range(0,4):\n",
+ " # stud.append((raw_input(\"Enter roll no and marks: \").split())) # storing data in the 2D array\n",
+ "\n",
+ "stud = [\"1 38\",\"2 78\",\"3 93\",\"4 48\"]\n",
+ "for i in range (0,4):\n",
+ " print \"Enter roll no and marks: \"\n",
+ " print stud[i]\n",
+ " \n",
+ "#Result\n",
+ "for i in range(0,4):\n",
+ " print stud[i][0],stud[i][1:] \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter roll no and marks: \n",
+ "1 38\n",
+ "Enter roll no and marks: \n",
+ "2 78\n",
+ "Enter roll no and marks: \n",
+ "3 93\n",
+ "Enter roll no and marks: \n",
+ "4 48\n",
+ "1 38\n",
+ "2 78\n",
+ "3 93\n",
+ "4 48\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>2-D Array Demo , Page number: 293<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate that a 2-D array is an array of arrays '''\n",
+ "\n",
+ "#Variable declaration\n",
+ "s = [ [1234, 56 ], [ 1212, 33], [ 1434, 80 ], [ 1312, 78 ]]\n",
+ "\n",
+ "#Result\n",
+ "for i in range(0,4):\n",
+ " print \"Address of %d th 1-D array = %u\"%( i, id(s[i]) )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of 0 th 1-D array = 134002248\n",
+ "Address of 1 th 1-D array = 134654472\n",
+ "Address of 2 th 1-D array = 134791816\n",
+ "Address of 3 th 1-D array = 134792008\n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Accessing 2-D Array Elements, Page number: 295<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the use of pointer\n",
+ "notation to access 2-D array elements'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "s = [ [ 1234, 56 ], [ 1212, 33 ], [ 1434, 80 ], [ 1312, 78 ] ]\n",
+ "\n",
+ "for i in range(0,4):\n",
+ " for j in range(0,2):\n",
+ " print s[i][j] \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1234\n",
+ "56\n",
+ "1212\n",
+ "33\n",
+ "1434\n",
+ "80\n",
+ "1312\n",
+ "78\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Pointer To An Array , Page number: 296<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the usage of pointer to an array'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "s = [ [ 1234, 56 ], [ 1212, 33 ], [ 1434, 80 ], [ 1312, 78 ]]\n",
+ "\n",
+ "#Result\n",
+ "for i in range(0,4):\n",
+ " p = s[i]\n",
+ " pint = p\n",
+ " print \"\\n\" \n",
+ " for j in range(0,2):\n",
+ " print pint[j] \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "1234\n",
+ "56\n",
+ "\n",
+ "\n",
+ "1212\n",
+ "33\n",
+ "\n",
+ "\n",
+ "1434\n",
+ "80\n",
+ "\n",
+ "\n",
+ "1312\n",
+ "78\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Passing 2-D Array To a Function , Page number: 297<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate three ways of accessing a 2-D array'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "a = [[1, 2, 3, 4] , [5, 6, 7, 8] , [9, 0, 1, 6 ]]\n",
+ "\n",
+ "#Function definitions\n",
+ "def display ( q, row, col ):\n",
+ " for i in range(0,row):\n",
+ " for j in range(0,col):\n",
+ " print q [ (i * col)%3][j]\n",
+ " print \"\\n\" \n",
+ " print \"\\n\" \n",
+ "\n",
+ "\n",
+ "def show ( q, row, col ):\n",
+ " for i in range(0,row):\n",
+ " p = q[i]\n",
+ " for j in range(0,col):\n",
+ " print p[j]\n",
+ " print \"\\n\" \n",
+ " print \"\\n\" \n",
+ "\n",
+ "def Print ( q, row, col ):\n",
+ " for i in range(0,row):\n",
+ " for j in range(0,col):\n",
+ " print q[i][j]\n",
+ " print \"\\n\" \n",
+ " print \"\\n\" \n",
+ " \n",
+ "#function calls\n",
+ "display ( a, 3, 4 ) \n",
+ "show ( a, 3, 4 ) \n",
+ "Print ( a, 3, 4 )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "\n",
+ "\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "\n",
+ "\n",
+ "9\n",
+ "0\n",
+ "1\n",
+ "6\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "\n",
+ "\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "\n",
+ "\n",
+ "9\n",
+ "0\n",
+ "1\n",
+ "6\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "\n",
+ "\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "\n",
+ "\n",
+ "9\n",
+ "0\n",
+ "1\n",
+ "6\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 25
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Array of Pointers , Page number: 300<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate an array of pointers'''\n",
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "#Variable declaration\n",
+ "arr = [] # array of integer pointers \n",
+ "i = c_int(31)\n",
+ "j = c_int(5)\n",
+ "k = c_int(19)\n",
+ "l = c_int(71)\n",
+ "arr.append(pointer(i))\n",
+ "arr.append(pointer(j))\n",
+ "arr.append(pointer(k))\n",
+ "arr.append(pointer(l))\n",
+ "\n",
+ "for m in range(0,4):\n",
+ " print arr[m].contents "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "c_long(31)\n",
+ "c_long(5)\n",
+ "c_long(19)\n",
+ "c_long(71)\n"
+ ]
+ }
+ ],
+ "prompt_number": 26
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Address Array , Page number: 301<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate that an array of pointers can even\n",
+ "contain the addresses of other arrays'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "a = [ 0, 1, 2, 3, 4 ]\n",
+ "p = [ a[0], a[1], a[2], a[3], a[4] ]\n",
+ "\n",
+ "#Result\n",
+ "print p, id(p), id(id(p ) )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "[0, 1, 2, 3, 4] 134794120 134005648\n"
+ ]
+ }
+ ],
+ "prompt_number": 29
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/chapter-9.ipynb b/Let_us_C/chapter-9.ipynb new file mode 100644 index 00000000..1f588a47 --- /dev/null +++ b/Let_us_C/chapter-9.ipynb @@ -0,0 +1,616 @@ +{
+ "metadata": {
+ "name": "chapter-9.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 9: Puppetting On Strings <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Print String , Page number: 329<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate printing of a string'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "name= \"Klinsman\" # character array or string\n",
+ "i = 0\n",
+ "\n",
+ "#while loop for printing\n",
+ "while i <= 7 :\n",
+ " print name[i] \n",
+ " i = i + 1\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "K\n",
+ "l\n",
+ "i\n",
+ "n\n",
+ "s\n",
+ "m\n",
+ "a\n",
+ "n\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Print String, Page number: 330<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate printing of a string'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "name= \"Klinsman\\0\" # character array or string\n",
+ "i = 0\n",
+ "\n",
+ "#while loop for printing\n",
+ "while (name[i] != '\\0') :\n",
+ " print name[i]\n",
+ " i = i + 1\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "K\n",
+ "l\n",
+ "i\n",
+ "n\n",
+ "s\n",
+ "m\n",
+ "a\n",
+ "n\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Print String Using Pointer, Page number: 330<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to print a string using a pointer to access the array elements'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "name = \"Klinsman\\0\" # string or character array\n",
+ "i = 0\n",
+ "ptr = name[i] # store base address of string\n",
+ "\n",
+ "#Result\n",
+ "while ptr != '\\0':\n",
+ " print ptr\n",
+ " i = i+1\n",
+ " ptr = name[i]\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "K\n",
+ "l\n",
+ "i\n",
+ "n\n",
+ "s\n",
+ "m\n",
+ "a\n",
+ "n\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>strlen Function, Page number: 336<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the 'strlen' function '''\n",
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "arr = \"Bamboozled\" # character array or string\n",
+ "\n",
+ "#string length function\n",
+ "len1 = len(arr) \n",
+ "len2 = len( \"Humpty Dumpty\" )\n",
+ "\n",
+ "#Result\n",
+ "print \"string = %s length = %d\" %( arr, len1 )\n",
+ "print \"string = %s length = %d\" %(\"Humpty Dumpty\", len2 )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "string = Bamboozled length = 10\n",
+ "string = Humpty Dumpty length = 13\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>xstrlen Function, Page number: 337<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a function that\n",
+ "counts the number of characters present in a string'''\n",
+ "\n",
+ "#Function definition\n",
+ "def xstrlen(s):\n",
+ " length = 0\n",
+ " while ( s[length] != '\\0' ):\n",
+ " length += 1\n",
+ " return length \n",
+ "\n",
+ "#Variable declaration\n",
+ "arr = \"Bamboozled\\0\" # character array or string\n",
+ "\n",
+ "#Function calls\n",
+ "len1 = xstrlen(arr) \n",
+ "len2 = xstrlen( \"Humpty Dumpty\\0\" )\n",
+ "\n",
+ "#Result\n",
+ "print \"string = %s length = %d\" %( arr, len1 )\n",
+ "print \"string = %s length = %d\" %(\"Humpty Dumpty\\0\", len2 )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "string = Bamboozled\u0000 length = 10\n",
+ "string = Humpty Dumpty\u0000 length = 13\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>strcpy Function, Page number: 338<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the 'strcpy' function '''\n",
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "source = \"Sayonara\" \n",
+ "target = [] \n",
+ "\n",
+ "#strcpy function\n",
+ "import copy\n",
+ "target = copy.copy(source)\n",
+ "\n",
+ "#Result\n",
+ "print \"source string = \", source \n",
+ "print \"target string = \", target \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "source string = Sayonara\n",
+ "target string = Sayonara\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>xstrcpy Function, Page number: 339<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a function that\n",
+ "copies the contents of one string into another'''\n",
+ "\n",
+ "#Function definition\n",
+ "def xstrcpy (t,s):\n",
+ " i = 0\n",
+ " while ( s[i] != '\\0' ):\n",
+ " t = t + s[i]\n",
+ " i = i + 1\n",
+ " return t\n",
+ " \n",
+ "#Variable declaration\n",
+ "source = \"Sayonara\\0\" \n",
+ "target = ''\n",
+ "\n",
+ "target = xstrcpy ( target, source ) # function call\n",
+ "\n",
+ "#Result\n",
+ "print \"source string = \", source \n",
+ "print \"target string = \", target "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "source string = Sayonara\u0000\n",
+ "target string = Sayonara\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>strcat Function, Page number: 342<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the 'strcat' function'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "source = \"Folks!\" \n",
+ "target = \"Hello\"\n",
+ " \n",
+ "target = target + source # string concatenation\n",
+ "#Result\n",
+ "print \"source string = \", source \n",
+ "print \"target string = \", target \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "source string = Folks!\n",
+ "target string = HelloFolks!\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>strcmp Function, Page number: 343<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate the 'strcmp' function'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "string1 = \"Jerry\\0\" \n",
+ "string2 = \"Ferry\\0\"\n",
+ "\n",
+ "#Function definition\n",
+ "def strcmp (string1 , string2):\n",
+ " if (string1 == string2):\n",
+ " v = 0 #If the two strings are identical, strcmp returns a value zero\n",
+ " return v\n",
+ " else:\n",
+ " n = 0\n",
+ " while ( string1[n]):\n",
+ " if ( string1[n] == string2[n]):\n",
+ " n = n + 1\n",
+ " continue\n",
+ " else:\n",
+ " v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters\n",
+ " return v\n",
+ " return v\n",
+ "\n",
+ "#Function call\n",
+ "i = strcmp ( string1, \"Jerry\\0\" ) \n",
+ "j = strcmp ( string1, string2 ) \n",
+ "k = strcmp ( string1, \"Jerry boy\\0\" )\n",
+ "\n",
+ "#Result\n",
+ "print i,j,k"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0 4 -32\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Two Dimensional Array of Characters, Page number: 344<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program asks you to type your name. When you do so, it checks your\n",
+ "name against a master list to see if you are worthy of\n",
+ "entry to the palace'''\n",
+ "\n",
+ "\n",
+ "#Function definition\n",
+ "def strcmp (string1 , string2):\n",
+ " if (string1 == string2):\n",
+ " v = 0 #If the two strings are identical, strcmp returns a value zero\n",
+ " return v\n",
+ " else:\n",
+ " n = 0\n",
+ " while ( string1[n]):\n",
+ " if ( string1[n] == string2[n]):\n",
+ " n = n + 1\n",
+ " continue\n",
+ " else:\n",
+ " v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters\n",
+ " return v\n",
+ " return v\n",
+ "\n",
+ "#Variable declaration\n",
+ "FOUND = 1\n",
+ "NOTFOUND = 0\n",
+ "masterlist =[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
+ "yourname = []\n",
+ "flag = NOTFOUND\n",
+ "\n",
+ "#Input from user\n",
+ "#yourname = raw_input(\"Enter your name: \")\n",
+ "yourname = \"Akshatha\"\n",
+ "\n",
+ "#Checking in the master list\n",
+ "for i in range(0,6):\n",
+ " a = strcmp ( masterlist[i], yourname )\n",
+ " if a == 0:\n",
+ " print \"Welcome, you can enter the palace\" \n",
+ " flag = FOUND\n",
+ " break\n",
+ " \n",
+ "if flag == NOTFOUND :\n",
+ " print \"Sorry, you are a trespasser\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sorry, you are a trespasser\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Exchange Names using 2-D Array of Characters, Page number: 348<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to exchange names using 2-D array of characters'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "names = [\"akshay\\0\\0\\0\",\"parag\\0\\0\\0\\0\",\"raman\\0\\0\\0\\0\",\"srinivas\\0\",\"gopal\\0\\0\\0\\0\",\"rajesh\\0\\0\\0\"]\n",
+ "\n",
+ "#Initial condition\n",
+ "print \"Original: \", names[2], names[3] \n",
+ "\n",
+ "#Exchanging names\n",
+ "for i in range(0,9):\n",
+ " t = names[3][i]\n",
+ " names[3] = names[3][0:i] + names[2][i] + names[3][i+1:]\n",
+ " names[2] = names[2][0:i] + t + names[2][i+1:]\n",
+ " \n",
+ "\n",
+ "#Result \n",
+ "print \"New: \", names[2], names[3] "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Original: raman\u0000\u0000\u0000\u0000 srinivas\u0000\n",
+ "New: srinivas\u0000 raman\u0000\u0000\u0000\u0000\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Exchange Names using Array of Pointers to Strings, Page number: 349<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to exchange names using 2-D array of characters'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "names = [\"akshay\\0\\0\\0\",\"parag\\0\\0\\0\\0\",\"raman\\0\\0\\0\\0\",\"srinivas\\0\",\"gopal\\0\\0\\0\\0\",\"rajesh\\0\\0\\0\"]\n",
+ "\n",
+ "#Initial condition\n",
+ "print \"Original: \", names[2], names[3] \n",
+ "\n",
+ "#Exchanging names\n",
+ "temp = names[2] \n",
+ "names[2] = names[3] \n",
+ "names[3] = temp \n",
+ "\n",
+ "#Result \n",
+ "print \"New: \", names[2], names[3] \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Original: raman\u0000\u0000\u0000\u0000 srinivas\u0000\n",
+ "New: srinivas\u0000 raman\u0000\u0000\u0000\u0000\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Solution, Page number: 351<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the solution to the problem of limitation\n",
+ "of Array of Pointers to Strings'''\n",
+ "\n",
+ "import copy\n",
+ "\n",
+ "#Variable declaration\n",
+ "names = []\n",
+ "n=[\"John\",\"Max\",\"Jim\",\"Tony\",\"Tom\",\"Harry\"]\n",
+ "\n",
+ "for i in range(0,6):\n",
+ " n1 = n[i]\n",
+ " p = copy.copy(n1)\n",
+ " names.append(p) \n",
+ "\n",
+ "for i in range(0,6):\n",
+ " print names[i] \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "John\n",
+ "Max\n",
+ "Jim\n",
+ "Tony\n",
+ "Tom\n",
+ "Harry\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Let_us_C/screenshots/binary.png b/Let_us_C/screenshots/binary.png Binary files differnew file mode 100644 index 00000000..5a14ee93 --- /dev/null +++ b/Let_us_C/screenshots/binary.png diff --git a/Let_us_C/screenshots/hellowindows.png b/Let_us_C/screenshots/hellowindows.png Binary files differnew file mode 100644 index 00000000..622bf2b7 --- /dev/null +++ b/Let_us_C/screenshots/hellowindows.png diff --git a/Let_us_C/screenshots/macro.png b/Let_us_C/screenshots/macro.png Binary files differnew file mode 100644 index 00000000..161b4c74 --- /dev/null +++ b/Let_us_C/screenshots/macro.png |