A record data type is a heterogeneous data type—think of it as representing a row in a spreadsheet or a database. To give an example of a record data type, we will create a record for a shop inventory. This record contains the name of an item represented by a 40-character string, the number of items in the store represented by a 32-bit integer, and finally, the price of the item represented by a 32-bit float. The following steps show how to create a record data type (see the record.py
file in the Chapter02
folder of this book's code bundle):
To create a record, check the following code snippet:
In: t = dtype([('name', str_, 40), ('numitems', int32), ('price', float32)]) In: t Out: dtype([('name', '|S40'), ('numitems', '<i4'), ('price', '<f4')])
To view the type of the field, check the following code snippet:
In: t['name'] Out: dtype('|S40')
If you don't give the array()
function a data type, it will assume that it is dealing with floating point numbers. To create...