Chapter 11 - What’s the Address? Pointers

example 11.1, page no. 232

In [1]:
import sys

iPtr = 0
fPtr = 0.0
cPtr = 'c'

print sys.getsizeof(iPtr)
print sys.getsizeof(fPtr)
print sys.getsizeof(cPtr)
24
24
38

example 11.2, page no. 233

In [2]:
iPtr = 0
print "The value of iPtr is", iPtr
The value of iPtr is 0

example 11.3, page no. 234

In [3]:
iPtr = None
print "The value of iPtr is ", iPtr
The value of iPtr is  None

example 11.4, page no. 235

In [4]:
num = 5
iPtr = id(num)
print "The address of x using id() is ", id(num)
print "The address of x using iPtr is ", iPtr
The address of x using id() is  42658888
The address of x using iPtr is  42658888

example 11.5, page no. 236

In [5]:
num = 5
iPtr = id(num)
print "The value of num is ", num
num = 10
print "The value of num after num = 10 is ", num
iPtr = 15
print "The value of num after iPtr = 15 is ", num
The value of num is  5
The value of num after num = 10 is  10
The value of num after iPtr = 15 is  10

example 11.6, page no. 238

In [6]:
num1 = 5
num2 = 14
iPtr = id(num1)
print "The value of num1 is ", num1
iPtr = 2
print "The value of num1 after iPtr = 2 is ", iPtr
iPtr = id(num2)
print "The value of num2 is ", num2
iPtr /= 2
print "The value of num after iPtr /= 2 is ", iPtr
The value of num1 is  5
The value of num1 after iPtr = 2 is  2
The value of num2 is  14
The value of num after iPtr /= 2 is  21329336

exmple 11.7, page no. 239

In [7]:
testScore = [4, 7, 1]
print "The address of the array using testScore is ", testScore
print "The address of the first element of the array using &testScore[0] is ", id(testScore[0])
print "The value of the first element of the array using *testScore is ", testScore[0]
print "The value of the first element of the array using testScore[0] is ", testScore[0]
The address of the array using testScore is  [4, 7, 1]
The address of the first element of the array using &testScore[0] is  42658912
The value of the first element of the array using *testScore is  4
The value of the first element of the array using testScore[0] is  4

example 11.8, page no. 240

In [8]:
MAX = 3

testScore = [4, 7, 1]
for i in range(MAX):
    print "The address of index ", i, " of the array is ", id(testScore[i])
    print "The value at index ", i, " of the array is ", testScore[i]
The address of index  0  of the array is  42658912
The value at index  0  of the array is  4
The address of index  1  of the array is  42658840
The value at index  1  of the array is  7
The address of index  2  of the array is  42658984
The value at index  2  of the array is  1

example 11.9, page no. 241

In [9]:
MAX = 3

testScore = [4, 7, 1]
iPtr = testScore
for i in range(MAX):
    print "The address of index ", i, " of the array is ", id(iPtr[i])
    print "The value at index ", i, " of the array is ", iPtr[i]
The address of index  0  of the array is  42658912
The value at index  0  of the array is  4
The address of index  1  of the array is  42658840
The value at index  1  of the array is  7
The address of index  2  of the array is  42658984
The value at index  2  of the array is  1

example 11.10, page no. 241

In [10]:
MAX = 3

testScore = [4, 7, 1]
iPtr = testScore
for i in range(MAX):
    print "The address of index ", i, " of the array is ", id(iPtr[i])
    print "The value at index ", i, " of the array is ", iPtr[i]
The address of index  0  of the array is  42658912
The value at index  0  of the array is  4
The address of index  1  of the array is  42658840
The value at index  1  of the array is  7
The address of index  2  of the array is  42658984
The value at index  2  of the array is  1

example 11.11, page no. 243

In [11]:
import sys
MAX = 3

testScore = [4, 7, 1]
iPtr = testScore
i = 0
while (id(iPtr[MAX-1]) <= id(testScore[MAX-1])):
    try:
        print "The address of index ", i, " of the array is ", id(iPtr[i])
        print "The value at index ", i, " of the array is ", iPtr[i]
        i += 1
    except IndexError:
        print "\n\nEnd of program"
        sys.exit()
An exception has occurred, use %tb to see the full traceback.

SystemExit
The address of index  0  of the array is  42658912
The value at index  0  of the array is  4
The address of index  1  of the array is  42658840
The value at index  1  of the array is  7
The address of index  2  of the array is  42658984
The value at index  2  of the array is  1
The address of index  3  of the array is  

End of program
To exit: use 'exit', 'quit', or Ctrl-D.

example 11.12, page no. 243

In [12]:
import sys
MAX = 3

testScore = [4, 7, 1]
iPtr = id(testScore[MAX-1])
i = MAX - 1
while (iPtr >= id(testScore[0])):
    print "The address of index ", i, " of the array is ", iPtr
    print "The value at index ", i, " of the array is ", testScore[i]
    i -= 1
    if i < 0:
        break
The address of index  2  of the array is  42658984
The value at index  2  of the array is  1
The address of index  1  of the array is  42658984
The value at index  1  of the array is  7
The address of index  0  of the array is  42658984
The value at index  0  of the array is  4

example 11.13, page no. 245

In [13]:
MAX = 3

def assignValues(tests, num):
    for i in range(num):
        print "Enter test score #", i + 1, ": ",
        tests.append(int(raw_input()))

def displayValues(scores, elems):
    for i in range(elems):
        print "Test score #", i + 1, ": ", scores[i]

testScore = []
assignValues(testScore, MAX)
displayValues(testScore, MAX)
Enter test score # 1 : 4
 Enter test score # 2 : 5
 Enter test score # 3 : 6
 Test score # 1 :  4
Test score # 2 :  5
Test score # 3 :  6

example 11.14, page no. 246

In [14]:
MAX = 3

def assignValues(tests, num):
    for i in range(num):
        print "Enter test score #", i + 1, ": ",
        tests.append(int(raw_input()))

def displayValues(scores, elems):
    for i in range(elems):
        print "Test score #", i + 1, ": ", scores[i]

testScore = []
assignValues(testScore, MAX)
displayValues(testScore, MAX)
Enter test score # 1 : 3
 Enter test score # 2 : 4
 Enter test score # 3 : 5
 Test score # 1 :  3
Test score # 2 :  4
Test score # 3 :  5

example 11.15, page no. 247

In [15]:
def doubleIt(x):
    print "The number to be doubled is ", x
    x *= 2
    print "The number doubled in doubleIt is ", x


print "Enter number: ",
num = int(raw_input())
doubleIt(num)
print "The number doubled in main is ", num
Enter number: 4
 The number to be doubled is  4
The number doubled in doubleIt is  8
The number doubled in main is  4

example 11.16, page no. 248

In [16]:
def doubleIt(x):
    print "The number to be doubled is ", x
    x *= 2
    print "The number doubled in doubleIt is ", x


print "Enter number: ",
num = int(raw_input())
doubleIt(num)
print "The number doubled in main is ", num
Enter number: 4
 The number to be doubled is  4
The number doubled in doubleIt is  8
The number doubled in main is  4

example 11.17, page no. 250

In [17]:
print "Enter the number of test scores: ",
numTests = int(raw_input())
iPtr = []
for i in range(numTests):
    print "Enter test score #", i + 1,": ",
    iPtr.append(int(raw_input()))

for i in range(numTests):
    print "Test score #", i + 1, " is ", iPtr[i]
Enter the number of test scores: 3
 Enter test score # 1 : 5
 Enter test score # 2 : 6
 Enter test score # 3 : 7
 Test score # 1  is  5
Test score # 2  is  6
Test score # 3  is  7

example 11.18, page no. 253

In [18]:
str = "Jeff Kent"
print str
Jeff Kent

example 11.19, page no. 253

In [19]:
def setName():
    print "Enter your name: ",
    name = raw_input()
    return name

str = setName()
print str
Enter your name: Jeff
 Jeff

example 11.20, page no. 254

In [20]:
def setName():
    print "Enter your name: ",
    name = raw_input()
    return name

str = setName()
print str
Enter your name: Jeff
 Jeff

example 11.21, page no. 255

In [21]:
def setName():
    print "Enter your name: ",
    name = raw_input()
    return name

str = setName()
print str
Enter your name: Jeff
 Jeff