# ArrayDemoEfficient.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-14 File created. # 2022-12-27 Converted to Python 3. # This program works like ArrayDemo.py. This one uses the # array type specified in module array. # Objects of type array.array can be indexed in the same way # as lists. array.arrays can contain only numerical values. import array # A module which specifies efficient numerical arrays integer_index = 0 array_of_integers = array.array( 'i' ) # create an array of type int array_of_integers.append( 333 ) array_of_integers.append( 33 ) array_of_integers.append( 3 ) array_of_integers.append( array_of_integers[ 2 ] + 2 ) for integer_index in range( 4, 50 ) : array_of_integers.append( \ array_of_integers[ integer_index - 1 ] + 2 ) print( "\n The contents of \"array_of_integers\" is:" ) for integer_index in range( 50 ) : if ( ( integer_index % 10 ) == 0 ) : print( "" ) # This prints an empty line. print( "%5d" % array_of_integers[ integer_index ], end="" ) print( "\n\n The typecode of the array is " + array_of_integers.typecode )