summaryrefslogtreecommitdiff
path: root/Let_us_C/chapter-10.ipynb
diff options
context:
space:
mode:
authorJovina Dsouza2014-06-18 12:43:07 +0530
committerJovina Dsouza2014-06-18 12:43:07 +0530
commit206d0358703aa05d5d7315900fe1d054c2817ddc (patch)
treef2403e29f3aded0caf7a2434ea50dd507f6545e2 /Let_us_C/chapter-10.ipynb
parentc6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff)
downloadPython-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip
adding book
Diffstat (limited to 'Let_us_C/chapter-10.ipynb')
-rw-r--r--Let_us_C/chapter-10.ipynb499
1 files changed, 499 insertions, 0 deletions
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