# CHAPTER 8: ARRAYS¶

## EXAMPLE ON PAGE:256¶

In [1]:

x=5
x=10
print "x=%d\n" % (x)                        #prints 10

x=10



## EXAMPLE ON PAGE:257-258¶

In [2]:

sum=0
marks=[]                             #array declaration
for i in range(30):
marks.append(0)
for i in range(0,30,1):
print "Enter marks"
marks[i]=eval(raw_input())       #store data in array
for i in range(0,30,1):
sum=sum+marks[i]
avg=sum/30
print "Average marks=%d\n" % (avg)

Enter marks
98
Enter marks
88
Enter marks
97
Enter marks
96
Enter marks
87
Enter marks
89
Enter marks
80
Enter marks
86
Enter marks
95
Enter marks
99
Enter marks
90
Enter marks
100
Enter marks
99
Enter marks
94
Enter marks
85
Enter marks
84
Enter marks
92
Enter marks
91
Enter marks
87
Enter marks
76
Enter marks
75
Enter marks
96
Enter marks
83
Enter marks
80
Enter marks
93
Enter marks
79
Enter marks
70
Enter marks
78
Enter marks
85
Enter marks
86
Average marks=87



## EXAMPLE ON PAGE:261¶

In [2]:

num=[]
for i in range(40):
num.append(0)
for i in range(0,40,1):                            #be carefull about the size of the array
num[i]=i


## EXAMPLE ON PAGE:261-262¶

In [3]:

def display(m):
print "%d" % (m)
marks=[55,65,75,56,78,78,90]
for i in range(0,7,1):
display(marks[i])

55
65
75
56
78
78
90


## EXAMPLE ON PAGE:263¶

In [4]:

def show(n):
print "%d" % (n)
def disp(n):
show(n)                                             #calls show()
marks=[55,65,75,56,78,78,90]
for i in range(0,7,1):
disp(marks[i])

55
65
75
56
78
78
90


## EXAMPLE ON PAGE:263-264¶

In [5]:

i=3
j=1.5
k='c'
print "Value of i=%d\n" % (i)
print "Value of j=%f\n" % (j)
print "Value of k=%c\n" % (k)
x=id(i)
y=id(j)
z=id(k)
print "Original address in x=%u\n" % (x)
print "Original address in y=%u\n" % (y)
print "Original address in z=%u\n" % (z)
x+=1
y+=1
z+=1
print "New address in x=%u\n" % (x)
print "New address in y=%u\n" % (y)
print "New address in z=%u\n" % (z)

Value of i=3

Value of j=1.500000

Value of k=c



## EXAMPLE ON PAGE:265¶

In [6]:

arr=[10,20,30,45,67,56,74]
i=id(arr[1])
j=id(arr[5])
print "%d %d\n" % (j-i,arr[5]-arr[1])

-432 36



## EXAMPLE ON PAGE:266¶

In [7]:

arr=[10,20,36,72,45,36]
j=id(arr[4])
k=id(arr[0+4])                                              #didn't get any other way
if j==k:
print "The two pointers point to the same location\n"
else:
print "The two pointers do not point to the same location\n"

The two pointers point to the same location



## EXAMPLE ON PAGE:267¶

In [8]:

num=[24,34,12,44,56,17]
for i in range(0,6,1):
print "element no.%d" % (i)

element no.0

element no.1

element no.2

element no.3

element no.4

element no.5



## EXAMPLE ON PAGE:268¶

In [10]:

num=[24,34,12,44,56,17]
for i in range(0,6,1):
print "element=%d\n" % (num[i])

address=21147060
element=24

element=34

element=12

element=44

element=56

element=17



## EXAMPLE ON PAGE:268¶

In [11]:

num=[24,34,12,44,56,17]
j=id(num[0])                                      #assign address of zeroth element
for i in range(0,6,1):
print "element=%d\n" % (num[i])

address=21147060
element=24

element=34

element=12

element=44

element=56

element=17



## EXAMPLE ON PAGE:270¶

In [12]:

def display(j,n):
for item in j:
print "element=%d\n" % (item)
num=[24,34,12,44,56,17]
display(num,6)

element=24

element=34

element=12

element=44

element=56

element=17



## EXAMPLE ON PAGE:271¶

In [13]:

num=[24,34,12,44,56,17]
for i in range(0,6,1):
print "element=%d %d" % (num[i],num[i])           #no other way
print "%d %d\n" % (num[i],num[i])

address=21147060
element=24 24
24 24

element=34 34
34 34

element=12 12
12 12

element=44 44
44 44

element=56 56
56 56

element=17 17
17 17



## EXAMPLE ON PAGE:272¶

In [14]:

stud=[[0,0],[0,0],[0,0],[0,0]]
for i in range(0,4,1):
print "Enter roll no. and marks"
stud[i][0]=eval(raw_input())
stud[i][1]=eval(raw_input())
for i in range(0,4,1):
print "%d %d\n" % (stud[i][0],stud[i][1])

Enter roll no. and marks
1206030
100
Enter roll no. and marks
1206007
95
Enter roll no. and marks
1206034
97
Enter roll no. and marks
1206026
88
1206030 100

1206007 95

1206034 97

1206026 88



## EXAMPLE ON PAGE:275¶

In [15]:

s=[[1234,56],[1212,33],[1434,80],[1312,78]]
for i in range(0,4,1):
print "Address of %dth 1-D array=%u\n" % (i,id(s[i]))

Address of 0th 1-D array=88430392



## EXAMPLE ON PAGE:277¶

In [16]:

s=[[1234,56],[1212,33],[1434,80],[1312,78]]
for i in range(0,4,1):
for j in range(0,2,1):
print "%d" % (s[i][j])
print "\n"

1234
56

1212
33

1434
80

1312
78



## EXAMPLE ON PAGE:277-278¶

In [18]:

s=[[1234,56],[1212,33],[1434,80],[1312,78]]
p=[]
for i in range(2):
p.append(0)
for i in range(0,4,1):
p=s
pint=p
print "\n"
for j in range(0,2,1):
print "%d" % (pint[i][j])


1234
56

1212
33

1434
80

1312
78


## EXAMPLE ON PAGE:279-280¶

In [20]:

def display(q,row,col):
for item in q:
print item
print "\n"
a=[[1,2,3,4],[5,6,7,8],[9,0,1,6]]
display(a,3,4)

[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 0, 1, 6]



## EXAMPLE ON PAGE:281-282¶

In [21]:

arr=[]
for i in range(4):
arr.append(0)
i=31
j=5
k=19
l=71
arr[0]=i
arr[1]=j
arr[2]=k
arr[3]=l
for m in range(0,4,1):
print "%d\n" % (arr[m])

31

5

19

71



## EXAMPLE ON PAGE:282¶

In [22]:

a=[0,1,2,3,4]
print "%u %u %d\n" % (id(id(a[0])),id(a[0]),a[0])

88518416 21147348 0