# Chapter 9: More Data Types and Operations

### Example 9.1, Page Number: 182

In [2]:
def code(str):
print str

#Calling function
code("this is a test")

this is a test


### Example 9.2, Page Number: 183

In [1]:
def f(i):
i=100
print i

#Variable declaration
k=10

#function call
f(k)

100


### Example 9.3, Page Number: 187

In [7]:
#Variable Declaration
first=10                  #global definition of first and last
last=20

#Result
print first,last

10 20


### Example 9.4, Page Number: 188

In [3]:
#Variable declaration
sum=0
count=0
num=5              #Loop for user entries

#compute a running average
def r_avg(i):
global sum,count
sum=sum+i
count+=1
return sum/count

while True:
print "Enter numbers(-1 to quit): "
num-=1                 #User input
if not(num==-1):
print "Running average is: ",r_avg(num)   #Result
if num<0:
break

Enter numbers(-1 to quit):
Running average is:  4
Enter numbers(-1 to quit):
Running average is:  3
Enter numbers(-1 to quit):
Running average is:  3
Enter numbers(-1 to quit):
Running average is:  2
Enter numbers(-1 to quit):
Running average is:  2
Enter numbers(-1 to quit):


### Example 9.5, Page Number: 189

In [4]:
#Variable declaration
sum=0
count=0
num=10              #Loop for user entries

#user input given: 9,8,7,6,-2,4,3,2,1,-1

#compute a running average
def r_avg(i):
global sum,count
sum=sum+i
count+=1
return sum/count

def reset():
global sum,count
sum=0
count=0

while True:
print "Enter numbers(-1 to quit, -2 to reset): "
num-=1                 #User input
if num==5:
num=-2
if num==-2:            #for reset
num=4
reset()
continue
if not(num==-1):
print "Running average is: ",r_avg(num)   #Result
else:
break

Enter numbers(-1 to quit, -2 to reset):
Running average is:  9
Enter numbers(-1 to quit, -2 to reset):
Running average is:  8
Enter numbers(-1 to quit, -2 to reset):
Running average is:  8
Enter numbers(-1 to quit, -2 to reset):
Running average is:  7
Enter numbers(-1 to quit, -2 to reset):
Enter numbers(-1 to quit, -2 to reset):
Running average is:  3
Enter numbers(-1 to quit, -2 to reset):
Running average is:  2
Enter numbers(-1 to quit, -2 to reset):
Running average is:  2
Enter numbers(-1 to quit, -2 to reset):
Running average is:  1
Enter numbers(-1 to quit, -2 to reset):


### Example 9.6, Page Number: 196

In [1]:

name=["Jonathan","Golden Delicious","Red Delicious","Winesap",
"Cortland","McIntosh"]

#enumeration type
(Jonathan,Golden_Delicious,Red_Delicious,Winesap,Cortland,McIntosh) = (0,1,2,3,4,5)

fruit=Jonathan
print name[fruit]

fruit = Winesap
print name[fruit]

fruit = McIntosh
print name[fruit]

Jonathan
Winesap
McIntosh


### Example 9.7, Page Number: 198

In [3]:

ch='j'                  #User input
while True:
#This statement turns off the 6th but.
c=chr(ord(ch)&223)  #ch is now uppercase
print c
if c=='Q':
break
else:
ch = chr(ord(ch)+1)  #incrementing for different user inputs


J
K
L
M
N
O
P
Q


### Example 9.8, Page Number: 200

In [4]:
#Variable declaration
ch='J'                  #User input
while True:
#This statement turns off the 6th but.
c=chr(ord(ch)|32)  #ch is now uppercase
print c
if c=='q':
break
else:
ch = chr(ord(ch)+1)  #incrementing for different user inputs


j
k
l
m
n
o
p
q


### Example 9.9, Page Number: 201

In [5]:

def disp_binary(u):
t=128
while t:
if u&t:
print 1,
else:
print 0,
t=t/2
print ""

#Variable declaration
u=99                   #User Input

print "Here's the number in binary: ",
disp_binary(u)

print "Here's the complement of th number: ",
disp_binary(~u)

Here's the number in binary:  0 1 1 0 0 0 1 1
Here's the complement of th number:  1 0 0 1 1 1 0 0


### Example 9.10, Page Number: 202

In [6]:

def disp_binary(u):
t=128
while t:
if u&t:
print 1,
else:
print 0,
t=t/2
print ""

#Variable dclaration
i=1

#Result
for t in range(8):
disp_binary(i)
i=i<<1

print"\n"

for t in range(8):
i=i>>1
disp_binary(i)


0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0
0 0 0 0 1 0 0 0
0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0
0 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0

1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1


### Example 9.11, Page Number: 204

In [7]:
def div_zero():
print "Cannot divide by zero."
return 0

#Variable declaration
i=10                     #User Input
j=0

#This statement prevents a divide by zero
if j:
result=i/j
else:
result=div_zero()

#Result
print "Result: ",result

Cannot divide by zero.
Result:  0


### Example 9.12, Page Number: 206

In [1]:
#Variable declaration
j=10
i=None

j+=1
j+100
i=999+j

#Result
print i

1010


### Example 9.13, Page Number: 207

In [2]:
from ctypes import *

#Variable declaration
ch=c_char
i=c_int

print sizeof(ch),          #size of char
print sizeof(i),           #size of int
print sizeof(c_float),     #size of float
print sizeof(c_double)     #size of double

1 4 4 8


### Example 9.14, Page Number: 209

In [3]:
from ctypes import *

#Variabke declaration
i=c_int(20)          #allocate memory for int
p=pointer(i)         #assign a pointer to the memory

#Result
print p[0]           #proove that it works by displaying value

20


### Example 9.15, Page Number: 210

In [4]:
from ctypes import *

#Variable declaration
i=c_int(99)          #initialize with 99
p=pointer(i)         #assign a pointer to the value

#Result
print p[0]           #displays 99

99


### Example 9.16, Page Number: 211

In [5]:
from ctypes import *

#Variable declaration
i=c_double(10)
p=pointer(i)

#assign the values 100 to 109
for i in range(10):
p[i]=100.00+i

#display the contents of the array
for i in range(10):
print p[i],

100.0 101.0 102.0 103.0 104.0 105.0 106.0 107.0 108.0 109.0


### Example 9.17, Page Number: 211

In [6]:
#Variable declaration
i=c_int()
j=c_double()
pi=pointer(i)         #pointer to int
pj=pointer(j)         #pointer to double

#Assign values using pointers
pi[0]=10
pj[0]=100.123

#Result
print pi[0],pj[0]

10 100.123


### Example 9.18, Page Number: 212

In [11]:
from ctypes import *

#Variable declaration
i=pointer(c_int())
j=pointer(c_double())

#Checking if i and j have been allocated memory addresses
if not(id(i)):
print "Allocation Failure."

if not(id(j)):
print "Allocation Failure."

i[0]=10
j[0]=100.123

#Result
print i[0],j[0]

10 100.123

In [ ]: