# Hour 19 : Understanding Structures¶

## Example 19.1, Page No 316¶

In [6]:
class computer:
def __init__(self, cost, year, cpu_speed, cpu_type):
self.cost = cost
self.year = year
self.cpu_speed = cpu_speed
self.cpu_type = cpu_type
if __name__ == '__main__':
cpu_type = raw_input("The type of CPU inside your computer: ")
cpu_speed = int(raw_input("The speed(MHz) of the CPU?: "))
cost = float(raw_input("How much you paid for the computer?: "))
computer(cost,year,cpu_speed,cpu_type)
print "Hear are what you have entered"
print "Year: ",year,"\n"
print "Cost: ",cost,"\n"
print "CPU type: ",cpu_type,"\n"
print "CPU speed: ",cpu_speed,"MHz"

The type of CPU inside your computer: Pentium
The speed(MHz) of the CPU?: 100
How much you paid for the computer?: 1234.56
Hear are what you have entered
Year:  1996

Cost:  1234.56

CPU type:  Pentium

CPU speed:  100 MHz


## Example 19.2, Page No 318¶

In [1]:
class employee:
def __init__(self,id1,name):
self.id1 = id1
self.name = name
if __name__ ==   '__main__':
info = employee(1,"B.Smith")
print "Here is a sample: \n"
print "Employee Name : ",info.name,"\n"
print "Employee ID : ",info.id1
info.name = raw_input("What's your name?: ")
info.id1 = int(raw_input("What's your ID number: "))
print "\nHere are what you entered: \n"
print "Name: ",info.name,"\n"
print "Id: ",info.id1,"\n"

Here is a sample:

Employee Name :  B.Smith

Employee ID :  1

Here are what you entered:

Name:  T. Zhang

Id:  1234



## Example 19.3, Page No 320¶

In [7]:
class computer:
def __init__(self, cost, year, cpu_speed, cpu_type):
self.cost = cost
self.year = year
self.cpu_speed = cpu_speed
self.cpu_type = cpu_type
cpu_type = raw_input("The type of CPU inside your computer: ")
cpu_speed = int(raw_input("The speed(MHz) of the CPU?: "))
cost = float(raw_input("How much you paid for the computer?: "))
s = computer(cost,year,cpu_speed,cpu_type)
return s
if __name__ == '__main__':
print "Hear are what you have entered"
print "Year: ",model.year,"\n"
print "Cost: ",model.cost,"\n"
print "CPU type: ",model.cpu_type,"\n"
print "CPU speed: ",model.cpu_speed ,"MHz"

The type of CPU inside your computer: Pentium
The speed(MHz) of the CPU?: 100
How much you paid for the computer?: 1234.56
Hear are what you have entered
Year:  1996

Cost:  1234.56

CPU type:  Pentium

CPU speed:  100 MHz


## Example 19.4, Page No 322¶

In [8]:
# There is no pointer in python
class computer:
def __init__(self, cost, year, cpu_speed, cpu_type):
self.cost = cost
self.year = year
self.cpu_speed = cpu_speed
self.cpu_type = cpu_type
cpu_type = raw_input("The type of CPU inside your computer: ")
cpu_speed = int(raw_input("The speed(MHz) of the CPU?: "))
cost = float(raw_input("How much you paid for the computer?: "))
s = computer(cost,year,cpu_speed,cpu_type)
return s
if __name__ == '__main__':
print "Hear are what you have entered"
print "Year: ",model.year,"\n"
print "Cost: ",model.cost,"\n"
print "CPU type: ",model.cpu_type,"\n"
print "CPU speed: ",model.cpu_speed ,"MHz"

The type of CPU inside your computer: Pentium
The speed(MHz) of the CPU?: 100
How much you paid for the computer?: 1234.56
Hear are what you have entered
Year:  1996

Cost:  1234.56

CPU type:  Pentium

CPU speed:  100 MHz


## Example 19.5, Page No 325¶

In [10]:
class haiku:
def __init__(self,start_year,end_year,author,str1,str2,str3):
self.start_year = start_year
self.end_year = end_year
self.author = author
self.str1 = str1
self.str2 = str2
self.str3 = str3
print ptr_s.str1
print ptr_s.str2
print ptr_s.str3
print " ---",ptr_s.author
print "(",ptr_s.start_year,",",ptr_s.end_year,")"
if __name__ == '__main__':
poem = []
poem.append(haiku(1729,1781,"Chora","A strom wind blows","out from among the grasses","the full moon grows"))
for i in range(len(poem)):

Leading me along
from looking at the moon
--- Sodo
( 1641 , 1716 )
A strom wind blows
out from among the grasses
the full moon grows
--- Chora
( 1729 , 1781 )


## Example 19.6, Page No 327¶

In [14]:
class department:
def __init__(self,code,dname,position):
self.code = code
self.dname = dname
self.position = position
class employee:
def __init__(self,code,dname,position,id1,name):
self.DPT = department(code,dname,position)
self.id1 = id1
self.name = name
def InfoDisplay(ptr):
print "Name: ",ptr.name,"\n"
print "ID #: ",ptr.id1,"\n"
print "Dept. name: ",ptr.DPT.dname,"\n"
print "Dept. code: ",ptr.DPT.code,"\n"
def InfoEnter(ptr):
name = raw_input("Dept. name: ")
code = raw_input("Dept. code: ")
id1 = raw_input("Your employee ID #: ")
if __name__ == '__main__':
info = employee("1","marketing","manager",1,"B.Smith")
print "\nHere is a sample"
InfoDisplay(info)
InfoEnter(info)
print "\nHere are what you entered"
InfoDisplay(info)


Here is a sample
Name:  B.Smith

ID #:  1

Dept. name:  marketing

Dept. code:  1

Dept. name: R&D
Dept. code: 3