Chapter 13: Simple pointers

Example 13.1, Page number: 225

In [1]:
# Variable declaration
thing_var = 2

# Calculation and result
print ('Thing %d' % thing_var)

thing_ptr = thing_var
thing_ptr = 3
print ('Thing %d' % thing_ptr)
Thing 2
Thing 3

Example 13.2, Page number: 227

In [2]:
# Function declaration
def inc_count (count_ptr) :
   count_ptr += 1
   return count_ptr

# Calculation and result
count = 0

while (count < 10) :
   print ('Count %d\n' % inc_count (count))
   count += 1
Count 1

Count 2

Count 3

Count 4

Count 5

Count 6

Count 7

Count 8

Count 9

Count 10

Example 13.3, Page number: 230

In [3]:
# Variable declaration
array = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

# Calculation and result
print ('&array[index] (array+index) array[index]\n')

for index in range (0, 10) :
   print ('0x%s 0x%s 0x%s\n' % (id (array[index]), id (array[index]), array[index]))
&array[index] (array+index) array[index]

0x20916008 0x20916008 0x0

0x20270280 0x20270280 0x1

0x20733728 0x20733728 0x2

0x20916464 0x20916464 0x3

0x20270232 0x20270232 0x4

0x20733560 0x20733560 0x5

0x20270256 0x20270256 0x6

0x20733752 0x20733752 0x7

0x20916512 0x20916512 0x8

0x20916032 0x20916032 0x9

Example 13.4, Page number: 232

In [4]:
# Variable declaration
array = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]
index = 0

# Calculation and result
while (array[index] != 0) :
   index += 1

print ('Number of elements before zero %d\n' % index)
Number of elements before zero 6

Example 13.5, Page number: 232

In [5]:
# Variable declaration
array = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]
index = 0

# Calculation and result
while (array[index] != 0) :
   index += 1

print ('Number of elements before zero %d\n' % index)
Number of elements before zero 6

Example 13.6, Page number: 233

In [6]:
# Variable declaration
data = []

# Calculation and result
for index in range (0, 10) :
   data.append(0)
print (data)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Example 13.10, Page number: 238

In [8]:
# Variable declaration
line = 'Steve/Oualline'

# Calculation and result
first_ptr, last_ptr = line.split('/')
print ('First: %s Last: %s\n' % (first_ptr, last_ptr))
First: Steve Last: Oualline

Example 13.11, Page number: 240

In [9]:
# Function declaration
def tmp_name() :
   if not hasattr (tmp_name, 'sequence') :
      tmp_name.sequence = 0
   tmp_name.sequence += 1
   name = 'tmp'
   name += str (tmp_name.sequence)
   return name

# Calculation and result
print ('Name: %s\n' % tmp_name())
Name: tmp1

Example 13.12, Page number: 245

In [10]:
# Variable and function declaration
import sys
v_count = sys.argv
counter = len(sys.argv)

# Produces verbose messages
verbose = 0

# Sends output to a file
out_file = open ('print.out', 'w')

# Sets the number of lines per page
line_max = 66

def do_file (name) :
   print ('Verbose %d Lines %d Input %s Output %s\n' % (verbose, line_max, name, out_file.name))

def usage() :
   print ('Usage is %s [options] [file-list]\n' % program_name)
   print ('Options\n')
   print (' -v verbose\n')
   print (' -l<number> Number of lines\n')
   print (' -o<name> Set output filename\n')
   sys.exit(1)
   
# Calculation and result
program_name = str (sys.argv[0])

while ((counter > 1) and (sys.argv[1][0] == '-')) :
   if (sys.argv[1][1] == 'v') :
      verbose = 1
      break

   elif (sys.argv[1][1] == 'o') :
      temp = str (sys.argv[1])
      out_file.write (temp)
      break

   elif (sys.argv[1][1] == 'l') :
      line_max = int (sys.argv[1][2])
      break

   else :
      print ('Bad option %s\n' % sys.argv[1])
      usage()

   for index in range (0, counter) :
      if (index == counter - 1) :
         break
      else :
         v_count[index] = v_count[index + 1]

   counter -= 1

if (counter == 1) :
   do_file ('print.in')

else :
   while (counter > 1) :
      do_file (sys.argv[1])

      for index in range (0, counter) :
         if (index == counter - 1) :
            break
         else :
            v_count[index] = v_count[index + 1]
         
      counter -= 1

out_file.close()
An exception has occurred, use %tb to see the full traceback.

SystemExit: 1
Bad option -f

Usage is -c [options] [file-list]

Options

 -v verbose

 -l<number> Number of lines

 -o<name> Set output filename

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

Example 13.13, Page number: 248

In [11]:
# Function declaration
def tmp_name() :
   if not hasattr (tmp_name, 'sequence') :
      tmp_name.sequence = 0
   tmp_name.sequence += 1
   name = 'tmp'
   name += str (tmp_name.sequence)
   return name

# Calculation and result
name1 = tmp_name()
name2 = tmp_name()
print ('Name1: %s\n' % name1)
print ('Name2: %s\n' % name2)
Name1: tmp1

Name2: tmp2