{ "metadata": { "name": "", "signature": "sha256:93c2825491a65ebd6dbc85e6208fc8a03cf21f92da06cf2457eda56a011645f1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 1 , Page number: CP-211" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to display student details\n", "# class declaration instead of a structure\n", "\n", "class student:\n", " rno = 0\n", " sname = \"\"\n", " tot = 0\n", "\n", "# class object variable\n", "\n", "x = student()\n", "x.rno = 20147\n", "x.sname = \"PRADEEP\"\n", "x.tot = 64\n", "\n", "print \"Enter roll number, name and total marks\"\n", "print x.rno,x.sname,x.tot\n", "print \" Details entered are\"\n", "print \"Roll No. :\",x.rno\n", "print \"Student name :\",x.sname\n", "print \"Total marks :\",x.tot\n", "print \"Press any key to continue. . .\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter roll number, name and total marks\n", "20147 PRADEEP 64\n", " Details entered are\n", "Roll No. : 20147\n", "Student name : PRADEEP\n", "Total marks : 64\n", "Press any key to continue. . .\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 2 , Page number: CP-212" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to input student details and print the marks of a specified student as output\n", "\n", "# class declaration for structure\n", "\n", "class student:\n", " def __init__(self,rno,sname,tot):\n", " self.rno = rno\n", " self.sname = sname\n", " self.tot = tot\n", " \n", "# variable declaration\n", "\n", "ch = \"y\"\n", "n = 3\n", "\n", "# details of n students\n", "std = []\n", "std.append(student(20201,\"ARUN\",78))\n", "std.append(student(20208,\"DEEPAK\",69))\n", "std.append(student(20223,\"SUSMITHA\",88))\n", "\n", "print \"How many students ?\",n\n", "print \"Roll number ?\",std[0].rno\n", "print \"Name ?\",std[0].sname\n", "print \"Total marks ?\",std[0].tot\n", "print \"Roll number ?\",std[1].rno\n", "print \"Name ?\",std[1].sname\n", "print \"Total marks ?\",std[1].tot\n", "print \"Roll number ?\",std[2].rno\n", "print \"Name ?\",std[2].sname\n", "print \"Total marks ?\",std[2].tot\n", "print \n", "\n", "\n", "# To display marks of the student\n", "while ch == \"y\" or ch == \"Y\":\n", " temp = 20208\n", " print \"Enter student roll number to display marks :\",temp\n", " print \n", " flag = 0\n", " #loop to search and display details\n", " for i in range(3):\n", " if flag == 0:\n", " if std[i].rno == temp:\n", " print \"Marks obtained by \",std[i].rno,std[i].sname\n", " print \"Total :\",std[i].tot\n", " flag = 1\n", " if flag == 0:\n", " print temp,\" is not present in the list \"\n", " \n", " ch = \"n\" \n", " print \"press y - to continue\"\n", " print \" any other key to stop.\",ch\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many students ? 3\n", "Roll number ? 20201\n", "Name ? ARUN\n", "Total marks ? 78\n", "Roll number ? 20208\n", "Name ? DEEPAK\n", "Total marks ? 69\n", "Roll number ? 20223\n", "Name ? SUSMITHA\n", "Total marks ? 88\n", "\n", "Enter student roll number to display marks : 20208\n", "\n", "Marks obtained by 20208 DEEPAK\n", "Total : 69\n", "press y - to continue\n", " any other key to stop. n\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3 , Page number: CP-214" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to declare a structure for student details and display list of students who obtained more than 75 marks\n", "\n", "# class for student structure\n", "\n", "class student:\n", " def __init__(self,rno,sname,tot):\n", " self.rno = rno\n", " self.sname = sname\n", " self.tot = tot\n", "\n", "std = []\n", "std.append(student(30401,\"ANAND\",59))\n", "std.append(student(30404,\"NIRMAL\",64))\n", "std.append(student(30428,\"ISWARYA\",82))\n", "std.append(student(30432,\"VIVEKA\",79))\n", "\n", "n = 4\n", "print \"How many students ?\",n\n", "print \"Roll Number ?\",std[0].rno\n", "print \"Name ?\",std[0].sname\n", "print \"Total marks ?\",std[0].tot\n", "print \"Roll Number ?\",std[1].rno\n", "print \"Name ?\",std[1].sname\n", "print \"Total marks ?\",std[1].tot\n", "print \"Roll Number ?\",std[2].rno\n", "print \"Name ?\",std[2].sname\n", "print \"Total marks ?\",std[2].tot\n", "print \"Roll Number ?\",std[3].rno\n", "print \"Name ?\",std[3].sname\n", "print \"Total marks ?\",std[3].tot\n", "print \n", "\n", "\n", "print \"----------------------------------------------------\"\n", "print \" Roll No. Name Total marks \"\n", "print \"----------------------------------------------------\"\n", "for i in range(n):\n", " if std[i].tot >= 75:\n", " print \" \",std[i].rno,\" \",std[i].sname,\" \",std[i].tot\n", "\n", "print \"----------------------------------------------------\"\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many students ? 4\n", "Roll Number ? 30401\n", "Name ? ANAND\n", "Total marks ? 59\n", "Roll Number ? 30404\n", "Name ? NIRMAL\n", "Total marks ? 64\n", "Roll Number ? 30428\n", "Name ? ISWARYA\n", "Total marks ? 82\n", "Roll Number ? 30432\n", "Name ? VIVEKA\n", "Total marks ? 79\n", "\n", "----------------------------------------------------\n", " Roll No. Name Total marks \n", "----------------------------------------------------\n", " 30428 ISWARYA 82\n", " 30432 VIVEKA 79\n", "----------------------------------------------------\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4 , Page number: CP-216" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to store employee information and to compute employee's pay\n", "\n", "import math\n", "# class declaration for employee\n", "class employee:\n", " def __init__(self,eno,ename,epay,jdate):\n", " self.eno = eno\n", " self.ename = ename\n", " self.epay = epay\n", " self.jdate = jdate\n", "\n", "employs = []\n", "employs.append(employee(20101,\"ASHIKA\",1000,\"31/04/2001\"))\n", "employs.append(employee(20182,\"ASHWIN\",6000,\"11/12/1995\"))\n", "employs.append(employee(20204,\"PRAVEEN\",3000,\"18/06/1994\"))\n", "\n", "n = 3\n", "\n", "print \"Employee No. ?\",employs[0].eno\n", "print \"Name ?\",employs[0].ename\n", "print \"Existing date ?\",employs[0].epay\n", "print \"Joinin date ?\",employs[0].jdate\n", "print \n", "print \"Press y- to continue any other key to stop. y\"\n", "print \"Employee No. ?\",employs[1].eno\n", "print \"Name ?\",employs[1].ename\n", "print \"Existing date ?\",employs[1].epay\n", "print \"Joinin date ?\",employs[1].jdate\n", "print\n", "print \"Press y- to continue any other key to stop. y\"\n", "print \"Employee No. ?\",employs[2].eno\n", "print \"Name ?\",employs[2].ename\n", "print \"Existing date ?\",employs[2].epay\n", "print \"Joinin date ?\",employs[2].jdate\n", "print\n", "print \"Press y- to continue any other key to stop. N\"\n", "print\n", "print n,\" records are entered\"\n", "print \"Press any key to print the revised salary list\"\n", "print \n", "\n", "\n", "\n", "def revise(temp):\n", " if temp <= 2000:\n", " temp = int(temp + math.ceil(temp * 0.15))\n", " return temp\n", " elif temp <= 5000:\n", " temp = int(temp + temp * 0.10)\n", " return temp\n", " else:\n", " return temp\n", "\n", " \n", "# loop to increment salary\n", "for i in range(n):\n", " employs[i].epay = revise(employs[i].epay)\n", "\n", " \n", "\n", "\n", "# loop to print revised salary list\n", "print \" Employees Revised Pay List \"\n", "print \"---------------------------------------------------------\"\n", "print \" S.No. Number Name Joining date Pay \"\n", "print \"---------------------------------------------------------\"\n", "\n", "for i in range(n):\n", " print \" \",i+1,\" \",employs[i].eno,\" \",employs[i].ename,\" \",employs[i].jdate,\" \",employs[i].epay\n", "\n", "print \"---------------------------------------------------------\" \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Employee No. ? 20101\n", "Name ? ASHIKA\n", "Existing date ? 1000\n", "Joinin date ? 31/04/2001\n", "\n", "Press y- to continue any other key to stop. y\n", "Employee No. ? 20182\n", "Name ? ASHWIN\n", "Existing date ? 6000\n", "Joinin date ? 11/12/1995\n", "\n", "Press y- to continue any other key to stop. y\n", "Employee No. ? 20204\n", "Name ? PRAVEEN\n", "Existing date ? 3000\n", "Joinin date ? 18/06/1994\n", "\n", "Press y- to continue any other key to stop. N\n", "\n", "3 records are entered\n", "Press any key to print the revised salary list\n", "\n", " Employees Revised Pay List \n", "---------------------------------------------------------\n", " S.No. Number Name Joining date Pay \n", "---------------------------------------------------------\n", " 1 20101 ASHIKA 31/04/2001 1150\n", " 2 20182 ASHWIN 11/12/1995 6000\n", " 3 20204 PRAVEEN 18/06/1994 3300\n", "---------------------------------------------------------\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5 , Page number: CP-219" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to store cricket details and to display a team-wise list with batting average\n", "\n", "# class for cricket structure\n", "\n", "class cricket:\n", " def __init__(self,pname,tname,bavg):\n", " self.pname = pname\n", " self.tname = tname\n", " self.bavg = bavg\n", "\n", "n = 6\n", "probable = []\n", "probable.append(cricket(\"KUMBLE\",\"KARNATAKA\",22))\n", "probable.append(cricket(\"KAMBLI\",\"MUMBAI\",39))\n", "probable.append(cricket(\"SRIKANTH\",\"TAMILNADU\",52))\n", "probable.append(cricket(\"SACHIM\",\"MUMBAI\",69))\n", "probable.append(cricket(\"RAHUL\",\"KARNATAKA\",57))\n", "probable.append(cricket(\"RAMESH\",\"TAMILNADU\",48))\n", "\n", "print \"How many players ?\",n\n", "print\n", "print \"Player name ?\",probable[0].pname\n", "print \"Which team ?\",probable[0].tname\n", "print \"Batting average ?\",probable[0].bavg\n", "print \"Player name ?\",probable[1].pname\n", "print \"Which team ?\",probable[1].tname\n", "print \"Batting average ?\",probable[1].bavg\n", "print \"Player name ?\",probable[2].pname\n", "print \"Which team ?\",probable[2].tname\n", "print \"Batting average ?\",probable[2].bavg\n", "print \"Player name ?\",probable[3].pname\n", "print \"Which team ?\",probable[3].tname\n", "print \"Batting average ?\",probable[3].bavg\n", "print \"Player name ?\",probable[4].pname\n", "print \"Which team ?\",probable[4].tname\n", "print \"Batting average ?\",probable[4].bavg\n", "print \"Player name ?\",probable[5].pname\n", "print \"Which team ?\",probable[5].tname\n", "print \"Batting average ?\",probable[5].bavg\n", "print\n", "print \n", "j = 0\n", "teams = []\n", "teams.append(probable[0].tname)\n", "j = j + 1\n", "for i in range(n):\n", " flag = 0\n", " for k in range(j):\n", " if flag == 0:\n", " if probable[i].tname == teams[k]:\n", " flag = 1\n", " if flag == 0 :\n", " teams.append(probable[i].tname)\n", " j = j + 1\n", "\n", "# loop to print team-wise list\n", "\n", "for k in range(3):\n", " print \" \",teams[k]\n", " print \"---------------------------------------------\"\n", " for i in range(n):\n", " if probable[i].tname == teams[k]:\n", " print \" \",probable[i].pname,\" \",probable[i].bavg\n", " print \"---------------------------------------------\" \n", "\n", "\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many players ? 6\n", "\n", "Player name ? KUMBLE\n", "Which team ? KARNATAKA\n", "Batting average ? 22\n", "Player name ? KAMBLI\n", "Which team ? MUMBAI\n", "Batting average ? 39\n", "Player name ? SRIKANTH\n", "Which team ? TAMILNADU\n", "Batting average ? 52\n", "Player name ? SACHIM\n", "Which team ? MUMBAI\n", "Batting average ? 69\n", "Player name ? RAHUL\n", "Which team ? KARNATAKA\n", "Batting average ? 57\n", "Player name ? RAMESH\n", "Which team ? TAMILNADU\n", "Batting average ? 48\n", "\n", "\n", " KARNATAKA\n", "---------------------------------------------\n", " KUMBLE 22\n", " RAHUL 57\n", "---------------------------------------------\n", " MUMBAI\n", "---------------------------------------------\n", " KAMBLI 39\n", " SACHIM 69\n", "---------------------------------------------\n", " TAMILNADU\n", "---------------------------------------------\n", " SRIKANTH 52\n", " RAMESH 48\n", "---------------------------------------------\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6 , Page number: CP-235" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to illustrate the use of union using integer,string and float\n", "\n", "class student:\n", " def __init__(self,roll_no,sname,marks):\n", " self.roll_no = roll_no\n", " self.sname = sname\n", " self.marks = marks\n", "\n", "std = []\n", "std.append(student(0,\"AJITH\",0))\n", "std.append(student(0,\"RAJU\",0))\n", "std.append(student(0,\"VIGNESH\",0))\n", "std.append(student(0,\"DIVYA\",0))\n", "\n", "ch = 2\n", "print \"-----------------------------\"\n", "print \" Main menu \"\n", "print \"-----------------------------\"\n", "print \"Press 1 to enter roll numbers\"\n", "print \" 2 to enter names \"\n", "print \" 3 to enter marks \"\n", "print \" 4 to stop \"\n", "print \n", "print \"Enter your choice :\",ch\n", "\n", "n = 4\n", "print \"How many students?\",n\n", "for i in range(n):\n", " print \" Student name ? \",std[i].sname\n", "\n", "print\n", "print \n", "# display required list\n", "# switch case\n", "if ch == 1: #case 1\n", " print \"------------------------------\"\n", " print \" Students roll number list \"\n", " print \"------------------------------\"\n", " for i in range(n):\n", " print std[i].roll_no\n", " print \"-------------------------------\"\n", "elif ch == 2: # case 2\n", " print \"------------------------------\"\n", " print \" Students name list \"\n", " print \"------------------------------\"\n", " for i in range(n):\n", " print std[i].sname\n", " print \"-------------------------------\"\n", "elif ch == 3: # case 3\n", " print \"------------------------------\"\n", " print \" Students mark list \"\n", " print \"------------------------------\"\n", " for i in range(n):\n", " print \"Student marks\",std[i].roll_no\n", " print \"-------------------------------\"\n", " \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-----------------------------\n", " Main menu \n", "-----------------------------\n", "Press 1 to enter roll numbers\n", " 2 to enter names \n", " 3 to enter marks \n", " 4 to stop \n", "\n", "Enter your choice : 2\n", "How many students? 4\n", " Student name ? AJITH\n", " Student name ? RAJU\n", " Student name ? VIGNESH\n", " Student name ? DIVYA\n", "\n", "\n", "------------------------------\n", " Students name list \n", "------------------------------\n", "AJITH\n", "RAJU\n", "VIGNESH\n", "DIVYA\n", "-------------------------------\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }