{
 "metadata": {
  "name": "",
  "signature": "sha256:adc18fea588596d0bfa5f1f11f3697194c0b52d2d9eb299f10c4a99dfea56e31"
 },
 "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": [
      "\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": [
      "\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": [
      "\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": [
      "\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": [
      "\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": [
      "\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": [
      "\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": [
      "\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": [
      "\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": [
      "\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": {}
  }
 ]
}