Chapter 10: Structures and Unions

Example 1, Page number: SAU - 5

In [3]:
#student structure

import sys

class student:
    rno = 0
    sname = ""
    tot = 0 
    
x = student()

x.rno = int(raw_input("\nEnter roll number, name and total marks"))
x.sname = raw_input("")
x.tot = int(raw_input(""))

sys.stdout.write("\n\tDetails entered are \n")
sys.stdout.write("\nRoll No.     :  %d"%(x.rno))
sys.stdout.write("\nStudent Name :  %s"%(x.sname))
sys.stdout.write("\nTotal marks  :  %d"%(x.tot))
sys.stdout.write("\n\nPress any key to continue...")
Enter roll number, name and total marks20147
PRADEEP
64

	Details entered are 

Roll No.     :  20147
Student Name :  PRADEEP
Total marks  :  64

Press any key to continue...

Example 2, Page number: SAU - 6

In [5]:
import sys

class student:
    rno = 0 
    sname = ""
    tot = 0
    
ch = 'y'
std = [student() for i in range(0,50)]

n = int(raw_input("\nHow many students ? "))

for i in range(0,n):
    std[i].rno = int(raw_input("\nRoll Number ? "))
    std[i].sname = raw_input("\nName ? ")
    std[i].tot = int(raw_input("\nTotal marks ? "))
    
while ch == 'y' or ch == 'Y':
    temp = int(raw_input("\nEnter student roll number to display marks : "))
    flag  = 0
    for i in range(0,n):
        if flag == 0:
            if std[i].rno == temp:
                sys.stdout.write("\nMarks obtained by %d  %s"%(std[i].rno,std[i].sname))
                sys.stdout.write("\nTotal   :  %d"%(std[i].tot))
                flag = 1
                
    if flag == 0 :
        sys.stdout.write("\n%d is not present in the list"%(temp))
    ch = raw_input("\npress y - to continue \n any other key to stop.")
How many students ? 3

Roll Number ? 20201

Name ? ARUN

Total marks ? 78

Roll Number ? 20208

Name ? DEEPAK

Total marks ? 69

Roll Number ? 20223

Name ? SUSMITHA

Total marks ? 88

Enter student roll number to display marks : 20208

Marks obtained by 20208  DEEPAK
Total   :  69
press y - to continue 
 any other key to stop.N

Example 3, Page number: SAU - 8

In [7]:
import sys

class student:
    rno = 0 
    sname = ""
    tot = 0
    
std = [student() for i in range(0,50)]

n = int(raw_input("\nHow many students ? "))

for i in range(0,n):
    std[i].rno = int(raw_input("\nRoll Number ? "))
    std[i].sname = raw_input("\nName ? ")
    std[i].tot = int(raw_input("\nTotal marks ? "))
    
print "\n-------------------------------------------------------"
print "\n     Roll No.          Name             Total Marks"
print "\n-------------------------------------------------------"

for i in range(0,n):
    if std[i].tot >= 75:
        sys.stdout.write("\n   %-8d        %-20s  %5d"%(std[i].rno,std[i].sname,std[i].tot))
        
print "\n-------------------------------------------------------"
How many students ? 4

Roll Number ? 30401

Name ? ANAND

Total marks ? 59

Roll Number ? 30404

Name ? NIRMAL

Total marks ? 64

Roll Number ? 30428

Name ? ISWARYA

Total marks ? 82

Roll Number ? 30432

Name ? VIVEKA

Total marks ? 79

-------------------------------------------------------

     Roll No.          Name             Total Marks

-------------------------------------------------------

   30428           ISWARYA                  82
   30432           VIVEKA                   79
-------------------------------------------------------

Example 4, Page number: SAU - 10

In [17]:
import sys

class employee:
    eno = 0
    ename = ""
    epay = 0
    jdate = ""
    
def revise(temp):
    if temp <= 2000:
        return (temp + (temp*0.15))
    else:
        if temp <= 5000:
            return (temp + (temp * 0.10))
        else:
            return temp
        
    
employs = [employee() for i in range(0,25)]
i = 0
ch = 'y'

while ch.upper() == 'Y':
    employs[i].eno = int(raw_input("\nEmployee No.? "))
    employs[i].ename = raw_input("\nName ? ")
    employs[i].epay = int(raw_input("\nExisting pay? "))
    employs[i].jdate = raw_input("\nJoining Date ? ")
    i += 1
    ch = raw_input("\nPress y - to continue any other key to stop.")
    
n = i
sys.stdout.write("\n\n%d records are entered"%(n))
sys.stdout.write("\nPress any key to print the revised salary list")

for i in range(0,n):
    employs[i].epay = revise(employs[i].epay)
    
print "\n   Employees Revised Pay List"
print "\n----------------------------------------------------------------------------------"
print "\n   S.No        Number         Name             Joinint date        Pay"
print "\n----------------------------------------------------------------------------------"

for i in range(0,n):
    sys.stdout.write("\n     %-4d      %-d      %-15s        %10s          %-8d"%(i+1,employs[i].eno,employs[i].ename,employs[i].jdate,employs[i].epay))
    
print "\n----------------------------------------------------------------------------------"
Employee No.? 20101

Name ? ASHIKA

Existing pay? 1000

Joining Date ? 31/04/2001

Press y - to continue any other key to stop.Y

Employee No.? 20182

Name ? ASHWIN

Existing pay? 6000

Joining Date ? 11/12/1995

Press y - to continue any other key to stop.Y

Employee No.? 20204

Name ? PRAVEEN

Existing pay? 3000

Joining Date ? 18/06/1994

Press y - to continue any other key to stop.N


3 records are entered
Press any key to print the revised salary list
   Employees Revised Pay List

----------------------------------------------------------------------------------

   S.No        Number         Name             Joinint date        Pay

----------------------------------------------------------------------------------

     1         20101      ASHIKA                 31/04/2001          1150    
     2         20182      ASHWIN                 11/12/1995          6000    
     3         20204      PRAVEEN                18/06/1994          3300    
----------------------------------------------------------------------------------

Example 5, Page number: SAU - 13

In [3]:
import sys

class cricket:
    pname =  ""
    tname = ""
    bavg = 0
    
probable = [cricket() for i in range(0,30)]

n = int(raw_input("\nHow many players ? "))

for i in range(0,n):
    probable[i].pname = raw_input("\nPlayer name ? ")
    probable[i].tname = raw_input("\nWhich team ? ")
    probable[i].bavg = int(raw_input("\nBatting average ? "))
    
j = 0
teams = ['' for i in range(0,20)]

teams[j] = probable[0].tname
j += 1
i = 1

for i in range(1,n):
    flag = 0
    k = 0
    while k < j and flag == 0:    
        if probable[i].tname == teams[k]:
            flag = 1
        k += 1
    if flag == 0:
        teams[j] = probable[i].tname
        j += 1
    i += 1

for k in range(0,j):
    sys.stdout.write("\n\t%s"%(teams[k]))
    print "\n-------------------------------------------------------"
    for i in range(0,n):
        if probable[i].tname == teams[k]:
            sys.stdout.write("\n  %-15s     %6d"%(probable[i].pname,probable[i].bavg))
    print "\n-------------------------------------------------------"
How many players ? 6

Player name ? KUMBLE

Which team ? KARNATAKA

Batting average ? 22

Player name ? KAMBLI

Which team ? MUMBAI

Batting average ? 39

Player name ? SRIKANTH

Which team ? TAMILNADU

Batting average ? 52

Player name ? SACHIN

Which team ? MUMBAI

Batting average ? 69

Player name ? RAHUL

Which team ? KARNATAKA

Batting average ? 57

Player name ? RAMESH

Which team ? TAMILNADU

Batting average ? 48

	KARNATAKA
-------------------------------------------------------

  KUMBLE                  22
  RAHUL                   57
-------------------------------------------------------

	MUMBAI
-------------------------------------------------------

  KAMBLI                  39
  SACHIN                  69
-------------------------------------------------------

	TAMILNADU
-------------------------------------------------------

  SRIKANTH                52
  RAMESH                  48
-------------------------------------------------------

Example 6, Page number: SAU - 19

In [3]:
import sys

class student:
    roll_no = 0
    sname = ""
    marks = 0 
 
std = [student() for i in range(0,10)]

print "\n-----------------------------------------------------------------------"
print "\n                        Main Menu"
print "\n-----------------------------------------------------------------------"
print "\nPress 1 to enter roll numbers"
print "\n      2 to enter names"
print "\n      3 to enter marks"
print "\n      4 to stop"
ch = int(raw_input("\nEnter your choice : "))
if ch < 4:
    n = int(raw_input("How many students ? "))

    for i in range(0,n):
        if ch == 1:
            std[i].roll_no = int(raw_input("\nRoll number ? "))
        else:
            if ch == 2:
                std[i].sname = raw_input("\nStudent name ? ")
            else:
                if ch == 3:
                    std[i].marks = int(raw_input("\nAverage marks ? "))
                
    if ch == 1:
        print "\n-------------------------------------------------------------------"
        print "\n                        Students roll number list"
        print "\n-------------------------------------------------------------------"
        for i in range(0,n):
            sys.stdout.write("\n\t%d"%(std[i].roll_no))
        print "\n-------------------------------------------------------------------"
    else:
        if ch == 2:
            print "\n-------------------------------------------------------------------"
            print "\n                        Students name list"
            print "\n-------------------------------------------------------------------"
            for i in range(0,n):
                sys.stdout.write("\n\t%s"%(std[i].sname))
            print "\n-------------------------------------------------------------------"
        else:
            if ch == 3:
                print "\n-------------------------------------------------------------------"
                print "\n                        Students mark list"
                print "\n-------------------------------------------------------------------"
                for i in range(0,n):
                    sys.stdout.write("\n\t%d"%(std[i].marks))
                print "\n-------------------------------------------------------------------"
-----------------------------------------------------------------------

                        Main Menu

-----------------------------------------------------------------------

Press 1 to enter roll numbers

      2 to enter names

      3 to enter marks

      4 to stop

Enter your choice : 2
How many students ? 4

Student name ? AJITH

Student name ? RAJU

Student name ? VIGNESH

Student name ? DIVYA

-------------------------------------------------------------------

                        Students name list

-------------------------------------------------------------------

	AJITH
	RAJU
	VIGNESH
	DIVYA
-------------------------------------------------------------------
In [ ]: