Example: 1, Page No.:5.118¶

In [4]:
#Program to print the value and address of the element

a=[10,20,30,40,50]
for b in range(0,5):
print "The value of a[%d]=%d"%(b,a[b])

The value of a[0]=10
The value of a[1]=20
The value of a[2]=30
The value of a[3]=40
The value of a[4]=50


Example: 2, Page No.: 5.119¶

In [58]:
#Program to print the value and the address of the element using pointer with function

a=[10,20,30,40,50]

for c in range(0,5):
print "The Value of a[%d]=%d"%(c,a[c])
def value(b):
b=''
id(b)

The Value of a[0]=10
The Value of a[1]=20
The Value of a[2]=30
The Value of a[3]=40
The Value of a[4]=50


Example:3, Page No,: 5.120¶

In [67]:
#Program to add the sum of number using pointer

a=[10,20,30,40,50]

b=total=0
c=id(c)

for b in range(0,5):
print "Enter the number %d: "%(b+1)
a[b]=input()

c=a
for b in range(0,5):
total=total+c[b]
c[b]=c[b]+1

print "Total= %d"%(total)

Enter the number 1:
10
Enter the number 2:
20
Enter the number 3:
30
Enter the number 4:
40
Enter the number 5:
50
Total= 150


Example: 4, Page No.: 5.121¶

In [72]:
#Program to sort a given number using pointer.

arr = []
n = input("Enter the no. of elements: ")
for i in range(0,n):
arr.append(0)
print "Enter the element: "
for i in range(0,n):
arr[i]=input()
print "Before Sorting:"
for i in range(0,n):
print "%d"%arr[i],

for i in range(0,n):
for j in range(i,n):
if arr[i]>arr[j]:
t = arr[i]
arr[i] = arr[j]
arr[j] = t
print "\nAfter Sorting:"
for i in range(0,n):
print "%d" %arr[i],

Enter the no. of elements: 5
Enter the element:
8
5
7
4
6
Before Sorting:
8 5 7 4 6
After Sorting:
4 5 6 7 8