# Chapter 9 : Introduction to Data Structures¶

## Example 9.1, Page No 205¶

In [2]:
class employee:
def __init__(self,fname,lname,id1,salary):
self.fname = fname
self.lname = lname
self.id1 = id1
self.salary = salary
if __name__ == '__main__':
emp1 = employee("Michael","Vine",123,50000.00)
print "\n First name: " + emp1.fname + "\n"
print "Last name: " + emp1.lname + "\n"
print "Employee ID: " + str(emp1.id1) + "\n"
print "Salary: " + str(emp1.salary) + "\n"

 First name: Michael

Last name: Vine

Employee ID: 123

Salary: 50000.0



## Example 9.2, Page No 206¶

In [3]:
class employee:
def __init__(self,fname,lname,id1,salary):
self.fname = fname
self.lname = lname
self.id1 = id1
self.salary = salary
if __name__ == '__main__':
emp1 = employee("Michael","Vine",123,50000.00)
print "\n First name: " + emp1.fname + "\n"
print "Last name: " + emp1.lname + "\n"
print "Employee ID: " + str(emp1.id1) + "\n"
print "Salary: " + str(emp1.salary) + "\n"

 First name: Michael

Last name: Vine

Employee ID: 123

Salary: 50000.0



## Example 9.3, Page No 209¶

In [1]:
class scores:
def __init__(self,name,score):
self.name = name
self.score = score
if __name__ == '__main__':
highScores = []
highScores.append(scores("Hunter",40768))
highScores.append(scores("Kenya",38568))
highScores.append(scores("Apollo",35985))
for x in range(3):
print "\n" + highScores[x].name + "\t" + str(highScores[x].score) + "\n"

Hunter	40768

Kenya	38568

Apollo	35985



## Example 9.4, Page No 211¶

In [3]:
class employee:
def __init__(self,id1,name,salary):
self.id1 = id1
self.name = name
self.salary = salary
def processEmp(emp1):
emp1.id1 = 123
emp1.name = "Sheila"
emp1.salary = 65000.00
if __name__ == '__main__':
emp1 = employee(0,0,0)
processEmp(emp1)
print "\n Id : " + str(emp1.id1) + "\n"
print "Name : " + emp1.name + "\n"
print "Salary : " + str(emp1.salary) + "\n"

 Id : 123

Name : Sheila

Salary : 65000.0



## Example 9.5, Page No 212¶

In [5]:
class player:
def __init__(self,name,score):
self.name = name
self.score = score
if __name__ == '__main__':
aPlayer = player(0,0)
ptrPlayer = id(aPlayer)
aPlayer.name = "Pinball Wizard"
aPlayer.score = 1000000.00
print "\nPlayer : " + aPlayer.name + "\n"
print "Score : " + str(aPlayer.score)

Player : Pinball Wizard

Score : 1000000.0


## Example 9.6, Page No 213¶

In [6]:
class employee:
def __init__(self,id1,name,salary):
self.id1 = id1
self.name = name
self.salary = salary
def processEmp(emp1):
emp1.id1 = 123
emp1.name = "Sheila"
emp1.salary = 65000.00
if __name__ == '__main__':
emp1 = employee(0,0,0)
ptrEmp = id(emp1)
processEmp(emp1)
print "\nId : " + str(emp1.id1) + "\n"
print "Name : " + emp1.name + "\n"
print "Salary : " + str(emp1.salary) + "\n"

Id : 123

Name : Sheila

Salary : 65000.0



## Example 9.7, Page No 215¶

In [10]:
class employee:
def __init__(self,id1,name,salary):
self.id1 = id1
self.name = name
self.salary = salary
def processEmp(emp1 = []):
emp1.append(employee(123,"Sheila",65000.00))
emp1.append(employee(234,"Hunter",28000.00))
emp1.append(employee(456,"Kenya",48000.00))
return emp1
if __name__ == '__main__':
emp1 = []
emp1 = employee(0,0,0)
emp1 = processEmp(emp1 = [])
for x in range(3):
print "\nId : " + str(emp1[x].id1) + "\n"
print "Name : " + emp1[x].name + "\n"
print "Salary : " + str(emp1[x].salary)

Id : 123

Name : Sheila

Salary : 65000.0

Id : 234

Name : Hunter

Salary : 28000.0

Id : 456

Name : Kenya

Salary : 48000.0


## Example 9.8, Page No 217¶

In [10]:
class phonebook:
self.name = name
self.number = number
class magazine:
def __init__(self,name,author,isbn):
self.name = name
self.author = author
self.isbn = isbn
if __name__ == '__main__':
aBook = phonebook("John",123,"John Smith")
aMagazine = magazine("C Programming","Michael Vine",1-59863-480-1)
print "\nUnion Details\n"
print "Address for aBook.name: " +  str(id(aBook.name)) + "\n"
print "Address for aBook.number: ", str(id(aBook.number)) + "\n"
print "\nStructure Details\n"
print "Address for aMagazine.name: ", str(id(aMagazine.name)) + "\n"
print "Address for aMagazine.author: ", str(id(aMagazine.author)) + "\n"
print "Address for aMagazine.isbn: ", str(id(aMagazine.isbn)) + "\n"


Union Details

Structure Details



## Example 9.9, Page No 219¶

In [11]:
x = 12
y = 5
print "\nWithout Type-Casting\n"
print "12 \\ 5 = " + str(x/y) + "\n"
print "\nWith Type-Casting\n"
print "12 \\ 5 = " + str(float(x)/float(y)) + "\n"

Without Type-Casting

12 \ 5 = 2

With Type-Casting

12 \ 5 = 2.4



## Example 9.10, Page No 220¶

In [6]:
number = 86
letter = 'M'
print "\n86 type-casted to char is: " + chr(number) + "\n"
print "\n'M' type-casted to int is: " + str(ord(letter)) + "\n "

86 type-casted to char is: V

'M' type-casted to int is: 77



## Example 9.11, Page No 222¶

In [1]:
# This program is not possible in python.