Chapter 7 : Pointers

Example 7.1, Page No 157

In [1]:
x=5
iPtr=id(x)
iPtr=7

Example 7.2, Page No 157

In [5]:
x=1
iPtr=id(x)

iPtr=5

print "\n*iPtr = "+str(iPtr)+"\n &x = "+str(id(x))
*iPtr = 5
 &x = 20176608

Example 7.3, Page No 158

In [1]:
x=5
y=10
iPtr=None

print "Pointer points to: ",iPtr
iPtr=id(y)
print "Ptr now points to: ",iPtr
x=iPtr
print "The value of x is now",x
iPtr=15
print "The value of y is now",iPtr
Pointer points to:  None
Ptr now points to:  20373108
The value of x is now 20373108
The value of y is now 15

Example 7.4, Page No 159

In [3]:
def addTwoNumbers(x,y):
    return x+y


x=0
y=0

x=int(raw_input("Enter first number:"))
y=int(raw_input("Enter second number:"))

print "Result is: ",addTwoNumbers(x,y)
Enter first number:10
Enter second number:15
Result is:  25

Example 7.5, Page No 161

In [1]:
def demoPassByValue(x):
    x+=5
    print "The value of x is:",x

x=0
x=int(raw_input("Enter a number:"))
demoPassByValue(x)
print "The original value of x did not change:",x
Enter a number:5
The value of x is: 10
The original value of x did not change: 5

Example 7.6, Page No 162

In [2]:
def demoPassByReference(x):
    x+=5
    print "The value of x is now",x

x=0
x=int(raw_input("Enter a number:"))
demoPassByReference(x)
Enter a number:10
The value of x is now 15

Example 7.7, Page No 164

In [4]:
iArray=[1,2,3,4,5]
iPtr=id(iArray)
print "Address of pointer: ",iPtr
print "First address of array: ",id(iArray[0])
print "Pointer points to: ",iArray[0]
print "First element of array contains",iArray[0]
Address of pointer:  28621960
First address of array:  19652320
Pointer points to:  1
First element of array contains 1

Example 7.8, Page No 165

In [7]:
def nameLength(name):
    x=0
    while(name[x] != '\0'):
        x+=1;
    return x
aName='\0'
aName=raw_input("Enter your first name: ")
aName=aName+'\0'
print "Your first name contains"
print str(nameLength(aName))+" characters"
Enter your first name: mehta
Your first name contains
5 characters

Example 7.9, Page No 167

In [4]:
def squareNumber(num):
    for x in range(3):
        num[x]=num[x]*num[x]

iNumbers=[2,4,6]
print "The current array value are:"

for x in range(3):
    print iNumbers[x]

print "\n"

squareNumber(iNumbers)

print "The modified array values are :"

for x in range(3):
    print iNumbers[x]

print "\n"
The current array value are:
2
4
6


The modified array values are :
4
16
36


Example 7.10, Page No 168

In [11]:
def printArgument(num):
    print "Read only argument is: ",num
    
iNumber=5
printArgument(iNumber)
    
Read only argument is:  5

Example 7.11, Page No 169

In [12]:
def printArray(num):
    for x in range(3):
        print num[x]

iNumbers=[2,4,6]
printArray(iNumbers)
2
4
6

Example 7.12, Page No 170

In [14]:
def modifyArray(num):
    for x in range(3):
        num[x]=num[x]*num[x]
        
iNumbers=[2,4,6]
modifyArray(iNumbers)

Example 7.13, Page No 173

In [ ]:
import random
def encrypt(sMessage,random):
    x=0
    while(sMessage[x]):
        sMessage+=str(random)
        x+=1
    x=0
    print "Encrypted message is: "
    
    while(sMessage[x]):
        print sMessage[x]
        x+=1

def decrypt(sMessage,random):
    x=0
    while(sMessage[x]):
        sMessage[x]=sMessage[x]-str(random)
        x+=1
    x=0
    while(sMessage[x]):
        print sMessage[x]
        x+=1
        
myString=range(21)
iSelection=0
iRand= (random.randint(1,4)%4)+1

while(iSelection != 4):
    print "\n\n1\t Encrypt Clear Text\n"
    print "2\tDecrypt Cipher Text\n"
    print "3\tGenerate New Key\n"
    print "4\tQuit\n"
    iSelection=int(raw_input("\nSelect a Cryptography Option: "))
    
    if(iSelection ==1):
        myString=raw_input("\nEnter one word as clear text to encrypt: ")
        encrypt(myString,iRand)
        break
    elif(iSelection==2):
        myString=raw_input("\nEnter cipher text to decrypt: ")
        decrypt(myString,iRand)
        break
    elif(iSelection==3):
        iRand= (random.randrange(1,4,1)%4)+1
        print "\nNew Key Generated\n"
        break
    else:
            exit(0)

1	 Encrypt Clear Text

2	Decrypt Cipher Text

3	Generate New Key

4	Quit


Select a Cryptography Option: 1

Enter one word as clear text to encrypt: bhavin