Chapter 8 : Pointers

Example 8.1, Page No 179

In [1]:
from ctypes import *

iNo=c_int
pNo = id(iNo)
dPrice=dTotal=float
pPrice=id(dPrice)
pTotal=id(dTotal)
cName=range(20)
pName=id(cName)
pName = raw_input("Enter your name: ")
pNo = int(raw_input("Enter quantity: "))
pPrice = float(raw_input("and price: "))
pTotal = pNo * pPrice
print "Dear " + pName + ", your price is " + str(pTotal) + " kr."
Enter your name: chirag
Enter quantity: 10
and price: 100
Dear chirag, your price is 1000.0 kr.

Example 8.2, Page No 182

In [10]:
iSal=[14000,15000,16000,17000,18000,19000]
pSal = id(iSal)
dTax=[0.32,0.34,0.35,0.36,0.365,0.37]
pTax=id(dTax)
print "Salary          Tax"
for i in range(6):
    print str(iSal[i])+"          ",str(iSal[i]*dTax[i])
    i=i+1
Salary          Tax
14000           4480.0
15000           5100.0
16000           5600.0
17000           6120.0
18000           6570.0
19000           7030.0

Example 8.3, Page No 185

In [4]:
def find(s):
    for p in range(len(s)):
        if(s[p]=='@'):
            print "It is an email address"
        p=p+1

#cString=range(9)
pString=id(cString)
cString=raw_input("Enter A text: ")
find(cString)
Enter A text: hello@gmail.com
It is an email address

Example 8.4, Page No 188

In [2]:
iNumber=int(raw_input("How many product will be entered? "))

for i in range(iNumber):
    pNo = id(iNumber)
    print pNo
    pNO = pNo + 1
pNo = pNo - iNumber
for i in range(iNumber):
    print id(pNO)
    pNo = pNO + 1
pNo = pNo - iNumber
del pNo   
How many product will be entered? 5
3848360
3848360
3848360
3848360
3848360
51980880
51980880
51980880
51980880
51980880

Example 8.5, Page No 189

In [5]:
iNo = 5
cName = range(iNo)
print "Enter the name of 5 course mates: "
for i in range(iNo):
    print "Mate no. " ,i+1
    temp = raw_input("Mate name: ")
    cName.append(temp)
for j in range(iNo):
    print cName[j]
for k in range(iNo):
    del cName[k]    
Enter the name of 5 course mates: 
Mate no.  1
Mate name: a
Mate no.  2
Mate name: b
Mate no.  3
Mate name: c
Mate no.  4
Mate name: d
Mate no.  5
Mate name: e
0
1
2
3
4
In [ ]: