def code(str):
print str
#Calling function
code("this is a test")
def f(i):
i=100
print i
#Variable declaration
k=10
#function call
f(k)
#Variable Declaration
first=10 #global definition of first and last
last=20
#Result
print first,last
#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
#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
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]
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
#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
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)
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)
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
#Variable declaration
j=10
i=None
j+=1
j+100
i=999+j
#Result
print i
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
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
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
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],
#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]
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]