{ "metadata": { "name": "", "signature": "sha256:133c881c520fd0cdef07f65db120109cc62a996dd5161cf0f664c2ffcaa3adc4" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 13, Structures" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 13-1, Page Number: 370" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Introduction to structures\n", "\n", "from collections import namedtuple\n", "\n", "#Structure defintion\n", "struct = namedtuple(\"struct\", \"integer real\")\n", "\n", "first_structure = struct( 7, 3.14 )\n", "second_structure = first_structure\n", "\n", "print \"integer = %d, real = %f\"%(second_structure.integer,second_structure.real)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "integer = 7, real = 3.140000\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 13-2, Page Number: 371" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Initializing a structure\n", "\n", "from collections import namedtuple\n", "\n", "#Structure defintion\n", "struct = namedtuple(\"struct\", \"integer real\")\n", "\n", "structure = struct( 7, 3.14 )\n", "\n", "print \"integer = %d, real = %f\"%(structure.integer,structure.real)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "integer = 7, real = 3.140000\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 13-3, Page Number: 372" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# The structure tag\n", "\n", "from collections import namedtuple\n", "\n", "#Structure defintion\n", "struct = namedtuple(\"struct\", \"integer real\")\n", "\n", "first_structure = struct( 7, 3.14 )\n", "second_structure = first_structure\n", "\n", "print \"integer = %d, real = %f\"%(second_structure.integer,second_structure.real)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "integer = 7, real = 3.140000\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 13-4, Page Number: 375" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# An array of structures\n", "\n", "from collections import namedtuple\n", "\n", "#Structure defintion\n", "struct = namedtuple(\"struct\", \"code price\")\n", "\n", "#code = [ \"a\", \"b\", \"c\", \"d\", \"e\" ]\n", "#price = [ 3.29, 2.99, 0.59, 1.59, 2.39 ]\n", "\n", "table = [ struct(\"a\",3.29) , struct(\"b\",2.99), struct(\"c\",0.59), struct(\"d\",1.59), struct(\"e\",2.39) ]\n", "\n", "print \"Enter item code:\",\n", "item_code = \"a\"\n", "print item_code\n", "i = 0\n", "\n", "while item_code != \".\" :\n", "\n", "\t#Search for 'item_code' in 'table'\n", "\tfor i in range(4) :\n", "\t\tif table[i].code == item_code :\n", "\t\t\tbreak\n", "\n", "\tif i < 4:\n", "\t\tprint \"Enter quantity: \",\n", "\t\tquantity = 1\n", "\t\tprint quantity\n", "\t\n", "\t\tprint \"\\nUnit price = %.2f, total price = %.2f.\\n\\n\"%(table[i].price,quantity * table[i].price)\n", "\telse :\n", "\t\tprint \"\\nItem code %c does not exist.\\n\\n\"%item_code\n", "\tprint \"Enter item code:\",\n", "\titem_code = \".\"\n", "\tprint item_code\n", "\n", "\n", "print \"Thank you.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter item code: a\n", "Enter quantity: 1\n", "\n", "Unit price = 3.29, total price = 3.29.\n", "\n", "\n", "Enter item code: .\n", "Thank you.\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 13-5, Page Number: 378" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A pointer in a structure\n", "\n", "from collections import namedtuple\n", "\n", "#Structure defintion\n", "struct = namedtuple(\"struct\", \"name price\")\n", "\n", "#code = [ \"a\", \"b\", \"c\", \"d\", \"e\" ]\n", "#price = [ 3.29, 2.99, 0.59, 1.59, 2.39 ]\n", "\n", "table = [ struct(\"pickles\",2.29) , struct(\"soda\",0.99), struct(\"yogurt\",0.59), struct(\"bread\",1.09), struct(\"milk\",0.69) ]\n", "\n", "print \"Enter item name:\",\n", "item_name = \"pickles\"\n", "print item_name\n", "i = 0\n", "\n", "while item_name != \".\" :\n", "\n", "\t#Search for 'item_code' in 'table'\n", "\tfor i in range(4) :\n", "\t\tif table[i].name == item_name :\n", "\t\t\tbreak\n", "\n", "\tif i < 4:\n", "\t\tprint \"Enter quantity: \",\n", "\t\tquantity = 1\n", "\t\tprint quantity\n", "\t\n", "\t\tprint \"\\nUnit price = %.2f, total price = %.2f.\\n\\n\"%(table[i].price,quantity * table[i].price)\n", "\telse :\n", "\t\tprint \"\\nItem code %c does not exist.\\n\\n\"%item_name\n", "\tprint \"Enter item code:\",\n", "\titem_name = \".\"\n", "\tprint item_name\n", "\n", "\n", "print \"Thank you.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter item name: pickles\n", "Enter quantity: 1\n", "\n", "Unit price = 2.29, total price = 2.29.\n", "\n", "\n", "Enter item code: .\n", "Thank you.\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 13-6, Page Number: 381" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# An array in a structure\n", "\n", "from collections import namedtuple\n", "\n", "#Structure defintion\n", "struct = namedtuple(\"struct\", \"name price\")\n", "\n", "#code = [ \"a\", \"b\", \"c\", \"d\", \"e\" ]\n", "#price = [ 3.29, 2.99, 0.59, 1.59, 2.39 ]\n", "\n", "table = [ struct(\"pickles\",2.29) , struct(\"soda\",0.99), struct(\"yogurt\",0.59), struct(\"bread\",1.09), struct(\"milk\",0.69) ]\n", "\n", "print \"Enter item name:\",\n", "item_name = \"milk\"\n", "print item_name\n", "i = 0\n", "\n", "while item_name != \".\" :\n", "\n", "\t#Search for 'item_code' in 'table'\n", "\tfor i in range(5) :\n", "\t\tif table[i].name == item_name :\n", "\t\t\tbreak\n", "\n", "\tif i < 5:\n", "\t\tprint \"Enter quantity: \",\n", "\t\tquantity = 5\n", "\t\tprint quantity\n", "\t\n", "\t\tprint \"\\nUnit price = %.2f, total price = %.2f.\\n\\n\"%(table[i].price,quantity * table[i].price)\n", "\telse :\n", "\t\tprint \"\\nItem code %c does not exist.\\n\\n\"%item_name\n", "\tprint \"Enter item code:\",\n", "\titem_name = \".\"\n", "\tprint item_name\n", "\n", "\n", "print \"Thank you.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter item name: milk\n", "Enter quantity: 5\n", "\n", "Unit price = 0.69, total price = 3.45.\n", "\n", "\n", "Enter item code: .\n", "Thank you.\n" ] } ], "prompt_number": 6 } ], "metadata": {} } ] }