Creating Arrays of Records
Method 1 (SQA Arrays)
1 2 3 4 5 6 7 8 | # 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)
1 2 3 4 5 6 7 8 9 | # 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
1 2 3 4 5 | for b in range ( len (bookList)): str = bookList[b].title str + = "was published in " str + = booklist[b].year print ( str ) |
Method 2 – by object
1 2 3 4 5 | for book in bookList: str = book.title str + = "was published in " str + = book.year print ( str ) |