{ "metadata": { "name": "", "signature": "sha256:03a9be4b5720a51a42be3ae01bfa08ccecef76e9a5e17f613e76c0afaff965de" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 11 : Structures" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 1, Page No.: 5.81" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Print the student number, name and marks through accessing structure elements.\n", "\n", "#Defining Structure in class\n", "class std:\n", " no=0\n", " name = ''\n", " m1 = m2 = m3 =0\n", "\n", "#assigning variable to structure class \n", "s=std()\n", "\n", "#UserInput\n", "s.no=raw_input(\"Give Student Number: \")\n", "s.name=raw_input(\"Enter the Student Name: \")\n", "s.m1=input(\"Enter Student Mark1: \")\n", "s.m2=input(\"Enter Student Mark2: \")\n", "s.m3=input(\"Enter Student Mark3: \")\n", "\n", "total=avg=0.0\n", "\n", "total = (s.m1 + s.m2 + s.m3)\n", "avg='%.2f' %(total / 3)\n", "\n", "#Result\n", "print \"\\nThe Output is.... \\n\",s.no,s.name,total,avg" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give Student Number: 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Student Name: raja\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Student Mark1: 99\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Student Mark2: 97\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Student Mark3: 95\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The Output is.... \n", "1 raja 291 97.00\n" ] } ], "prompt_number": 39 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 2, Page No. 5.83" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#print a value from Structure.\n", "\n", "class struct:\n", " a=b=0\n", " \n", "x=struct()\n", "y=struct()\n", "\n", "x.a=29\n", "y=x\n", "\n", "#Result\n", "print \"The Value of a %d\"%y.a\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The Value of a 29\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 3, Page No.:5.84" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program for User defined data type \n", "\n", "#There is no preprocessor directive in python\n", "D = 7\n", "\n", "#No typedef function in python\n", "wk = int(raw_input(\"Enter Weeks : \"))\n", "\n", "#Result\n", "print \"\\nNumber of Days = %d\"%(wk*D)\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Weeks : 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Number of Days = 28\n" ] } ], "prompt_number": 55 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 4, Page.No.:5.85" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to create user defined data type from structure\n", "\n", "\n", "class struct:\n", " name=''\n", " sex=''\n", " age=0\n", " \n", "\n", "\n", "candidate = [struct() for i in range(0,2)]\n", "\n", "for a in range(0,2):\n", " candidate[a].name= raw_input(\"Name of the Employee: \")\n", " candidate[a].sex= raw_input(\"Sex: \")\n", " candidate[a].age= input(\"Age: \")\n", "\n", "#Result\n", "print \"\\n Name\\t Sex\\t Age\\n\"\n", "\n", "for a in range(0,2):\n", " print \" %s\\t\"%(candidate[a].name),\" %s\\t\"%(candidate[a].sex),\" %d\\n\"%(candidate[a].age)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Name of the Employee: venkat\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Sex: m\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Age: 25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Name of the Employee: geetha\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Sex: f\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Age: 23\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", " Name\t Sex\t Age\n", "\n", " venkat\t m\t 25\n", "\n", " geetha\t f\t 23\n", "\n" ] } ], "prompt_number": 68 } ], "metadata": {} } ] }