Arrays of Records
Creating Arrays of Records
Method 1 (SQA Arrays)
#
bookList = [Book() for b in range(20)]
#
bookList[0].title = "XYZ"
bookList[0].year = 2001
bookList[0].price = 14.99
bookList[0].outOfPrint = False
#
Method 2 (Python Lists)
#
bookList = []
newBook = Book("XYZ", 2001, 14.99, False)
bookList.append(newBook)
newBook = Book("ABC", 1976, 13.99, True)
bookList.append(newBook)
#
Traversing an Array of Records
Method 1 – by index
for b in range(len(bookList)):
str = bookList[b].title
str += "was published in "
str += booklist[b].year
print(str)
Method 2 – by object
for book in bookList:
str = book.title
str += "was published in "
str += book.year
print(str)