#Display the address of the variable
import sys
#Variable Initialization
num = int(raw_input("Enter a Number = "))
#Result
sys.stdout.write("Value of num = %d\n"%(num))
sys.stdout.write("Address of num = %d\n"%(id(num)))
#Display the addresses of different variables together with their values.
import sys
#Variable Initialization
c = raw_input("Enter alphabet")
i = int(raw_input("Enter number"))
f = float(raw_input("Enter float"))
#Result
sys.stdout.write("\nAddress of (c) %c = %d"%(c,id(c)))
sys.stdout.write("\nAddress of (i) %d = %d"%(i,id(i)))
sys.stdout.write("\nAddress of (f) %.2f = %d"%(f,id(f)))
#Memory address differs in different systems
#Show pointer of any data type that occupies same space
import sys
#Result
#Garbage collector module is used for python memory management
sys.stdout.write("char %d byte & its pointer %d bytes\n"%(sys.getsizeof(chr),sys.getsizeof(id(chr))))
sys.stdout.write("int %d byte & its pointer %d bytes\n"%(sys.getsizeof(int),sys.getsizeof(id(int))))
sys.stdout.write("float %d byte & its pointer %d bytes\n"%(sys.getsizeof(float),sys.getsizeof(id(float))))
#value tagged method is used in python
#Display the value of variable and its location using pointer
import sys
#Variable Initialization
#There is no pointer concept in python
v = 10
p = v
#In python, variables of same value are tagged together instead of storing the value in different locations
#Result
sys.stdout.write("\nAddress of v = %u"%(id(v)))
sys.stdout.write("\nValue of v = %d"%(p))
sys.stdout.write("\nAddress of p = %d"%(id(p)))
#Print the value of variable by different ways.
import sys
#Variable Initialization
a = int(raw_input("Enter Integer Value"))
b = float(raw_input("Enter float Value"))
pa = id(a)
pb = id(b)
#Result
#There is no pointer concept in python. content of memory location can't be accessed directly by user.
sys.stdout.write("\nAddress of a = %u"%(pa))
sys.stdout.write("\nValue of a = %d"%(a))
sys.stdout.write("\nValue of a = %d"%(a))
sys.stdout.write("\nValue of a = %d"%(a))
sys.stdout.write("\nAddress of b = %u"%(pb))
sys.stdout.write("\nValue of b = %.2f"%(b))
sys.stdout.write("\nValue of b = %.2f"%(b))
sys.stdout.write("\nValue of b = %.2f"%(b))
#Print value of variable using different pointer notations.
import sys
#Variable Initialization
v = 10
p = id(v)
#Result
#There is no pointer concept in python and can't access memory location directly by user
sys.stdout.write("\nv = %d v = %d v = %d"%(v,v,v))
#Print element and its address using pointer
import sys
#Variable Initialization
i = int(raw_input("Enter a number : "))
k = id(i)
#Result
#There is no pointer concept in python and can't access memory location directly by user
sys.stdout.write("\nAddress of i is %u"%(k))
sys.stdout.write("\nValue of i is %d"%(i))
#The memory address differs in different systems
#Add two numbers through variables and their pointers
import sys
#Variable Initialization
a = int(raw_input("Enter Two Numbers : "))
b = int(raw_input("Enter Two Numbers : "))
ap = a
bp = b
#Calculation
c = a + b
d = ap + bp
#Result
#There is no pointer concept in python and can't access memory location directly by user
sys.stdout.write("\nSum of A & B Using Variable : %d"%(c))
sys.stdout.write("\nSum of A & B Using Pointer : %d"%(d))
#Assign pointer value to another variable
import sys
#Variable Initialization
a = 5
c = id(a)
b = a
#Result
#There is no pointer concept in python and can't access memory location directly by user
sys.stdout.write("\nMemory location of 'a' = %u"%(c))
sys.stdout.write("\nThe value of a = %d & b = %d"%(a,b))
#Assign value of 'b' to 'a' through pointers and add the values.
import sys
#Variable Initialization
a = int(raw_input("Enter Value of 'a' & 'b' : "))
b = int(raw_input("Enter Value of 'a' & 'b' : "))
pa = id(a)
pb = id(b)
#Result
#There is no pointer concept in python and can't access memory location directly by user
sys.stdout.write("\nValue of a = %d & b = %d"%(a,b))
sys.stdout.write("\nAddress of a = %u"%(pa))
sys.stdout.write("\nAddress of b = %u"%(pb))
sys.stdout.write("\n\nAddition of 'a' & 'b' : %d"%(a+b))
a = b
pa = id(a)
#In python, variables of same value are tagged together instead of storing the value in different locations
sys.stdout.write("\n*pa = %d *pb = %d"%(a,b))
sys.stdout.write("\npa = %u pb = %u"%(pa,pb))
sys.stdout.write("\nAddition of *pa + *pb : %d"%(a+b))
#The effect of increment on pointer variables.
import sys
#Variable Initialization
x = int(raw_input("Enter integer Value"))
y = raw_input("Enter Character Value")
z = float(raw_input("Enter float Value"))
x1 = id(x)
y1 = id(y)
z1 = id(z)
#Result
#There is no pointer concept in python and can't access memory location directly by user
sys.stdout.write("\nAddress of x = %u\n"%(x1))
sys.stdout.write("\nAddress of y = %u\n"%(y1))
sys.stdout.write("\nAddress of z = %u\n"%(z1))
x1 += 1
y1 += 1
z1 += 1
#In python, variables of same value are tagged together instead of storing the value in different locations
sys.stdout.write("\nAfter Increment in Pointers\n")
sys.stdout.write("\nNow Address of x = %u\n"%(x1))
sys.stdout.write("Now Address of y = %u\n"%(y1))
sys.stdout.write("Now Address of z = %u\n"%(z1))
sys.stdout.write("\nSize of Integer : %d"%(sys.getsizeof(x)))
sys.stdout.write("\nSize of Character : %d"%(sys.getsizeof(y)))
sys.stdout.write("\nSize of Float : %d"%(sys.getsizeof(z)))
#The effect of increased and decreased operator used as prefix and suffix with pointer variable.
import sys
#Variable Initialization
i = int(raw_input("Enter Value of i = "))
ii = id(i)
#Result
sys.stdout.write("Address of i = %u\n"%(ii))
#There is no increment or decrement operator and pointer concept in python
ii += 1
sys.stdout.write("Address of i = %u\n"%(ii))
sys.stdout.write("Address of i = %u\n"%(ii))
ii += 1
ii -= 1
sys.stdout.write("Address of i = %u\n"%(ii))
sys.stdout.write("Address of i = %u\n"%(ii))
ii -= 1
sys.stdout.write("Address of i = %u\n"%(ii))
#Memory address differs in different systems
#Perform different arithmetic operations using pointers
import sys
#Variable Initialization
a = 25
b = 10
p = id(a)
j = id(b)
#Result
#There is no pointer concept in python and user can't access memory contents directly
sys.stdout.write("\nAddition a + b = %d"%(a+b))
sys.stdout.write("\nSubtraction a - b = %d"%(a-b))
sys.stdout.write("\nProduct a * b = %d"%(a*b))
sys.stdout.write("\nDivision a / b = %d"%(a/b))
sys.stdout.write("\na Mod b = %d"%(a%b))
#Compare two pointers.
import sys
#Variable Initialization
a = 2
j = id(a)
k = id(a)
#Result
if j == k:
sys.stdout.write("\nThe Tow Pointers have the same address.")
else:
sys.stdout.write("\nThe Pointers have the different address.")
sys.stdout.write("\n\nAddress of a (j) = %u"%(j))
sys.stdout.write("\nAddress of a (k) = %u"%(k))
#Memory address differs in different systems
#Display elements of an array.
import sys
#Variable Initialization
x = [2,4,6,8,10]
k = 1
#Result
while k <= 5:
sys.stdout.write("%3d"%(x[k-1]))
k += 1
#There is no pointer concept in python
#Display array element with their addresses using array name as a pointer
import sys
#Variable Initialization
x = [2,4,6,8,10]
k = 0
#Result
sys.stdout.write("\nElement No.\tElement\tAddress")
while k < 5:
sys.stdout.write("\nx[%d] \t%13d %10u"%(k,x[k],id(x[k])))
k += 1
#There is no pointer concept in python
#Display array elements with their addresses using array name as a pointer
import sys
#Variable Initialization
num = [10,25,35,45]
#Result
sys.stdout.write("\nElement\t Address\n")
for i in range(0,4):
sys.stdout.write("num[%d] = %d "%(i,num[i]))
sys.stdout.write("%8u\n"%(id(num[i])))
#There is no pointer concept in python
#Access elements of array through different ways using pointer
import sys
#Variable Initialization
arr = [10,20,30,40,50]
p = 0
#Result
for p in range(0,5):
sys.stdout.write("Value of arr[%d] = "%(p))
sys.stdout.write("%d | "%(arr[p]))
sys.stdout.write("%d | "%(arr[p]))
sys.stdout.write("%d | "%(arr[p]))
sys.stdout.write("%d | "%(arr[p]))
sys.stdout.write("address of arr[%d] = %u\n"%(p,id(arr[p])))
#There is no pointer concept in python
#Memory address differs in different systems
#Find sum of all the elements of an array.
import sys
#Variable Initialization
sum1 = 0 #Since sum is a built-in function in python, sum1 is used
i = 0
a = [1,2,3,4,5]
#Result
sys.stdout.write("Elements Values Address\n\n")
while i < 5:
sys.stdout.write("a[%d]\t %5d\t %8u\n"%(i,a[i],id(a[i])))
sum1 = sum1 + a[i]
i += 1
#There is no pointer concept in python
#Sum of squares and cubes of array elements using pointer
import sys
#Variable Initialization
b = [1,2,3,4,5]
sumsq = 0
sumc = 0
#Result
for j in range(0,5):
sumsq = sumsq + pow(b[j],2)
sumc = sumc + pow(b[j],3)
sys.stdout.write("\nSum of Squares of array elements : %d"%(sumsq))
sys.stdout.write("\nSum of Cubes of array elements : %d"%(sumc))
#There is no pointer concept in python
#Copy element of one array to another array using pointers.
import sys
#Variable Initialization
so = [10,20,30,40,50]
ds = [0 for i in range(0,5)]
#Copying
for i in range(0,5):
ds[i] = so[i]
sys.stdout.write("Original Array Duplicated Array")
for i in range(0,5):
sys.stdout.write("\n\t%d\t\t%d"%(so[i],ds[i]))
#There is no pointer concept in python
#Copy one array into another array.
import sys
#Variable Initialization
arr1 = [15,25,35,45,55]
arr2 = [0 for i in range(0,5)]
j = 4
#Copying & Result
sys.stdout.write("\nOriginal Array Duplicate Array")
for i in range(0,5):
arr2[i] = arr1[j]
j -= 1
sys.stdout.write("\n\t%d\t\t%d"%(arr1[i],arr2[i]))
#There is no pointer concept in python
#Display array elements and their address using pointers
import sys
#Variable Initialization
a = [[1,2,3],[4,5,6],[7,8,9]]
j = 1
#Result
sys.stdout.write("\tElements of An Array with their addresses\n\n")
p = id(a[0][0])
for i in range(0,3):
for j in range(0,3):
sys.stdout.write("%5d [%5u]"%(a[i][j],id(a[i][j])))
sys.stdout.write("\n")
#There is no pointer concept in python
#Memory address differs in different systems
#Display array elements and their address.
import sys
#Variable Initialization
a = [[1,2,3],[4,5,6],[7,8,9]]
#Result
sys.stdout.write("\nElements of An Array with their addresses.\n\n")
for i in range(0,3):
for j in range(0,3):
sys.stdout.write("%5d [%5u]"%(a[i][j],id(a[i][j])))
sys.stdout.write("\n")
#There is no pointer concept in python
#Memory address differs in different systems
#Store addresses of different elements of an array using array of pointers
import sys
#Variable Initialization
arrl = [5,10,15]
arrp = [0 for i in range(0,3)]
for i in range(0,3):
arrp[i] = id(arrl[i])
#Result
sys.stdout.write("\nAddress \tElement\n")
for k in range(0,3):
sys.stdout.write("%u"%(arrp[k]))
sys.stdout.write("\t%7d\n"%(arrl[k]))
#There is no pointer concept in python
#Memory address differs in different systems
#Display address of elements and pointers
import sys
#Variable Initialization
a = [0,1,2,3,4]
p = [0 for i in range(0,5)]
for i in range(0,5):
p[i] = id(a[i])
#Result
for i in range(0,5):
sys.stdout.write("\n%d at location"%(a[i]))
sys.stdout.write("\t%u at location"%(id(a[i])))
sys.stdout.write(" %u"%(id(p[i])))
#There is no pointer concept in python
#Memory address differs in different systems
#Print value of a variable through pointer and pointer to pointer
import sys
#Variable Initialization
a = 2
p = id(a)
q = id(p)
#Result
sys.stdout.write("\n Value of a = %d Address of a = %u"%(a,id(a)))
sys.stdout.write("\nThrough *p Value of a = %d Address of a = %u"%(a,p))
sys.stdout.write("\nThrough **q Vallue of a = %d Address of a = %d"%(a,p))
#There is no pointer concept in python
#Memory address differs in different systems
#Different levels of array of pointer to pointer and display the elements
import sys
#Variable Initialization
a = [1,2,3]
b = [0 for i in range(0,3)]
c = [0 for i in range(0,3)]
d = [0 for i in range(0,3)]
e = [0 for i in range(0,3)]
f = [0 for i in range(0,3)]
for k in range(0,3):
b[k] = id(a[k])
c[k] = id(b[k])
d[k] = id(c[k])
e[k] = id(d[k])
f[k] = id(e[k])
#Result
for k in range(0,3):
sys.stdout.write("%3d"%(a[k]))
sys.stdout.write("%3d"%(a[k]))
sys.stdout.write("%3d"%(a[k]))
sys.stdout.write("%3d"%(a[k]))
sys.stdout.write("%3d\n"%(a[k]))
#There is no pointer concept in python
#Memory address differs in different systems
#Display string using character pointer
import sys
#Variable Initialization
name = ['K','U','M','A','R']
ch = id(name)
i = 0
#Result
while i < len(name): #There is no null terminating character in python string
sys.stdout.write("%c"%(name[i]))
i += 1
#There is no pointer concept in python
#Length of a given string including and excluding spaces using pointers
import sys
#Variable Initialization
p = 0
q = 0
s = 0
str1 = ['P','O','I','N','T','E','R','S',' ','A','R','E',' ','E','A','S','Y']
#Result
while s < len(str1): #There is no null termination character for python string
sys.stdout.write("%c"%(str1[s]))
p += 1
if ord(str1[s]) == 32:
q += 1
s += 1
sys.stdout.write("\nLength of String including spaces : %d"%(p))
sys.stdout.write("\nLength of String excluding spaces : %d"%(p-q))
#There is no pointer concept in python
#Interchange elements of character array using pointer
import sys
#Variable Initialization
names = ["kapil","manoj","amit","amol","pavan","mahesh"]
#Result
sys.stdout.write("Original : %s %s"%(names[3],names[4]))
tmp = names[3]
names[3] = names[4]
names[4] = tmp
sys.stdout.write("\nNew : %s %s"%(names[3],names[4]))
#There is no pointer concept in python
#String comparison.
import sys
#Variable Initialization
c = 0
str1 = raw_input("Enter First String : ")
str2 = raw_input("Enter Second String : ")
a = 0
l = 0
#Result
sys.stdout.write("\nSimilar Characters Found in Both String")
while a < len(str1):
if str1[a] == str2[a]:
sys.stdout.write("\n\t%c\t%c"%(str1[a],str2[a]))
l += 1
else:
c += 1
a += 1
if c == 0:
sys.stdout.write("\nThe Strings are Identical")
else:
sys.stdout.write("\nThe Strings are different at %d places."%(c))
sys.stdout.write("\nThe String Characters are similar at %d places."%(l))
#There is no pointer concept in python
#Compare three characters using pointers.
import sys
#Variable Initialization
x = raw_input("Enter Three Characters")
y = raw_input("Enter Three Characters")
z = raw_input("Enter Three Characters")
stat = 0
stat = y > x
if x == y:
sys.stdout.write("\n1st and 2nd Character are same\n")
else:
if stat < 0:
sys.stdout.write("\n2nd Character appears after the 1st Character in Alphabetic\n")
else:
sys.stdout.write("2nd Character appears before the first Character in Alphabetic\n")
stat = y > z
if y == z:
sys.stdout.write("\n2nd and 3rd Character are same.")
else:
if stat > 0:
sys.stdout.write("\n2nd Character appears after the 3rd Character in Alphabetic")
else:
sys.stdout.write("\n2nd Character appears before the 3rd Character in Alphabetic")
#Compare two strings irrespective of case.
import sys
#Variable Initialization
buf1 = "computer"
buf2 = "computer"
#Comparison
stat = buf1 == buf2
#Result
sys.stdout.write("The Characters upto 4th position are ")
if stat == 0:
sys.stdout.write("not ")
sys.stdout.write("same")
#There is no pointer concept in python and memicmp() function
#Print the string upto the first occurrence of a character
import sys
#Variable Initialization
src = raw_input("Enter a String : ")
f = raw_input("Enter a Character to find in the text : ")
dest = ['0' for i in range(0,len(src))]
ptr = 0
while ptr < len(src):
if src[ptr] == f:
dest[ptr] = src[ptr]
break
else:
dest[ptr] = src[ptr]
ptr += 1
if ptr == len(src):
sys.stdout.write("The character wasn't found\n")
else:
sys.stdout.write("String upto that Character : %s"%(dest[0:ptr+1]))
#Replace the contents of second string with the first string.
import sys
#Variable Initialization
src = raw_input("Enter a Source String : ")
dest = raw_input("Enter a Destination String :")
#Result
sys.stdout.write("\n\nDestination before memcpy : %s\n"%(dest))
dest = src[:len(src)] + dest[len(src):]
sys.stdout.write("Destination after memcpy : %s\n"%(dest))
#There is no pointer concept in python and memcpy function
#Display the string through their pointer
import sys
#Variable Initialization
c = "Central Processing Unit"
m = "Math Co- Processor"
#Result
sys.stdout.write("'c' is pointing the string '%s'\n"%(c))
sys.stdout.write("'m' is pointing the string '%s'\n"%(m))
#There is no pointer concept in python
#Use void pointer to display the value of different variables.
import sys
#Variable Initialization & Result
pt = 12
sys.stdout.write("\nP = %d"%(pt))
pt = 5.4
sys.stdout.write("\nR = %.1f"%(pt))
pt = 'S'
sys.stdout.write("\nC = %c"%(pt))
#There is no pointer concept in python