{ "metadata": { "name": "", "signature": "sha256:054e81c7d22cc52697211ef4bdf39c086b00ba90e5528d8b310b3644dab0674b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 19 : Understanding Structures" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 19.1, Page No 316" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class computer:\n", " def __init__(self, cost, year, cpu_speed, cpu_type):\n", " self.cost = cost\n", " self.year = year\n", " self.cpu_speed = cpu_speed\n", " self.cpu_type = cpu_type\n", "if __name__ == '__main__':\n", " cpu_type = raw_input(\"The type of CPU inside your computer: \")\n", " cpu_speed = int(raw_input(\"The speed(MHz) of the CPU?: \"))\n", " year = int(raw_input(\"The year your computer was made?: \"))\n", " cost = float(raw_input(\"How much you paid for the computer?: \"))\n", " computer(cost,year,cpu_speed,cpu_type)\n", " print \"Hear are what you have entered\"\n", " print \"Year: \",year,\"\\n\"\n", " print \"Cost: \",cost,\"\\n\"\n", " print \"CPU type: \",cpu_type,\"\\n\"\n", " print \"CPU speed: \",cpu_speed,\"MHz\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The type of CPU inside your computer: Pentium\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The speed(MHz) of the CPU?: 100\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The year your computer was made?: 1996\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "How much you paid for the computer?: 1234.56\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Hear are what you have entered\n", "Year: 1996 \n", "\n", "Cost: 1234.56 \n", "\n", "CPU type: Pentium \n", "\n", "CPU speed: 100 MHz\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 19.2, Page No 318" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class employee:\n", " def __init__(self,id1,name):\n", " self.id1 = id1\n", " self.name = name\n", "if __name__ == '__main__':\n", " info = employee(1,\"B.Smith\")\n", " print \"Here is a sample: \\n\"\n", " print \"Employee Name : \",info.name,\"\\n\"\n", " print \"Employee ID : \",info.id1\n", " info.name = raw_input(\"What's your name?: \")\n", " info.id1 = int(raw_input(\"What's your ID number: \"))\n", " print \"\\nHere are what you entered: \\n\"\n", " print \"Name: \",info.name,\"\\n\"\n", " print \"Id: \",info.id1,\"\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Here is a sample: \n", "\n", "Employee Name : B.Smith \n", "\n", "Employee ID : 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What's your name?: T. Zhang\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What's your ID number: 1234\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Here are what you entered: \n", "\n", "Name: T. Zhang \n", "\n", "Id: 1234 \n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 19.3, Page No 320" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class computer:\n", " def __init__(self, cost, year, cpu_speed, cpu_type):\n", " self.cost = cost\n", " self.year = year\n", " self.cpu_speed = cpu_speed\n", " self.cpu_type = cpu_type\n", "def DataReceiver(s):\n", " cpu_type = raw_input(\"The type of CPU inside your computer: \")\n", " cpu_speed = int(raw_input(\"The speed(MHz) of the CPU?: \"))\n", " year = int(raw_input(\"The year your computer was made?: \"))\n", " cost = float(raw_input(\"How much you paid for the computer?: \"))\n", " s = computer(cost,year,cpu_speed,cpu_type)\n", " return s\n", "if __name__ == '__main__':\n", " model = DataReceiver(model)\n", " print \"Hear are what you have entered\"\n", " print \"Year: \",model.year,\"\\n\"\n", " print \"Cost: \",model.cost,\"\\n\"\n", " print \"CPU type: \",model.cpu_type,\"\\n\"\n", " print \"CPU speed: \",model.cpu_speed ,\"MHz\" " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The type of CPU inside your computer: Pentium\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The speed(MHz) of the CPU?: 100\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The year your computer was made?: 1996\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "How much you paid for the computer?: 1234.56\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Hear are what you have entered\n", "Year: 1996 \n", "\n", "Cost: 1234.56 \n", "\n", "CPU type: Pentium \n", "\n", "CPU speed: 100 MHz\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 19.4, Page No 322" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# There is no pointer in python\n", "class computer:\n", " def __init__(self, cost, year, cpu_speed, cpu_type):\n", " self.cost = cost\n", " self.year = year\n", " self.cpu_speed = cpu_speed\n", " self.cpu_type = cpu_type\n", "def DataReceiver(s):\n", " cpu_type = raw_input(\"The type of CPU inside your computer: \")\n", " cpu_speed = int(raw_input(\"The speed(MHz) of the CPU?: \"))\n", " year = int(raw_input(\"The year your computer was made?: \"))\n", " cost = float(raw_input(\"How much you paid for the computer?: \"))\n", " s = computer(cost,year,cpu_speed,cpu_type)\n", " return s\n", "if __name__ == '__main__':\n", " model = DataReceiver(model)\n", " print \"Hear are what you have entered\"\n", " print \"Year: \",model.year,\"\\n\"\n", " print \"Cost: \",model.cost,\"\\n\"\n", " print \"CPU type: \",model.cpu_type,\"\\n\"\n", " print \"CPU speed: \",model.cpu_speed ,\"MHz\" " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The type of CPU inside your computer: Pentium\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The speed(MHz) of the CPU?: 100\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The year your computer was made?: 1996\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "How much you paid for the computer?: 1234.56\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Hear are what you have entered\n", "Year: 1996 \n", "\n", "Cost: 1234.56 \n", "\n", "CPU type: Pentium \n", "\n", "CPU speed: 100 MHz\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 19.5, Page No 325" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class haiku:\n", " def __init__(self,start_year,end_year,author,str1,str2,str3):\n", " self.start_year = start_year\n", " self.end_year = end_year\n", " self.author = author\n", " self.str1 = str1\n", " self.str2 = str2\n", " self.str3 = str3\n", "def DataDisplay(ptr_s):\n", " print ptr_s.str1\n", " print ptr_s.str2\n", " print ptr_s.str3\n", " print \" ---\",ptr_s.author\n", " print \"(\",ptr_s.start_year,\",\",ptr_s.end_year,\")\"\n", "if __name__ == '__main__':\n", " poem = []\n", " poem.append(haiku(1641,1716,\"Sodo\",\"Leading me along\",\"my shadow goes back home\",\"from looking at the moon\"))\n", " poem.append(haiku(1729,1781,\"Chora\",\"A strom wind blows\",\"out from among the grasses\",\"the full moon grows\"))\n", " for i in range(len(poem)):\n", " DataDisplay(poem[i])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Leading me along\n", "my shadow goes back home\n", "from looking at the moon\n", " --- Sodo\n", "( 1641 , 1716 )\n", "A strom wind blows\n", "out from among the grasses\n", "the full moon grows\n", " --- Chora\n", "( 1729 , 1781 )\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 19.6, Page No 327" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class department:\n", " def __init__(self,code,dname,position):\n", " self.code = code\n", " self.dname = dname\n", " self.position = position\n", "class employee:\n", " def __init__(self,code,dname,position,id1,name):\n", " self.DPT = department(code,dname,position)\n", " self.id1 = id1\n", " self.name = name\n", "def InfoDisplay(ptr):\n", " print \"Name: \",ptr.name,\"\\n\"\n", " print \"ID #: \",ptr.id1,\"\\n\"\n", " print \"Dept. name: \",ptr.DPT.dname,\"\\n\"\n", " print \"Dept. code: \",ptr.DPT.code,\"\\n\"\n", " print \"Your Position : \",ptr.DPT.position,\"\\n\"\n", "def InfoEnter(ptr):\n", " print \"Please enter your information:\\n\"\n", " name = raw_input(\"Your name: \")\n", " position = raw_input(\"Your position: \")\n", " name = raw_input(\"Dept. name: \")\n", " code = raw_input(\"Dept. code: \")\n", " id1 = raw_input(\"Your employee ID #: \")\n", "if __name__ == '__main__':\n", " info = employee(\"1\",\"marketing\",\"manager\",1,\"B.Smith\")\n", " print \"\\nHere is a sample\"\n", " InfoDisplay(info)\n", " InfoEnter(info)\n", " print \"\\nHere are what you entered\"\n", " InfoDisplay(info)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " \n", "Here is a sample\n", "Name: B.Smith \n", "\n", "ID #: 1 \n", "\n", "Dept. name: marketing \n", "\n", "Dept. code: 1 \n", "\n", "Your Position : manager \n", "\n", "Please enter your information:\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Your name: T. Zhang\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Your position: Engineer\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Dept. name: R&D\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Dept. code: 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Your employee ID #: 1234\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Here are what you entered\n", "Name: B.Smith \n", "\n", "ID #: 1 \n", "\n", "Dept. name: marketing \n", "\n", "Dept. code: 1 \n", "\n", "Your Position : manager \n", "\n" ] } ], "prompt_number": 14 } ], "metadata": {} } ] }