n=44
print "n = " , n
# prints the value of n
print "&n = " , hex(id(n)) # prints the address of n
'''
Note : Python doesn't support reference/pointer variable. But can be achieved by using mutable datatypes i.e. list.
'''
n = [44]
rn=n # r is a synonym for n
print "n = " , n , ", rn = " , rn
n[0] -= 1
print "n = " , n , ", rn = " , rn
rn[0] *= 2
print "n = " , n , ", rn = " , rn
n = [44]
rn=n # r is a synonym for n
print "&n = " , hex(id(n)) , ", rn = " , hex(id(rn ))
rn2 = n
rn3 = rn
print "&rn2 = " , hex(id(rn2)) , ", rn = " , hex(id(rn ))
n = [44]
print "n = " , n , ", &n = " , hex(id(n))
pn = n
print "pn = " , hex(id(pn)) , ", &pn = " , hex(id(hex(id(pn))))
n = [44]
print "n = " , n , ", &n = " , hex(id(n))
pn = n
print "\tpn = " , hex(id(pn)) , ",\n &pn = " , hex(id(hex(id(pn))))
print "*pn = " , pn
n = [44]
print "n = " , n , ", &n = " , hex(id(n))
pn = n
print "\tpn = " , hex(id(pn)) , ",\n &pn = " , hex(id(hex(id(pn))))
print "*pn = " , pn
ppn = pn
print " ppn = " , hex(id(hex(id(ppn))))
print " &ppn = " , hex(id(hex(id(hex(id(ppn))))))
print " *ppn = " , hex(id(ppn))
print "**ppn = " , ppn
n = [44]
print "n = " , n , "\n &n = " , hex(id(n))
pn = n
print "\tpn = " , hex(id(pn)) , ",\n &pn = " , hex(id(hex(id(pn))))
print "*pn = " , pn
nn = pn
print " ppn = " , hex(id(nn))
print " &ppn = " , hex(id(hex(id(nn))))
rpn = pn
print " ppn = " , hex(id(rpn))
print " &ppn = " , hex(id(hex(id(rpn))))
def max_(m,n):
if m>n:
return m
else:
return n
m = 44
n = 22
print m , ", " , n , ", " , max_(m,n)
m = max_(m,n)
m = 55
# changes the value of m from 44 to 55
print m , ", " , n , ", " , max_(m,n)
v = []
for k in range(1,5):
v.append(1.0/k)
for i in range(4):
print "v[" , i , "] = " , v[i]
import sys
a = [22, 33, 44]
print "a = " , hex(id(a))
print "sizeof(int) = " , sys.getsizeof(1)
s = 0
for i in a:
s += i
print "\t i = " , hex(id(i)),
print "\t *i = " , i,
print "\t sum = " , s
a = [22, 33, 44, 55, 66]
print "a = " , hex(id(a)) , ", *a = " , a[0]
for p in a:
print "p = " , hex(id(p)) , ", *p = " , p
def loc(a1,a2,n1,n2):
p = []
for element in a2:
if element in a1:
p.append(element)
return p
a1 = [11, 11, 11, 11, 11, 22, 33, 44, 55]
a2 = [11, 11, 11, 22, 33]
print "Array a1 begins at location\t" , hex(id(a1 ))
print "Array a2 begins at location\t" , hex(id(a2))
p = loc(a1, a2, 9, 5)
if (p):
print "Array a2 found at location\t" , hex(id(p))
for i in range(len(p)):
print "\t" , hex(id(p[i])) , ": " , p[i], "\t" , hex(id(a2[i])) , ": " , a2[i]
else:
print "Not found."
def get(a):
print "Enter number of items: "
n = int(raw_input())
print "Enter " , n , " items, one per line:"
for i in range(n):
print "\t" , i+1 , ": ",
a.append(float(raw_input()))
def print_(a):
for i in range(len(a)):
print a[i] ,
print ''
a = []
get(a)
print_(a)
a = []
get(a)
print_(a)
def sort(p, n):
for i in range(1,n):
for j in range(n-i):
if (p[j] > p[j+1]):
p[j],p[j+1] = p[j+1],p[j]
def sum_(k,n):
# returns the sum f(0) + f(1) + f(2) + . . . + f(n-1):
s = 0
for i in range(1,n+1):
s += k(i)
return s
def square(k):
return k*k
def cube(k):
return k*k*k
print sum_(square,4) # 1 + 4 + 9 + 16
print sum_(cube,4) # 1 + 8 + 27 + 64