Hour 12 : Understnading Arrays¶

Example 12.1, Page No 191¶

In [2]:
list_int=range(10)

for i in range(10):
list_int[i]=i+1
print "list_int["+str(i)+"] is initialized with "+str(list_int[i])+"\n"

list_int[0] is initialized with 1

list_int[1] is initialized with 2

list_int[2] is initialized with 3

list_int[3] is initialized with 4

list_int[4] is initialized with 5

list_int[5] is initialized with 6

list_int[6] is initialized with 7

list_int[7] is initialized with 8

list_int[8] is initialized with 9

list_int[9] is initialized with 10



Example 12.2, Page No 193¶

In [3]:
import sys
list_int=range(10)

total_byte= sys.getsizeof(int)*10

print "The size of int is "+str(sys.getsizeof(int))+" byte long"
print "The array of 10 ints has total "+str(total_byte)+" bytes"
print "The address of the first element: "+str(id(list_int[0]))
print "The address of the last element: "+str(id(list_int[9]))

The size of int is 436 byte long
The array of 10 ints has total 4360 bytes
The address of the first element: 21357036
The address of the last element: 21356928


Example 12.3, Page No 195¶

In [8]:
list_int=range(10)

for i in range(10):
list_int[i]=i+1

ptr_int=list_int
print "The start address of the array: "+str(id(ptr_int[0]))
print "The value of the first element: "+str(ptr_int[0])
ptr_int=id(list_int[0])
print "The address of the first element: "+str(ptr_int)
print "The value of the first element: "+str(list_int[0])

The start address of the array: 20177376
The value of the first element: 1
The address of the first element: 20177376
The value of the first element: 1


Example 12.4, Page No 196¶

In [5]:
array_ch=['H','e','l','l','o','!','\0']

for i in range(7):
print "array_ch["+str(i)+"] contains: "+str(array_ch[i])

'''Method I'''
print "Put all elements together(Method I):"
for ch in array_ch:
print ch,

''' Method II'''
print "\nPut all elements together(Method II):"
print "".join(array_ch)

array_ch[0] contains: H
array_ch[1] contains: e
array_ch[2] contains: l
array_ch[3] contains: l
array_ch[4] contains: o
array_ch[5] contains: !
array_ch[6] contains:
Put all elements together(Method I):
H e l l o !
Put all elements together(Method II):
Hello!


Example 12.5, Page No 198¶

In [5]:
array_ch=['C',' ','i','s',' ','p','o','w','e','r','f','u','l','!','\0']

i=0
while(array_ch[i]!='\0'):
print ""+array_ch[i],
i=i+1

print "\n"

C   i s   p o w e r f u l !



Example 12.6, Page No 200¶

In [17]:
two_dim=[[1,2,3,4,5],[10,20,30,40,50],[100,200,300,400,500]]

for i in range(3):
print "\n"
for j in range(5):
print "%6u" % two_dim[i][j],
print "\n"


1      2      3      4      5

10     20     30     40     50

100    200    300    400    500



Example 12.7, Page No 202¶

In [2]:
import sys
array_ch=['C',' ','i','s',' ','p','o','w','e','r','f','u','l','!','\0']

list_int=[[1,1,1],[2,2,8],[3,9,27],[4,16,64],[5,25,125],[6,36,216],[7,49,343]]

print "The size of array_ch[] is %d bytes.\n" %sys.getsizeof(array_ch)
print "The size of list_int[][] is %d bytes.\n" %sys.getsizeof(list_int)

The size of array_ch[] is 96 bytes.

The size of list_int[][] is 64 bytes.